[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / utils / net_rpc.c
blobfee994b751973aa56af1e1a907aece373cd13ccb
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 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "includes.h"
24 #include "utils/net.h"
26 static int net_mode_share;
28 /**
29 * @file net_rpc.c
31 * @brief RPC based subcommands for the 'net' utility.
33 * This file should contain much of the functionality that used to
34 * be found in rpcclient, execpt that the commands should change
35 * less often, and the fucntionality should be sane (the user is not
36 * expected to know a rid/sid before they conduct an operation etc.)
38 * @todo Perhaps eventually these should be split out into a number
39 * of files, as this could get quite big.
40 **/
43 /**
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
50 **/
52 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
53 DOM_SID **domain_sid, char **domain_name)
55 struct rpc_pipe_client *lsa_pipe;
56 POLICY_HND pol;
57 NTSTATUS result = NT_STATUS_OK;
58 uint32 info_class = 5;
60 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
61 if (!lsa_pipe) {
62 d_fprintf(stderr, "Could not initialise lsa pipe\n");
63 return result;
66 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False,
67 SEC_RIGHTS_MAXIMUM_ALLOWED,
68 &pol);
69 if (!NT_STATUS_IS_OK(result)) {
70 d_fprintf(stderr, "open_policy failed: %s\n",
71 nt_errstr(result));
72 return result;
75 result = rpccli_lsa_query_info_policy(lsa_pipe, mem_ctx, &pol,
76 info_class, domain_name,
77 domain_sid);
78 if (!NT_STATUS_IS_OK(result)) {
79 d_fprintf(stderr, "lsaquery failed: %s\n",
80 nt_errstr(result));
81 return result;
84 rpccli_lsa_close(lsa_pipe, mem_ctx, &pol);
85 cli_rpc_pipe_close(lsa_pipe);
87 return NT_STATUS_OK;
90 /**
91 * Run a single RPC command, from start to finish.
93 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
94 * @param conn_flag a NET_FLAG_ combination. Passed to
95 * net_make_ipc_connection.
96 * @param argc Standard main() style argc
97 * @param argc Standard main() style argv. Initial components are already
98 * stripped
99 * @return A shell status integer (0 for success)
102 int run_rpc_command(struct cli_state *cli_arg,
103 const int pipe_idx,
104 int conn_flags,
105 rpc_command_fn fn,
106 int argc,
107 const char **argv)
109 struct cli_state *cli = NULL;
110 struct rpc_pipe_client *pipe_hnd = NULL;
111 TALLOC_CTX *mem_ctx;
112 NTSTATUS nt_status;
113 DOM_SID *domain_sid;
114 char *domain_name;
116 /* make use of cli_state handed over as an argument, if possible */
117 if (!cli_arg) {
118 cli = net_make_ipc_connection(conn_flags);
119 } else {
120 cli = cli_arg;
123 if (!cli) {
124 return -1;
127 /* Create mem_ctx */
129 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
130 DEBUG(0, ("talloc_init() failed\n"));
131 cli_shutdown(cli);
132 return -1;
135 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
136 &domain_name);
137 if (!NT_STATUS_IS_OK(nt_status)) {
138 cli_shutdown(cli);
139 return -1;
142 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
143 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
144 /* Always try and create an schannel netlogon pipe. */
145 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
146 PIPE_AUTH_LEVEL_PRIVACY,
147 domain_name,
148 &nt_status);
149 if (!pipe_hnd) {
150 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
151 nt_errstr(nt_status) ));
152 cli_shutdown(cli);
153 return -1;
155 } else {
156 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
157 if (!pipe_hnd) {
158 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
159 cli_get_pipe_name(pipe_idx),
160 nt_errstr(nt_status) ));
161 cli_shutdown(cli);
162 return -1;
167 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
169 if (!NT_STATUS_IS_OK(nt_status)) {
170 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
171 } else {
172 DEBUG(5, ("rpc command function succedded\n"));
175 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
176 if (pipe_hnd) {
177 cli_rpc_pipe_close(pipe_hnd);
181 /* close the connection only if it was opened here */
182 if (!cli_arg) {
183 cli_shutdown(cli);
186 talloc_destroy(mem_ctx);
187 return (!NT_STATUS_IS_OK(nt_status));
190 /**
191 * Force a change of the trust acccount password.
193 * All parameters are provided by the run_rpc_command function, except for
194 * argc, argv which are passes through.
196 * @param domain_sid The domain sid aquired from the remote server
197 * @param cli A cli_state connected to the server.
198 * @param mem_ctx Talloc context, destoyed on compleation of the function.
199 * @param argc Standard main() style argc
200 * @param argc Standard main() style argv. Initial components are already
201 * stripped
203 * @return Normal NTSTATUS return.
206 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
207 const char *domain_name,
208 struct cli_state *cli,
209 struct rpc_pipe_client *pipe_hnd,
210 TALLOC_CTX *mem_ctx,
211 int argc,
212 const char **argv)
215 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
218 /**
219 * Force a change of the trust acccount password.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
223 * stripped
225 * @return A shell status integer (0 for success)
228 int net_rpc_changetrustpw(int argc, const char **argv)
230 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
231 rpc_changetrustpw_internals,
232 argc, argv);
235 /**
236 * Join a domain, the old way.
238 * This uses 'machinename' as the inital password, and changes it.
240 * The password should be created with 'server manager' or equiv first.
242 * All parameters are provided by the run_rpc_command function, except for
243 * argc, argv which are passes through.
245 * @param domain_sid The domain sid aquired from the remote server
246 * @param cli A cli_state connected to the server.
247 * @param mem_ctx Talloc context, destoyed on compleation of the function.
248 * @param argc Standard main() style argc
249 * @param argc Standard main() style argv. Initial components are already
250 * stripped
252 * @return Normal NTSTATUS return.
255 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
256 const char *domain_name,
257 struct cli_state *cli,
258 struct rpc_pipe_client *pipe_hnd,
259 TALLOC_CTX *mem_ctx,
260 int argc,
261 const char **argv)
264 fstring trust_passwd;
265 unsigned char orig_trust_passwd_hash[16];
266 NTSTATUS result;
267 uint32 sec_channel_type;
269 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
270 if (!pipe_hnd) {
271 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
272 "error was %s\n",
273 cli->desthost,
274 nt_errstr(result) ));
275 return result;
279 check what type of join - if the user want's to join as
280 a BDC, the server must agree that we are a BDC.
282 if (argc >= 0) {
283 sec_channel_type = get_sec_channel_type(argv[0]);
284 } else {
285 sec_channel_type = get_sec_channel_type(NULL);
288 fstrcpy(trust_passwd, global_myname());
289 strlower_m(trust_passwd);
292 * Machine names can be 15 characters, but the max length on
293 * a password is 14. --jerry
296 trust_passwd[14] = '\0';
298 E_md4hash(trust_passwd, orig_trust_passwd_hash);
300 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
301 orig_trust_passwd_hash,
302 sec_channel_type);
304 if (NT_STATUS_IS_OK(result))
305 printf("Joined domain %s.\n",opt_target_workgroup);
308 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
309 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
310 result = NT_STATUS_UNSUCCESSFUL;
313 return result;
316 /**
317 * Join a domain, the old way.
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
321 * stripped
323 * @return A shell status integer (0 for success)
326 static int net_rpc_perform_oldjoin(int argc, const char **argv)
328 return run_rpc_command(NULL, PI_NETLOGON,
329 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
330 rpc_oldjoin_internals,
331 argc, argv);
334 /**
335 * Join a domain, the old way. This function exists to allow
336 * the message to be displayed when oldjoin was explicitly
337 * requested, but not when it was implied by "net rpc join"
339 * @param argc Standard main() style argc
340 * @param argc Standard main() style argv. Initial components are already
341 * stripped
343 * @return A shell status integer (0 for success)
346 static int net_rpc_oldjoin(int argc, const char **argv)
348 int rc = net_rpc_perform_oldjoin(argc, argv);
350 if (rc) {
351 d_fprintf(stderr, "Failed to join domain\n");
354 return rc;
357 /**
358 * Basic usage function for 'net rpc join'
359 * @param argc Standard main() style argc
360 * @param argc Standard main() style argv. Initial components are already
361 * stripped
364 static int rpc_join_usage(int argc, const char **argv)
366 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
367 "\t to join a domain with admin username & password\n"\
368 "\t\t password will be prompted if needed and none is specified\n"\
369 "\t <type> can be (default MEMBER)\n"\
370 "\t\t BDC - Join as a BDC\n"\
371 "\t\t PDC - Join as a PDC\n"\
372 "\t\t MEMBER - Join as a MEMBER server\n");
374 net_common_flags_usage(argc, argv);
375 return -1;
378 /**
379 * 'net rpc join' entrypoint.
380 * @param argc Standard main() style argc
381 * @param argc Standard main() style argv. Initial components are already
382 * stripped
384 * Main 'net_rpc_join()' (where the admain username/password is used) is
385 * in net_rpc_join.c
386 * Try to just change the password, but if that doesn't work, use/prompt
387 * for a username/password.
390 int net_rpc_join(int argc, const char **argv)
392 if (lp_server_role() == ROLE_STANDALONE) {
393 d_printf("cannot join as standalone machine\n");
394 return -1;
397 if (strlen(global_myname()) > 15) {
398 d_printf("Our netbios name can be at most 15 chars long, "
399 "\"%s\" is %u chars long\n",
400 global_myname(), (unsigned int)strlen(global_myname()));
401 return -1;
404 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
405 return 0;
407 return net_rpc_join_newstyle(argc, argv);
410 /**
411 * display info about a rpc domain
413 * All parameters are provided by the run_rpc_command function, except for
414 * argc, argv which are passed through.
416 * @param domain_sid The domain sid acquired from the remote server
417 * @param cli A cli_state connected to the server.
418 * @param mem_ctx Talloc context, destoyed on completion of the function.
419 * @param argc Standard main() style argc
420 * @param argv Standard main() style argv. Initial components are already
421 * stripped
423 * @return Normal NTSTATUS return.
426 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
427 const char *domain_name,
428 struct cli_state *cli,
429 struct rpc_pipe_client *pipe_hnd,
430 TALLOC_CTX *mem_ctx,
431 int argc,
432 const char **argv)
434 POLICY_HND connect_pol, domain_pol;
435 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
436 SAM_UNK_CTR ctr;
437 fstring sid_str;
439 sid_to_string(sid_str, domain_sid);
441 /* Get sam policy handle */
442 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
443 &connect_pol);
444 if (!NT_STATUS_IS_OK(result)) {
445 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
446 goto done;
449 /* Get domain policy handle */
450 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
451 MAXIMUM_ALLOWED_ACCESS,
452 domain_sid, &domain_pol);
453 if (!NT_STATUS_IS_OK(result)) {
454 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
455 goto done;
458 ZERO_STRUCT(ctr);
459 result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
460 2, &ctr);
461 if (NT_STATUS_IS_OK(result)) {
462 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
463 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
464 d_printf("Domain SID: %s\n", sid_str);
465 d_printf("Sequence number: %llu\n", (unsigned long long)ctr.info.inf2.seq_num);
466 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
467 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
468 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
469 talloc_destroy(ctx);
472 done:
473 return result;
476 /**
477 * 'net rpc info' entrypoint.
478 * @param argc Standard main() style argc
479 * @param argc Standard main() style argv. Initial components are already
480 * stripped
483 int net_rpc_info(int argc, const char **argv)
485 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
486 rpc_info_internals,
487 argc, argv);
490 /**
491 * Fetch domain SID into the local secrets.tdb
493 * All parameters are provided by the run_rpc_command function, except for
494 * argc, argv which are passes through.
496 * @param domain_sid The domain sid acquired from the remote server
497 * @param cli A cli_state connected to the server.
498 * @param mem_ctx Talloc context, destoyed on completion of the function.
499 * @param argc Standard main() style argc
500 * @param argv Standard main() style argv. Initial components are already
501 * stripped
503 * @return Normal NTSTATUS return.
506 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
507 const char *domain_name,
508 struct cli_state *cli,
509 struct rpc_pipe_client *pipe_hnd,
510 TALLOC_CTX *mem_ctx,
511 int argc,
512 const char **argv)
514 fstring sid_str;
516 sid_to_string(sid_str, domain_sid);
517 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
518 sid_str, domain_name);
520 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
521 DEBUG(0,("Can't store domain SID\n"));
522 return NT_STATUS_UNSUCCESSFUL;
525 return NT_STATUS_OK;
528 /**
529 * 'net rpc getsid' entrypoint.
530 * @param argc Standard main() style argc
531 * @param argc Standard main() style argv. Initial components are already
532 * stripped
535 int net_rpc_getsid(int argc, const char **argv)
537 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
538 rpc_getsid_internals,
539 argc, argv);
542 /****************************************************************************/
545 * Basic usage function for 'net rpc user'
546 * @param argc Standard main() style argc.
547 * @param argv Standard main() style argv. Initial components are already
548 * stripped.
551 static int rpc_user_usage(int argc, const char **argv)
553 return net_help_user(argc, argv);
556 /**
557 * Add a new user to a remote RPC server
559 * All parameters are provided by the run_rpc_command function, except for
560 * argc, argv which are passes through.
562 * @param domain_sid The domain sid acquired from the remote server
563 * @param cli A cli_state connected to the server.
564 * @param mem_ctx Talloc context, destoyed on completion of the function.
565 * @param argc Standard main() style argc
566 * @param argv Standard main() style argv. Initial components are already
567 * stripped
569 * @return Normal NTSTATUS return.
572 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
573 const char *domain_name,
574 struct cli_state *cli,
575 struct rpc_pipe_client *pipe_hnd,
576 TALLOC_CTX *mem_ctx,
577 int argc, const char **argv)
580 POLICY_HND connect_pol, domain_pol, user_pol;
581 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
582 const char *acct_name;
583 uint32 acb_info;
584 uint32 unknown, user_rid;
586 if (argc < 1) {
587 d_printf("User must be specified\n");
588 rpc_user_usage(argc, argv);
589 return NT_STATUS_OK;
592 acct_name = argv[0];
594 /* Get sam policy handle */
596 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
597 &connect_pol);
598 if (!NT_STATUS_IS_OK(result)) {
599 goto done;
602 /* Get domain policy handle */
604 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
605 MAXIMUM_ALLOWED_ACCESS,
606 domain_sid, &domain_pol);
607 if (!NT_STATUS_IS_OK(result)) {
608 goto done;
611 /* Create domain user */
613 acb_info = ACB_NORMAL;
614 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
616 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
617 acct_name, acb_info, unknown,
618 &user_pol, &user_rid);
619 if (!NT_STATUS_IS_OK(result)) {
620 goto done;
623 if (argc == 2) {
625 uint32 *user_rids, num_rids, *name_types;
626 uint32 flags = 0x000003e8; /* Unknown */
627 SAM_USERINFO_CTR ctr;
628 SAM_USER_INFO_24 p24;
629 uchar pwbuf[516];
631 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
632 flags, 1, &acct_name,
633 &num_rids, &user_rids,
634 &name_types);
636 if (!NT_STATUS_IS_OK(result)) {
637 goto done;
640 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
641 MAXIMUM_ALLOWED_ACCESS,
642 user_rids[0], &user_pol);
644 if (!NT_STATUS_IS_OK(result)) {
645 goto done;
648 /* Set password on account */
650 ZERO_STRUCT(ctr);
651 ZERO_STRUCT(p24);
653 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
655 init_sam_user_info24(&p24, (char *)pwbuf,24);
657 ctr.switch_value = 24;
658 ctr.info.id24 = &p24;
660 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
661 &cli->user_session_key, &ctr);
663 if (!NT_STATUS_IS_OK(result)) {
664 d_fprintf(stderr, "Failed to set password for user %s - %s\n",
665 acct_name, nt_errstr(result));
667 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
669 if (!NT_STATUS_IS_OK(result)) {
670 d_fprintf(stderr, "Failed to delete user %s - %s\n",
671 acct_name, nt_errstr(result));
672 return result;
677 done:
678 if (!NT_STATUS_IS_OK(result)) {
679 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name,
680 nt_errstr(result));
681 } else {
682 d_printf("Added user %s\n", acct_name);
684 return result;
687 /**
688 * Add a new user to a remote RPC server
690 * @param argc Standard main() style argc
691 * @param argv Standard main() style argv. Initial components are already
692 * stripped
694 * @return A shell status integer (0 for success)
697 static int rpc_user_add(int argc, const char **argv)
699 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
700 argc, argv);
703 /**
704 * Delete a user from a remote RPC server
706 * All parameters are provided by the run_rpc_command function, except for
707 * argc, argv which are passes through.
709 * @param domain_sid The domain sid acquired from the remote server
710 * @param cli A cli_state connected to the server.
711 * @param mem_ctx Talloc context, destoyed on completion of the function.
712 * @param argc Standard main() style argc
713 * @param argv Standard main() style argv. Initial components are already
714 * stripped
716 * @return Normal NTSTATUS return.
719 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
720 const char *domain_name,
721 struct cli_state *cli,
722 struct rpc_pipe_client *pipe_hnd,
723 TALLOC_CTX *mem_ctx,
724 int argc,
725 const char **argv)
727 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
728 POLICY_HND connect_pol, domain_pol, user_pol;
730 if (argc < 1) {
731 d_printf("User must be specified\n");
732 rpc_user_usage(argc, argv);
733 return NT_STATUS_OK;
735 /* Get sam policy and domain handles */
737 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
738 &connect_pol);
740 if (!NT_STATUS_IS_OK(result)) {
741 goto done;
744 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
745 MAXIMUM_ALLOWED_ACCESS,
746 domain_sid, &domain_pol);
748 if (!NT_STATUS_IS_OK(result)) {
749 goto done;
752 /* Get handle on user */
755 uint32 *user_rids, num_rids, *name_types;
756 uint32 flags = 0x000003e8; /* Unknown */
758 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
759 flags, 1, &argv[0],
760 &num_rids, &user_rids,
761 &name_types);
763 if (!NT_STATUS_IS_OK(result)) {
764 goto done;
767 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
768 MAXIMUM_ALLOWED_ACCESS,
769 user_rids[0], &user_pol);
771 if (!NT_STATUS_IS_OK(result)) {
772 goto done;
776 /* Delete user */
778 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
780 if (!NT_STATUS_IS_OK(result)) {
781 goto done;
784 /* Display results */
785 if (!NT_STATUS_IS_OK(result)) {
786 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
787 } else {
788 d_printf("Deleted user account\n");
791 done:
792 return result;
795 /**
796 * Rename a user on a remote RPC server
798 * All parameters are provided by the run_rpc_command function, except for
799 * argc, argv which are passes through.
801 * @param domain_sid The domain sid acquired from the remote server
802 * @param cli A cli_state connected to the server.
803 * @param mem_ctx Talloc context, destoyed on completion of the function.
804 * @param argc Standard main() style argc
805 * @param argv Standard main() style argv. Initial components are already
806 * stripped
808 * @return Normal NTSTATUS return.
811 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
812 const char *domain_name,
813 struct cli_state *cli,
814 struct rpc_pipe_client *pipe_hnd,
815 TALLOC_CTX *mem_ctx,
816 int argc,
817 const char **argv)
819 POLICY_HND connect_pol, domain_pol, user_pol;
820 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
821 uint32 info_level = 7;
822 const char *old_name, *new_name;
823 uint32 *user_rid;
824 uint32 flags = 0x000003e8; /* Unknown */
825 uint32 num_rids, *name_types;
826 uint32 num_names = 1;
827 const char **names;
828 SAM_USERINFO_CTR *user_ctr;
829 SAM_USERINFO_CTR ctr;
830 SAM_USER_INFO_7 info7;
832 if (argc != 2) {
833 d_printf("Old and new username must be specified\n");
834 rpc_user_usage(argc, argv);
835 return NT_STATUS_OK;
838 old_name = argv[0];
839 new_name = argv[1];
841 ZERO_STRUCT(ctr);
842 ZERO_STRUCT(user_ctr);
844 /* Get sam policy handle */
846 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
847 &connect_pol);
848 if (!NT_STATUS_IS_OK(result)) {
849 goto done;
852 /* Get domain policy handle */
854 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
855 MAXIMUM_ALLOWED_ACCESS,
856 domain_sid, &domain_pol);
857 if (!NT_STATUS_IS_OK(result)) {
858 goto done;
861 if ((names = TALLOC_ARRAY(mem_ctx, const char *, num_names)) == NULL) {
862 result = NT_STATUS_NO_MEMORY;
863 goto done;
865 names[0] = old_name;
866 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
867 flags, num_names, names,
868 &num_rids, &user_rid, &name_types);
869 if (!NT_STATUS_IS_OK(result)) {
870 goto done;
873 /* Open domain user */
874 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
875 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
877 if (!NT_STATUS_IS_OK(result)) {
878 goto done;
881 /* Query user info */
882 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
883 info_level, &user_ctr);
885 if (!NT_STATUS_IS_OK(result)) {
886 goto done;
889 ctr.switch_value = info_level;
890 ctr.info.id7 = &info7;
892 init_sam_user_info7(&info7, new_name);
894 /* Set new name */
895 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
896 info_level, &cli->user_session_key, &ctr);
898 if (!NT_STATUS_IS_OK(result)) {
899 goto done;
902 done:
903 if (!NT_STATUS_IS_OK(result)) {
904 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
905 nt_errstr(result));
906 } else {
907 d_printf("Renamed user from %s to %s\n", old_name, new_name);
909 return result;
912 /**
913 * Rename a user on a remote RPC server
915 * @param argc Standard main() style argc
916 * @param argv Standard main() style argv. Initial components are already
917 * stripped
919 * @return A shell status integer (0 for success)
922 static int rpc_user_rename(int argc, const char **argv)
924 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
925 argc, argv);
928 /**
929 * Delete a user from a remote RPC server
931 * @param argc Standard main() style argc
932 * @param argv Standard main() style argv. Initial components are already
933 * stripped
935 * @return A shell status integer (0 for success)
938 static int rpc_user_delete(int argc, const char **argv)
940 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
941 argc, argv);
944 /**
945 * Set a password for a user on a remote RPC server
947 * All parameters are provided by the run_rpc_command function, except for
948 * argc, argv which are passes through.
950 * @param domain_sid The domain sid acquired from the remote server
951 * @param cli A cli_state connected to the server.
952 * @param mem_ctx Talloc context, destoyed on completion of the function.
953 * @param argc Standard main() style argc
954 * @param argv Standard main() style argv. Initial components are already
955 * stripped
957 * @return Normal NTSTATUS return.
960 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
961 const char *domain_name,
962 struct cli_state *cli,
963 struct rpc_pipe_client *pipe_hnd,
964 TALLOC_CTX *mem_ctx,
965 int argc,
966 const char **argv)
968 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
969 POLICY_HND connect_pol, domain_pol, user_pol;
970 SAM_USERINFO_CTR ctr;
971 SAM_USER_INFO_24 p24;
972 uchar pwbuf[516];
973 const char *user;
974 const char *new_password;
975 char *prompt = NULL;
977 if (argc < 1) {
978 d_printf("User must be specified\n");
979 rpc_user_usage(argc, argv);
980 return NT_STATUS_OK;
983 user = argv[0];
985 if (argv[1]) {
986 new_password = argv[1];
987 } else {
988 asprintf(&prompt, "Enter new password for %s:", user);
989 new_password = getpass(prompt);
990 SAFE_FREE(prompt);
993 /* Get sam policy and domain handles */
995 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
996 &connect_pol);
998 if (!NT_STATUS_IS_OK(result)) {
999 goto done;
1002 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1003 MAXIMUM_ALLOWED_ACCESS,
1004 domain_sid, &domain_pol);
1006 if (!NT_STATUS_IS_OK(result)) {
1007 goto done;
1010 /* Get handle on user */
1013 uint32 *user_rids, num_rids, *name_types;
1014 uint32 flags = 0x000003e8; /* Unknown */
1016 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1017 flags, 1, &user,
1018 &num_rids, &user_rids,
1019 &name_types);
1021 if (!NT_STATUS_IS_OK(result)) {
1022 goto done;
1025 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1026 MAXIMUM_ALLOWED_ACCESS,
1027 user_rids[0], &user_pol);
1029 if (!NT_STATUS_IS_OK(result)) {
1030 goto done;
1034 /* Set password on account */
1036 ZERO_STRUCT(ctr);
1037 ZERO_STRUCT(p24);
1039 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1041 init_sam_user_info24(&p24, (char *)pwbuf,24);
1043 ctr.switch_value = 24;
1044 ctr.info.id24 = &p24;
1046 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
1047 &cli->user_session_key, &ctr);
1049 if (!NT_STATUS_IS_OK(result)) {
1050 goto done;
1053 /* Display results */
1055 done:
1056 return result;
1060 /**
1061 * Set a user's password on a remote RPC server
1063 * @param argc Standard main() style argc
1064 * @param argv Standard main() style argv. Initial components are already
1065 * stripped
1067 * @return A shell status integer (0 for success)
1070 static int rpc_user_password(int argc, const char **argv)
1072 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1073 argc, argv);
1076 /**
1077 * List user's groups on a remote RPC server
1079 * All parameters are provided by the run_rpc_command function, except for
1080 * argc, argv which are passes through.
1082 * @param domain_sid The domain sid acquired from the remote server
1083 * @param cli A cli_state connected to the server.
1084 * @param mem_ctx Talloc context, destoyed on completion of the function.
1085 * @param argc Standard main() style argc
1086 * @param argv Standard main() style argv. Initial components are already
1087 * stripped
1089 * @return Normal NTSTATUS return.
1092 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1093 const char *domain_name,
1094 struct cli_state *cli,
1095 struct rpc_pipe_client *pipe_hnd,
1096 TALLOC_CTX *mem_ctx,
1097 int argc,
1098 const char **argv)
1100 POLICY_HND connect_pol, domain_pol, user_pol;
1101 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1102 uint32 *rids, num_rids, *name_types, num_names;
1103 uint32 flags = 0x000003e8; /* Unknown */
1104 int i;
1105 char **names;
1106 DOM_GID *user_gids;
1108 if (argc < 1) {
1109 d_printf("User must be specified\n");
1110 rpc_user_usage(argc, argv);
1111 return NT_STATUS_OK;
1113 /* Get sam policy handle */
1115 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1116 &connect_pol);
1117 if (!NT_STATUS_IS_OK(result)) goto done;
1119 /* Get domain policy handle */
1121 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1122 MAXIMUM_ALLOWED_ACCESS,
1123 domain_sid, &domain_pol);
1124 if (!NT_STATUS_IS_OK(result)) goto done;
1126 /* Get handle on user */
1128 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1129 flags, 1, &argv[0],
1130 &num_rids, &rids, &name_types);
1132 if (!NT_STATUS_IS_OK(result)) goto done;
1134 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1135 MAXIMUM_ALLOWED_ACCESS,
1136 rids[0], &user_pol);
1137 if (!NT_STATUS_IS_OK(result)) goto done;
1139 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1140 &num_rids, &user_gids);
1142 if (!NT_STATUS_IS_OK(result)) goto done;
1144 /* Look up rids */
1146 if (num_rids) {
1147 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1148 result = NT_STATUS_NO_MEMORY;
1149 goto done;
1152 for (i = 0; i < num_rids; i++)
1153 rids[i] = user_gids[i].g_rid;
1155 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1156 num_rids, rids,
1157 &num_names, &names, &name_types);
1159 if (!NT_STATUS_IS_OK(result)) {
1160 goto done;
1163 /* Display results */
1165 for (i = 0; i < num_names; i++)
1166 printf("%s\n", names[i]);
1168 done:
1169 return result;
1172 /**
1173 * List a user's groups from a remote RPC server
1175 * @param argc Standard main() style argc
1176 * @param argv Standard main() style argv. Initial components are already
1177 * stripped
1179 * @return A shell status integer (0 for success)
1182 static int rpc_user_info(int argc, const char **argv)
1184 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1185 argc, argv);
1188 /**
1189 * List users on a remote RPC server
1191 * All parameters are provided by the run_rpc_command function, except for
1192 * argc, argv which are passes through.
1194 * @param domain_sid The domain sid acquired from the remote server
1195 * @param cli A cli_state connected to the server.
1196 * @param mem_ctx Talloc context, destoyed on completion of the function.
1197 * @param argc Standard main() style argc
1198 * @param argv Standard main() style argv. Initial components are already
1199 * stripped
1201 * @return Normal NTSTATUS return.
1204 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1205 const char *domain_name,
1206 struct cli_state *cli,
1207 struct rpc_pipe_client *pipe_hnd,
1208 TALLOC_CTX *mem_ctx,
1209 int argc,
1210 const char **argv)
1212 POLICY_HND connect_pol, domain_pol;
1213 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1214 uint32 start_idx=0, num_entries, i, loop_count = 0;
1215 SAM_DISPINFO_CTR ctr;
1216 SAM_DISPINFO_1 info1;
1218 /* Get sam policy handle */
1220 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1221 &connect_pol);
1222 if (!NT_STATUS_IS_OK(result)) {
1223 goto done;
1226 /* Get domain policy handle */
1228 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1229 MAXIMUM_ALLOWED_ACCESS,
1230 domain_sid, &domain_pol);
1231 if (!NT_STATUS_IS_OK(result)) {
1232 goto done;
1235 /* Query domain users */
1236 ZERO_STRUCT(ctr);
1237 ZERO_STRUCT(info1);
1238 ctr.sam.info1 = &info1;
1239 if (opt_long_list_entries)
1240 d_printf("\nUser name Comment"\
1241 "\n-----------------------------\n");
1242 do {
1243 fstring user, desc;
1244 uint32 max_entries, max_size;
1246 get_query_dispinfo_params(
1247 loop_count, &max_entries, &max_size);
1249 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1250 &start_idx, 1, &num_entries,
1251 max_entries, max_size, &ctr);
1252 loop_count++;
1254 for (i = 0; i < num_entries; i++) {
1255 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1256 if (opt_long_list_entries)
1257 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1259 if (opt_long_list_entries)
1260 printf("%-21.21s %s\n", user, desc);
1261 else
1262 printf("%s\n", user);
1264 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1266 done:
1267 return result;
1270 /**
1271 * 'net rpc user' entrypoint.
1272 * @param argc Standard main() style argc
1273 * @param argc Standard main() style argv. Initial components are already
1274 * stripped
1277 int net_rpc_user(int argc, const char **argv)
1279 struct functable func[] = {
1280 {"add", rpc_user_add},
1281 {"info", rpc_user_info},
1282 {"delete", rpc_user_delete},
1283 {"password", rpc_user_password},
1284 {"rename", rpc_user_rename},
1285 {NULL, NULL}
1288 if (argc == 0) {
1289 return run_rpc_command(NULL,PI_SAMR, 0,
1290 rpc_user_list_internals,
1291 argc, argv);
1294 return net_run_function(argc, argv, func, rpc_user_usage);
1297 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1298 struct rpc_sh_ctx *ctx,
1299 struct rpc_pipe_client *pipe_hnd,
1300 int argc, const char **argv)
1302 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1303 ctx->cli, pipe_hnd, mem_ctx,
1304 argc, argv);
1307 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1308 struct rpc_sh_ctx *ctx,
1309 struct rpc_pipe_client *pipe_hnd,
1310 int argc, const char **argv)
1312 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1313 ctx->cli, pipe_hnd, mem_ctx,
1314 argc, argv);
1317 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1318 struct rpc_sh_ctx *ctx,
1319 struct rpc_pipe_client *pipe_hnd,
1320 int argc, const char **argv,
1321 NTSTATUS (*fn)(
1322 TALLOC_CTX *mem_ctx,
1323 struct rpc_sh_ctx *ctx,
1324 struct rpc_pipe_client *pipe_hnd,
1325 const POLICY_HND *user_hnd,
1326 int argc, const char **argv))
1329 POLICY_HND connect_pol, domain_pol, user_pol;
1330 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1331 DOM_SID sid;
1332 uint32 rid;
1333 enum lsa_SidType type;
1335 if (argc == 0) {
1336 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1337 return NT_STATUS_INVALID_PARAMETER;
1340 ZERO_STRUCT(connect_pol);
1341 ZERO_STRUCT(domain_pol);
1342 ZERO_STRUCT(user_pol);
1344 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1345 NULL, NULL, &sid, &type);
1346 if (!NT_STATUS_IS_OK(result)) {
1347 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1348 nt_errstr(result));
1349 goto done;
1352 if (type != SID_NAME_USER) {
1353 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1354 sid_type_lookup(type));
1355 result = NT_STATUS_NO_SUCH_USER;
1356 goto done;
1359 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1360 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1361 result = NT_STATUS_NO_SUCH_USER;
1362 goto done;
1365 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1366 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1367 if (!NT_STATUS_IS_OK(result)) {
1368 goto done;
1371 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1372 MAXIMUM_ALLOWED_ACCESS,
1373 ctx->domain_sid, &domain_pol);
1374 if (!NT_STATUS_IS_OK(result)) {
1375 goto done;
1378 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1379 MAXIMUM_ALLOWED_ACCESS,
1380 rid, &user_pol);
1381 if (!NT_STATUS_IS_OK(result)) {
1382 goto done;
1385 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1387 done:
1388 if (is_valid_policy_hnd(&user_pol)) {
1389 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1391 if (is_valid_policy_hnd(&domain_pol)) {
1392 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1394 if (is_valid_policy_hnd(&connect_pol)) {
1395 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1397 return result;
1400 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1401 struct rpc_sh_ctx *ctx,
1402 struct rpc_pipe_client *pipe_hnd,
1403 const POLICY_HND *user_hnd,
1404 int argc, const char **argv)
1406 NTSTATUS result;
1407 SAM_USERINFO_CTR *ctr;
1408 SAM_USER_INFO_21 *info;
1410 if (argc != 0) {
1411 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1412 return NT_STATUS_INVALID_PARAMETER;
1415 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1416 21, &ctr);
1417 if (!NT_STATUS_IS_OK(result)) {
1418 return result;
1421 info = ctr->info.id21;
1423 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1424 info->group_rid);
1426 return result;
1429 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1430 struct rpc_sh_ctx *ctx,
1431 struct rpc_pipe_client *pipe_hnd,
1432 int argc, const char **argv)
1434 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1435 rpc_sh_user_show_internals);
1438 #define FETCHSTR(name, rec) \
1439 do { if (strequal(ctx->thiscmd, name)) { \
1440 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1441 } while (0);
1443 #define SETSTR(name, rec, flag) \
1444 do { if (strequal(ctx->thiscmd, name)) { \
1445 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1446 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1447 usr->fields_present |= ACCT_##flag; } \
1448 } while (0);
1450 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1451 struct rpc_sh_ctx *ctx,
1452 struct rpc_pipe_client *pipe_hnd,
1453 const POLICY_HND *user_hnd,
1454 int argc, const char **argv)
1456 NTSTATUS result;
1457 SAM_USERINFO_CTR *ctr;
1458 SAM_USER_INFO_21 *usr;
1459 const char *username;
1460 const char *oldval = "";
1462 if (argc > 1) {
1463 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1464 ctx->whoami);
1465 return NT_STATUS_INVALID_PARAMETER;
1468 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1469 21, &ctr);
1470 if (!NT_STATUS_IS_OK(result)) {
1471 return result;
1474 usr = ctr->info.id21;
1476 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1478 FETCHSTR("fullname", full_name);
1479 FETCHSTR("homedir", home_dir);
1480 FETCHSTR("homedrive", dir_drive);
1481 FETCHSTR("logonscript", logon_script);
1482 FETCHSTR("profilepath", profile_path);
1483 FETCHSTR("description", acct_desc);
1485 if (argc == 0) {
1486 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1487 goto done;
1490 ZERO_STRUCTP(usr);
1492 if (strcmp(argv[0], "NULL") == 0) {
1493 argv[0] = "";
1496 SETSTR("fullname", full_name, FULL_NAME);
1497 SETSTR("homedir", home_dir, HOME_DIR);
1498 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1499 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1500 SETSTR("profilepath", profile_path, PROFILE);
1501 SETSTR("description", acct_desc, DESCRIPTION);
1503 result = rpccli_samr_set_userinfo2(
1504 pipe_hnd, mem_ctx, user_hnd, 21,
1505 &pipe_hnd->cli->user_session_key, ctr);
1507 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1508 ctx->thiscmd, oldval, argv[0]);
1510 done:
1512 return result;
1515 #define HANDLEFLG(name, rec) \
1516 do { if (strequal(ctx->thiscmd, name)) { \
1517 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1518 if (newval) { \
1519 newflags = oldflags | ACB_##rec; \
1520 } else { \
1521 newflags = oldflags & ~ACB_##rec; \
1522 } } } while (0);
1524 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1525 struct rpc_sh_ctx *ctx,
1526 struct rpc_pipe_client *pipe_hnd,
1527 int argc, const char **argv)
1529 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1530 rpc_sh_user_str_edit_internals);
1533 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1534 struct rpc_sh_ctx *ctx,
1535 struct rpc_pipe_client *pipe_hnd,
1536 const POLICY_HND *user_hnd,
1537 int argc, const char **argv)
1539 NTSTATUS result;
1540 SAM_USERINFO_CTR *ctr;
1541 SAM_USER_INFO_21 *usr;
1542 const char *username;
1543 const char *oldval = "unknown";
1544 uint32 oldflags, newflags;
1545 BOOL newval;
1547 if ((argc > 1) ||
1548 ((argc == 1) && !strequal(argv[0], "yes") &&
1549 !strequal(argv[0], "no"))) {
1550 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1551 ctx->whoami);
1552 return NT_STATUS_INVALID_PARAMETER;
1555 newval = strequal(argv[0], "yes");
1557 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1558 21, &ctr);
1559 if (!NT_STATUS_IS_OK(result)) {
1560 return result;
1563 usr = ctr->info.id21;
1565 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1566 oldflags = usr->acb_info;
1567 newflags = usr->acb_info;
1569 HANDLEFLG("disabled", DISABLED);
1570 HANDLEFLG("pwnotreq", PWNOTREQ);
1571 HANDLEFLG("autolock", AUTOLOCK);
1572 HANDLEFLG("pwnoexp", PWNOEXP);
1574 if (argc == 0) {
1575 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1576 goto done;
1579 ZERO_STRUCTP(usr);
1581 usr->acb_info = newflags;
1582 usr->fields_present = ACCT_FLAGS;
1584 result = rpccli_samr_set_userinfo2(
1585 pipe_hnd, mem_ctx, user_hnd, 21,
1586 &pipe_hnd->cli->user_session_key, ctr);
1588 if (NT_STATUS_IS_OK(result)) {
1589 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1590 ctx->thiscmd, oldval, argv[0]);
1593 done:
1595 return result;
1598 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1599 struct rpc_sh_ctx *ctx,
1600 struct rpc_pipe_client *pipe_hnd,
1601 int argc, const char **argv)
1603 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1604 rpc_sh_user_flag_edit_internals);
1607 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1608 struct rpc_sh_ctx *ctx)
1610 static struct rpc_sh_cmd cmds[] = {
1612 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1613 "Show/Set a user's full name" },
1615 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1616 "Show/Set a user's home directory" },
1618 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1619 "Show/Set a user's home drive" },
1621 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1622 "Show/Set a user's logon script" },
1624 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1625 "Show/Set a user's profile path" },
1627 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1628 "Show/Set a user's description" },
1630 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1631 "Show/Set whether a user is disabled" },
1633 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1634 "Show/Set whether a user locked out" },
1636 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1637 "Show/Set whether a user does not need a password" },
1639 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1640 "Show/Set whether a user's password does not expire" },
1642 { NULL, NULL, 0, NULL, NULL }
1645 return cmds;
1648 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1649 struct rpc_sh_ctx *ctx)
1651 static struct rpc_sh_cmd cmds[] = {
1653 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1654 "List available users" },
1656 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1657 "List the domain groups a user is member of" },
1659 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1660 "Show info about a user" },
1662 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1663 "Show/Modify a user's fields" },
1665 { NULL, NULL, 0, NULL, NULL }
1668 return cmds;
1671 /****************************************************************************/
1674 * Basic usage function for 'net rpc group'
1675 * @param argc Standard main() style argc.
1676 * @param argv Standard main() style argv. Initial components are already
1677 * stripped.
1680 static int rpc_group_usage(int argc, const char **argv)
1682 return net_help_group(argc, argv);
1686 * Delete group on a remote RPC server
1688 * All parameters are provided by the run_rpc_command function, except for
1689 * argc, argv which are passes through.
1691 * @param domain_sid The domain sid acquired from the remote server
1692 * @param cli A cli_state connected to the server.
1693 * @param mem_ctx Talloc context, destoyed on completion of the function.
1694 * @param argc Standard main() style argc
1695 * @param argv Standard main() style argv. Initial components are already
1696 * stripped
1698 * @return Normal NTSTATUS return.
1701 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1702 const char *domain_name,
1703 struct cli_state *cli,
1704 struct rpc_pipe_client *pipe_hnd,
1705 TALLOC_CTX *mem_ctx,
1706 int argc,
1707 const char **argv)
1709 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1710 BOOL group_is_primary = False;
1711 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1713 uint32 *group_rids, num_rids, *name_types, num_members,
1714 *group_attrs, group_rid;
1715 uint32 flags = 0x000003e8; /* Unknown */
1716 /* char **names; */
1717 int i;
1718 /* DOM_GID *user_gids; */
1719 SAM_USERINFO_CTR *user_ctr;
1720 fstring temp;
1722 if (argc < 1) {
1723 d_printf("specify group\n");
1724 rpc_group_usage(argc,argv);
1725 return NT_STATUS_OK; /* ok? */
1728 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1729 &connect_pol);
1731 if (!NT_STATUS_IS_OK(result)) {
1732 d_fprintf(stderr, "Request samr_connect failed\n");
1733 goto done;
1736 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1737 MAXIMUM_ALLOWED_ACCESS,
1738 domain_sid, &domain_pol);
1740 if (!NT_STATUS_IS_OK(result)) {
1741 d_fprintf(stderr, "Request open_domain failed\n");
1742 goto done;
1745 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1746 flags, 1, &argv[0],
1747 &num_rids, &group_rids,
1748 &name_types);
1750 if (!NT_STATUS_IS_OK(result)) {
1751 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1752 goto done;
1755 switch (name_types[0])
1757 case SID_NAME_DOM_GRP:
1758 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1759 MAXIMUM_ALLOWED_ACCESS,
1760 group_rids[0], &group_pol);
1761 if (!NT_STATUS_IS_OK(result)) {
1762 d_fprintf(stderr, "Request open_group failed");
1763 goto done;
1766 group_rid = group_rids[0];
1768 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1769 &num_members, &group_rids,
1770 &group_attrs);
1772 if (!NT_STATUS_IS_OK(result)) {
1773 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1774 goto done;
1777 if (opt_verbose) {
1778 d_printf("Domain Group %s (rid: %d) has %d members\n",
1779 argv[0],group_rid,num_members);
1782 /* Check if group is anyone's primary group */
1783 for (i = 0; i < num_members; i++)
1785 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1786 MAXIMUM_ALLOWED_ACCESS,
1787 group_rids[i], &user_pol);
1789 if (!NT_STATUS_IS_OK(result)) {
1790 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1791 goto done;
1794 ZERO_STRUCT(user_ctr);
1796 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1797 21, &user_ctr);
1799 if (!NT_STATUS_IS_OK(result)) {
1800 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1801 goto done;
1804 if (user_ctr->info.id21->group_rid == group_rid) {
1805 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1806 sizeof(temp)-1);
1807 if (opt_verbose)
1808 d_printf("Group is primary group of %s\n",temp);
1809 group_is_primary = True;
1812 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1815 if (group_is_primary) {
1816 d_fprintf(stderr, "Unable to delete group because some "
1817 "of it's members have it as primary group\n");
1818 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1819 goto done;
1822 /* remove all group members */
1823 for (i = 0; i < num_members; i++)
1825 if (opt_verbose)
1826 d_printf("Remove group member %d...",group_rids[i]);
1827 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1829 if (NT_STATUS_IS_OK(result)) {
1830 if (opt_verbose)
1831 d_printf("ok\n");
1832 } else {
1833 if (opt_verbose)
1834 d_printf("failed\n");
1835 goto done;
1839 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1841 break;
1842 /* removing a local group is easier... */
1843 case SID_NAME_ALIAS:
1844 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1845 MAXIMUM_ALLOWED_ACCESS,
1846 group_rids[0], &group_pol);
1848 if (!NT_STATUS_IS_OK(result)) {
1849 d_fprintf(stderr, "Request open_alias failed\n");
1850 goto done;
1853 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1854 break;
1855 default:
1856 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1857 argv[0],sid_type_lookup(name_types[0]));
1858 result = NT_STATUS_UNSUCCESSFUL;
1859 goto done;
1863 if (NT_STATUS_IS_OK(result)) {
1864 if (opt_verbose)
1865 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1866 } else {
1867 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1868 get_friendly_nt_error_msg(result));
1871 done:
1872 return result;
1876 static int rpc_group_delete(int argc, const char **argv)
1878 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1879 argc,argv);
1882 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1883 const char *domain_name,
1884 struct cli_state *cli,
1885 struct rpc_pipe_client *pipe_hnd,
1886 TALLOC_CTX *mem_ctx,
1887 int argc,
1888 const char **argv)
1890 POLICY_HND connect_pol, domain_pol, group_pol;
1891 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1892 GROUP_INFO_CTR group_info;
1894 if (argc != 1) {
1895 d_printf("Group name must be specified\n");
1896 rpc_group_usage(argc, argv);
1897 return NT_STATUS_OK;
1900 /* Get sam policy handle */
1902 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1903 &connect_pol);
1904 if (!NT_STATUS_IS_OK(result)) goto done;
1906 /* Get domain policy handle */
1908 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1909 MAXIMUM_ALLOWED_ACCESS,
1910 domain_sid, &domain_pol);
1911 if (!NT_STATUS_IS_OK(result)) goto done;
1913 /* Create the group */
1915 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1916 argv[0], MAXIMUM_ALLOWED_ACCESS,
1917 &group_pol);
1918 if (!NT_STATUS_IS_OK(result)) goto done;
1920 if (strlen(opt_comment) == 0) goto done;
1922 /* We've got a comment to set */
1924 group_info.switch_value1 = 4;
1925 init_samr_group_info4(&group_info.group.info4, opt_comment);
1927 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1928 if (!NT_STATUS_IS_OK(result)) goto done;
1930 done:
1931 if (NT_STATUS_IS_OK(result))
1932 DEBUG(5, ("add group succeeded\n"));
1933 else
1934 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1936 return result;
1939 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1940 const char *domain_name,
1941 struct cli_state *cli,
1942 struct rpc_pipe_client *pipe_hnd,
1943 TALLOC_CTX *mem_ctx,
1944 int argc,
1945 const char **argv)
1947 POLICY_HND connect_pol, domain_pol, alias_pol;
1948 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1949 ALIAS_INFO_CTR alias_info;
1951 if (argc != 1) {
1952 d_printf("Alias name must be specified\n");
1953 rpc_group_usage(argc, argv);
1954 return NT_STATUS_OK;
1957 /* Get sam policy handle */
1959 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1960 &connect_pol);
1961 if (!NT_STATUS_IS_OK(result)) goto done;
1963 /* Get domain policy handle */
1965 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1966 MAXIMUM_ALLOWED_ACCESS,
1967 domain_sid, &domain_pol);
1968 if (!NT_STATUS_IS_OK(result)) goto done;
1970 /* Create the group */
1972 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1973 argv[0], &alias_pol);
1974 if (!NT_STATUS_IS_OK(result)) goto done;
1976 if (strlen(opt_comment) == 0) goto done;
1978 /* We've got a comment to set */
1980 alias_info.level = 3;
1981 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1983 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1984 if (!NT_STATUS_IS_OK(result)) goto done;
1986 done:
1987 if (NT_STATUS_IS_OK(result))
1988 DEBUG(5, ("add alias succeeded\n"));
1989 else
1990 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1992 return result;
1995 static int rpc_group_add(int argc, const char **argv)
1997 if (opt_localgroup)
1998 return run_rpc_command(NULL, PI_SAMR, 0,
1999 rpc_alias_add_internals,
2000 argc, argv);
2002 return run_rpc_command(NULL, PI_SAMR, 0,
2003 rpc_group_add_internals,
2004 argc, argv);
2007 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2008 TALLOC_CTX *mem_ctx,
2009 const char *name,
2010 DOM_SID *sid,
2011 enum lsa_SidType *type)
2013 DOM_SID *sids = NULL;
2014 enum lsa_SidType *types = NULL;
2015 struct rpc_pipe_client *pipe_hnd;
2016 POLICY_HND lsa_pol;
2017 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2019 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2020 if (!pipe_hnd) {
2021 goto done;
2024 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2025 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2027 if (!NT_STATUS_IS_OK(result)) {
2028 goto done;
2031 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2032 &name, NULL, &sids, &types);
2034 if (NT_STATUS_IS_OK(result)) {
2035 sid_copy(sid, &sids[0]);
2036 *type = types[0];
2039 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
2041 done:
2042 if (pipe_hnd) {
2043 cli_rpc_pipe_close(pipe_hnd);
2046 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2048 /* Try as S-1-5-whatever */
2050 DOM_SID tmp_sid;
2052 if (string_to_sid(&tmp_sid, name)) {
2053 sid_copy(sid, &tmp_sid);
2054 *type = SID_NAME_UNKNOWN;
2055 result = NT_STATUS_OK;
2059 return result;
2062 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2063 TALLOC_CTX *mem_ctx,
2064 const DOM_SID *group_sid,
2065 const char *member)
2067 POLICY_HND connect_pol, domain_pol;
2068 NTSTATUS result;
2069 uint32 group_rid;
2070 POLICY_HND group_pol;
2072 uint32 num_rids;
2073 uint32 *rids = NULL;
2074 uint32 *rid_types = NULL;
2076 DOM_SID sid;
2078 sid_copy(&sid, group_sid);
2080 if (!sid_split_rid(&sid, &group_rid)) {
2081 return NT_STATUS_UNSUCCESSFUL;
2084 /* Get sam policy handle */
2085 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2086 &connect_pol);
2087 if (!NT_STATUS_IS_OK(result)) {
2088 return result;
2091 /* Get domain policy handle */
2092 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2093 MAXIMUM_ALLOWED_ACCESS,
2094 &sid, &domain_pol);
2095 if (!NT_STATUS_IS_OK(result)) {
2096 return result;
2099 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2100 1, &member,
2101 &num_rids, &rids, &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_open_group(pipe_hnd, mem_ctx, &domain_pol,
2109 MAXIMUM_ALLOWED_ACCESS,
2110 group_rid, &group_pol);
2112 if (!NT_STATUS_IS_OK(result)) {
2113 goto done;
2116 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2118 done:
2119 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2120 return result;
2123 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2124 TALLOC_CTX *mem_ctx,
2125 const DOM_SID *alias_sid,
2126 const char *member)
2128 POLICY_HND connect_pol, domain_pol;
2129 NTSTATUS result;
2130 uint32 alias_rid;
2131 POLICY_HND alias_pol;
2133 DOM_SID member_sid;
2134 enum lsa_SidType member_type;
2136 DOM_SID sid;
2138 sid_copy(&sid, alias_sid);
2140 if (!sid_split_rid(&sid, &alias_rid)) {
2141 return NT_STATUS_UNSUCCESSFUL;
2144 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2145 &member_sid, &member_type);
2147 if (!NT_STATUS_IS_OK(result)) {
2148 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2149 return result;
2152 /* Get sam policy handle */
2153 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2154 &connect_pol);
2155 if (!NT_STATUS_IS_OK(result)) {
2156 goto done;
2159 /* Get domain policy handle */
2160 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2161 MAXIMUM_ALLOWED_ACCESS,
2162 &sid, &domain_pol);
2163 if (!NT_STATUS_IS_OK(result)) {
2164 goto done;
2167 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2168 MAXIMUM_ALLOWED_ACCESS,
2169 alias_rid, &alias_pol);
2171 if (!NT_STATUS_IS_OK(result)) {
2172 return result;
2175 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2177 if (!NT_STATUS_IS_OK(result)) {
2178 return result;
2181 done:
2182 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2183 return result;
2186 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2187 const char *domain_name,
2188 struct cli_state *cli,
2189 struct rpc_pipe_client *pipe_hnd,
2190 TALLOC_CTX *mem_ctx,
2191 int argc,
2192 const char **argv)
2194 DOM_SID group_sid;
2195 enum lsa_SidType group_type;
2197 if (argc != 2) {
2198 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2199 return NT_STATUS_UNSUCCESSFUL;
2202 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2203 &group_sid, &group_type))) {
2204 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2205 return NT_STATUS_UNSUCCESSFUL;
2208 if (group_type == SID_NAME_DOM_GRP) {
2209 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2210 &group_sid, argv[1]);
2212 if (!NT_STATUS_IS_OK(result)) {
2213 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2214 argv[1], argv[0], nt_errstr(result));
2216 return result;
2219 if (group_type == SID_NAME_ALIAS) {
2220 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2221 &group_sid, argv[1]);
2223 if (!NT_STATUS_IS_OK(result)) {
2224 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2225 argv[1], argv[0], nt_errstr(result));
2227 return result;
2230 d_fprintf(stderr, "Can only add members to global or local groups "
2231 "which %s is not\n", argv[0]);
2233 return NT_STATUS_UNSUCCESSFUL;
2236 static int rpc_group_addmem(int argc, const char **argv)
2238 return run_rpc_command(NULL, PI_SAMR, 0,
2239 rpc_group_addmem_internals,
2240 argc, argv);
2243 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2244 TALLOC_CTX *mem_ctx,
2245 const DOM_SID *group_sid,
2246 const char *member)
2248 POLICY_HND connect_pol, domain_pol;
2249 NTSTATUS result;
2250 uint32 group_rid;
2251 POLICY_HND group_pol;
2253 uint32 num_rids;
2254 uint32 *rids = NULL;
2255 uint32 *rid_types = NULL;
2257 DOM_SID sid;
2259 sid_copy(&sid, group_sid);
2261 if (!sid_split_rid(&sid, &group_rid))
2262 return NT_STATUS_UNSUCCESSFUL;
2264 /* Get sam policy handle */
2265 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2266 &connect_pol);
2267 if (!NT_STATUS_IS_OK(result))
2268 return result;
2270 /* Get domain policy handle */
2271 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2272 MAXIMUM_ALLOWED_ACCESS,
2273 &sid, &domain_pol);
2274 if (!NT_STATUS_IS_OK(result))
2275 return result;
2277 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2278 1, &member,
2279 &num_rids, &rids, &rid_types);
2281 if (!NT_STATUS_IS_OK(result)) {
2282 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2283 goto done;
2286 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2287 MAXIMUM_ALLOWED_ACCESS,
2288 group_rid, &group_pol);
2290 if (!NT_STATUS_IS_OK(result))
2291 goto done;
2293 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2295 done:
2296 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2297 return result;
2300 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2301 TALLOC_CTX *mem_ctx,
2302 const DOM_SID *alias_sid,
2303 const char *member)
2305 POLICY_HND connect_pol, domain_pol;
2306 NTSTATUS result;
2307 uint32 alias_rid;
2308 POLICY_HND alias_pol;
2310 DOM_SID member_sid;
2311 enum lsa_SidType member_type;
2313 DOM_SID sid;
2315 sid_copy(&sid, alias_sid);
2317 if (!sid_split_rid(&sid, &alias_rid))
2318 return NT_STATUS_UNSUCCESSFUL;
2320 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2321 &member_sid, &member_type);
2323 if (!NT_STATUS_IS_OK(result)) {
2324 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2325 return result;
2328 /* Get sam policy handle */
2329 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2330 &connect_pol);
2331 if (!NT_STATUS_IS_OK(result)) {
2332 goto done;
2335 /* Get domain policy handle */
2336 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2337 MAXIMUM_ALLOWED_ACCESS,
2338 &sid, &domain_pol);
2339 if (!NT_STATUS_IS_OK(result)) {
2340 goto done;
2343 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2344 MAXIMUM_ALLOWED_ACCESS,
2345 alias_rid, &alias_pol);
2347 if (!NT_STATUS_IS_OK(result))
2348 return result;
2350 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2352 if (!NT_STATUS_IS_OK(result))
2353 return result;
2355 done:
2356 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2357 return result;
2360 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2361 const char *domain_name,
2362 struct cli_state *cli,
2363 struct rpc_pipe_client *pipe_hnd,
2364 TALLOC_CTX *mem_ctx,
2365 int argc,
2366 const char **argv)
2368 DOM_SID group_sid;
2369 enum lsa_SidType group_type;
2371 if (argc != 2) {
2372 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2373 return NT_STATUS_UNSUCCESSFUL;
2376 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2377 &group_sid, &group_type))) {
2378 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2379 return NT_STATUS_UNSUCCESSFUL;
2382 if (group_type == SID_NAME_DOM_GRP) {
2383 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2384 &group_sid, argv[1]);
2386 if (!NT_STATUS_IS_OK(result)) {
2387 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2388 argv[1], argv[0], nt_errstr(result));
2390 return result;
2393 if (group_type == SID_NAME_ALIAS) {
2394 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2395 &group_sid, argv[1]);
2397 if (!NT_STATUS_IS_OK(result)) {
2398 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2399 argv[1], argv[0], nt_errstr(result));
2401 return result;
2404 d_fprintf(stderr, "Can only delete members from global or local groups "
2405 "which %s is not\n", argv[0]);
2407 return NT_STATUS_UNSUCCESSFUL;
2410 static int rpc_group_delmem(int argc, const char **argv)
2412 return run_rpc_command(NULL, PI_SAMR, 0,
2413 rpc_group_delmem_internals,
2414 argc, argv);
2417 /**
2418 * List groups on a remote RPC server
2420 * All parameters are provided by the run_rpc_command function, except for
2421 * argc, argv which are passes through.
2423 * @param domain_sid The domain sid acquired from the remote server
2424 * @param cli A cli_state connected to the server.
2425 * @param mem_ctx Talloc context, destoyed on completion of the function.
2426 * @param argc Standard main() style argc
2427 * @param argv Standard main() style argv. Initial components are already
2428 * stripped
2430 * @return Normal NTSTATUS return.
2433 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2434 const char *domain_name,
2435 struct cli_state *cli,
2436 struct rpc_pipe_client *pipe_hnd,
2437 TALLOC_CTX *mem_ctx,
2438 int argc,
2439 const char **argv)
2441 POLICY_HND connect_pol, domain_pol;
2442 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2443 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2444 struct acct_info *groups;
2445 BOOL global = False;
2446 BOOL local = False;
2447 BOOL builtin = False;
2449 if (argc == 0) {
2450 global = True;
2451 local = True;
2452 builtin = True;
2455 for (i=0; i<argc; i++) {
2456 if (strequal(argv[i], "global"))
2457 global = True;
2459 if (strequal(argv[i], "local"))
2460 local = True;
2462 if (strequal(argv[i], "builtin"))
2463 builtin = True;
2466 /* Get sam policy handle */
2468 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2469 &connect_pol);
2470 if (!NT_STATUS_IS_OK(result)) {
2471 goto done;
2474 /* Get domain policy handle */
2476 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2477 MAXIMUM_ALLOWED_ACCESS,
2478 domain_sid, &domain_pol);
2479 if (!NT_STATUS_IS_OK(result)) {
2480 goto done;
2483 /* Query domain groups */
2484 if (opt_long_list_entries)
2485 d_printf("\nGroup name Comment"\
2486 "\n-----------------------------\n");
2487 do {
2488 SAM_DISPINFO_CTR ctr;
2489 SAM_DISPINFO_3 info3;
2490 uint32 max_size;
2492 ZERO_STRUCT(ctr);
2493 ZERO_STRUCT(info3);
2494 ctr.sam.info3 = &info3;
2496 if (!global) break;
2498 get_query_dispinfo_params(
2499 loop_count, &max_entries, &max_size);
2501 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2502 &start_idx, 3, &num_entries,
2503 max_entries, max_size, &ctr);
2505 if (!NT_STATUS_IS_OK(result) &&
2506 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2507 break;
2509 for (i = 0; i < num_entries; i++) {
2511 fstring group, desc;
2513 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2514 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2516 if (opt_long_list_entries)
2517 printf("%-21.21s %-50.50s\n",
2518 group, desc);
2519 else
2520 printf("%s\n", group);
2522 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2523 /* query domain aliases */
2524 start_idx = 0;
2525 do {
2526 if (!local) break;
2528 /* The max_size field in cli_samr_enum_als_groups is more like
2529 * an account_control field with indiviual bits what to
2530 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2531 * everything. I'm too lazy (sorry) to get this through to
2532 * rpc_parse/ etc. Volker */
2534 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2535 &start_idx, 0xffff,
2536 &groups, &num_entries);
2538 if (!NT_STATUS_IS_OK(result) &&
2539 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2540 break;
2542 for (i = 0; i < num_entries; i++) {
2544 char *description = NULL;
2546 if (opt_long_list_entries) {
2548 POLICY_HND alias_pol;
2549 ALIAS_INFO_CTR ctr;
2551 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2552 &domain_pol,
2553 0x8,
2554 groups[i].rid,
2555 &alias_pol))) &&
2556 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2557 &alias_pol, 3,
2558 &ctr))) &&
2559 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2560 &alias_pol)))) {
2561 description = unistr2_tdup(mem_ctx,
2562 ctr.alias.info3.description.string);
2566 if (description != NULL) {
2567 printf("%-21.21s %-50.50s\n",
2568 groups[i].acct_name,
2569 description);
2570 } else {
2571 printf("%s\n", groups[i].acct_name);
2574 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2575 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2576 /* Get builtin policy handle */
2578 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2579 MAXIMUM_ALLOWED_ACCESS,
2580 &global_sid_Builtin, &domain_pol);
2581 if (!NT_STATUS_IS_OK(result)) {
2582 goto done;
2584 /* query builtin aliases */
2585 start_idx = 0;
2586 do {
2587 if (!builtin) break;
2589 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2590 &start_idx, max_entries,
2591 &groups, &num_entries);
2593 if (!NT_STATUS_IS_OK(result) &&
2594 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2595 break;
2597 for (i = 0; i < num_entries; i++) {
2599 char *description = NULL;
2601 if (opt_long_list_entries) {
2603 POLICY_HND alias_pol;
2604 ALIAS_INFO_CTR ctr;
2606 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2607 &domain_pol,
2608 0x8,
2609 groups[i].rid,
2610 &alias_pol))) &&
2611 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2612 &alias_pol, 3,
2613 &ctr))) &&
2614 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2615 &alias_pol)))) {
2616 description = unistr2_tdup(mem_ctx,
2617 ctr.alias.info3.description.string);
2621 if (description != NULL) {
2622 printf("%-21.21s %-50.50s\n",
2623 groups[i].acct_name,
2624 description);
2625 } else {
2626 printf("%s\n", groups[i].acct_name);
2629 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2631 done:
2632 return result;
2635 static int rpc_group_list(int argc, const char **argv)
2637 return run_rpc_command(NULL, PI_SAMR, 0,
2638 rpc_group_list_internals,
2639 argc, argv);
2642 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2643 TALLOC_CTX *mem_ctx,
2644 const char *domain_name,
2645 const DOM_SID *domain_sid,
2646 POLICY_HND *domain_pol,
2647 uint32 rid)
2649 NTSTATUS result;
2650 POLICY_HND group_pol;
2651 uint32 num_members, *group_rids, *group_attrs;
2652 uint32 num_names;
2653 char **names;
2654 uint32 *name_types;
2655 int i;
2657 fstring sid_str;
2658 sid_to_string(sid_str, domain_sid);
2660 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2661 MAXIMUM_ALLOWED_ACCESS,
2662 rid, &group_pol);
2664 if (!NT_STATUS_IS_OK(result))
2665 return result;
2667 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2668 &num_members, &group_rids,
2669 &group_attrs);
2671 if (!NT_STATUS_IS_OK(result))
2672 return result;
2674 while (num_members > 0) {
2675 int this_time = 512;
2677 if (num_members < this_time)
2678 this_time = num_members;
2680 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2681 this_time, group_rids,
2682 &num_names, &names, &name_types);
2684 if (!NT_STATUS_IS_OK(result))
2685 return result;
2687 /* We only have users as members, but make the output
2688 the same as the output of alias members */
2690 for (i = 0; i < this_time; i++) {
2692 if (opt_long_list_entries) {
2693 printf("%s-%d %s\\%s %d\n", sid_str,
2694 group_rids[i], domain_name, names[i],
2695 SID_NAME_USER);
2696 } else {
2697 printf("%s\\%s\n", domain_name, names[i]);
2701 num_members -= this_time;
2702 group_rids += 512;
2705 return NT_STATUS_OK;
2708 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2709 TALLOC_CTX *mem_ctx,
2710 POLICY_HND *domain_pol,
2711 uint32 rid)
2713 NTSTATUS result;
2714 struct rpc_pipe_client *lsa_pipe;
2715 POLICY_HND alias_pol, lsa_pol;
2716 uint32 num_members;
2717 DOM_SID *alias_sids;
2718 char **domains;
2719 char **names;
2720 enum lsa_SidType *types;
2721 int i;
2723 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2724 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2726 if (!NT_STATUS_IS_OK(result))
2727 return result;
2729 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2730 &num_members, &alias_sids);
2732 if (!NT_STATUS_IS_OK(result)) {
2733 d_fprintf(stderr, "Couldn't list alias members\n");
2734 return result;
2737 if (num_members == 0) {
2738 return NT_STATUS_OK;
2741 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2742 if (!lsa_pipe) {
2743 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2744 nt_errstr(result) );
2745 return result;
2748 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2749 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2751 if (!NT_STATUS_IS_OK(result)) {
2752 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2753 cli_rpc_pipe_close(lsa_pipe);
2754 return result;
2757 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2758 alias_sids,
2759 &domains, &names, &types);
2761 if (!NT_STATUS_IS_OK(result) &&
2762 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2763 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2764 cli_rpc_pipe_close(lsa_pipe);
2765 return result;
2768 for (i = 0; i < num_members; i++) {
2769 fstring sid_str;
2770 sid_to_string(sid_str, &alias_sids[i]);
2772 if (opt_long_list_entries) {
2773 printf("%s %s\\%s %d\n", sid_str,
2774 domains[i] ? domains[i] : "*unknown*",
2775 names[i] ? names[i] : "*unknown*", types[i]);
2776 } else {
2777 if (domains[i])
2778 printf("%s\\%s\n", domains[i], names[i]);
2779 else
2780 printf("%s\n", sid_str);
2784 cli_rpc_pipe_close(lsa_pipe);
2785 return NT_STATUS_OK;
2788 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2789 const char *domain_name,
2790 struct cli_state *cli,
2791 struct rpc_pipe_client *pipe_hnd,
2792 TALLOC_CTX *mem_ctx,
2793 int argc,
2794 const char **argv)
2796 NTSTATUS result;
2797 POLICY_HND connect_pol, domain_pol;
2798 uint32 num_rids, *rids, *rid_types;
2800 /* Get sam policy handle */
2802 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2803 &connect_pol);
2805 if (!NT_STATUS_IS_OK(result))
2806 return result;
2808 /* Get domain policy handle */
2810 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2811 MAXIMUM_ALLOWED_ACCESS,
2812 domain_sid, &domain_pol);
2814 if (!NT_STATUS_IS_OK(result))
2815 return result;
2817 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2818 1, argv, &num_rids, &rids, &rid_types);
2820 if (!NT_STATUS_IS_OK(result)) {
2822 /* Ok, did not find it in the global sam, try with builtin */
2824 DOM_SID sid_Builtin;
2826 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2828 string_to_sid(&sid_Builtin, "S-1-5-32");
2830 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2831 MAXIMUM_ALLOWED_ACCESS,
2832 &sid_Builtin, &domain_pol);
2834 if (!NT_STATUS_IS_OK(result)) {
2835 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2836 return result;
2839 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2840 1, argv, &num_rids,
2841 &rids, &rid_types);
2843 if (!NT_STATUS_IS_OK(result)) {
2844 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2845 return result;
2849 if (num_rids != 1) {
2850 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2851 return result;
2854 if (rid_types[0] == SID_NAME_DOM_GRP) {
2855 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2856 domain_sid, &domain_pol,
2857 rids[0]);
2860 if (rid_types[0] == SID_NAME_ALIAS) {
2861 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2862 rids[0]);
2865 return NT_STATUS_NO_SUCH_GROUP;
2868 static int rpc_group_members(int argc, const char **argv)
2870 if (argc != 1) {
2871 return rpc_group_usage(argc, argv);
2874 return run_rpc_command(NULL, PI_SAMR, 0,
2875 rpc_group_members_internals,
2876 argc, argv);
2879 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2880 const char *domain_name,
2881 struct cli_state *cli,
2882 struct rpc_pipe_client *pipe_hnd,
2883 TALLOC_CTX *mem_ctx,
2884 int argc,
2885 const char **argv)
2887 NTSTATUS result;
2888 POLICY_HND connect_pol, domain_pol, group_pol;
2889 uint32 num_rids, *rids, *rid_types;
2890 GROUP_INFO_CTR ctr;
2892 if (argc != 2) {
2893 d_printf("Usage: 'net rpc group rename group newname'\n");
2894 return NT_STATUS_UNSUCCESSFUL;
2897 /* Get sam policy handle */
2899 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2900 &connect_pol);
2902 if (!NT_STATUS_IS_OK(result))
2903 return result;
2905 /* Get domain policy handle */
2907 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2908 MAXIMUM_ALLOWED_ACCESS,
2909 domain_sid, &domain_pol);
2911 if (!NT_STATUS_IS_OK(result))
2912 return result;
2914 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2915 1, argv, &num_rids, &rids, &rid_types);
2917 if (num_rids != 1) {
2918 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2919 return result;
2922 if (rid_types[0] != SID_NAME_DOM_GRP) {
2923 d_fprintf(stderr, "Can only rename domain groups\n");
2924 return NT_STATUS_UNSUCCESSFUL;
2927 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2928 MAXIMUM_ALLOWED_ACCESS,
2929 rids[0], &group_pol);
2931 if (!NT_STATUS_IS_OK(result))
2932 return result;
2934 ZERO_STRUCT(ctr);
2936 ctr.switch_value1 = 2;
2937 init_samr_group_info2(&ctr.group.info2, argv[1]);
2939 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2941 if (!NT_STATUS_IS_OK(result))
2942 return result;
2944 return NT_STATUS_NO_SUCH_GROUP;
2947 static int rpc_group_rename(int argc, const char **argv)
2949 if (argc != 2) {
2950 return rpc_group_usage(argc, argv);
2953 return run_rpc_command(NULL, PI_SAMR, 0,
2954 rpc_group_rename_internals,
2955 argc, argv);
2958 /**
2959 * 'net rpc group' entrypoint.
2960 * @param argc Standard main() style argc
2961 * @param argc Standard main() style argv. Initial components are already
2962 * stripped
2965 int net_rpc_group(int argc, const char **argv)
2967 struct functable func[] = {
2968 {"add", rpc_group_add},
2969 {"delete", rpc_group_delete},
2970 {"addmem", rpc_group_addmem},
2971 {"delmem", rpc_group_delmem},
2972 {"list", rpc_group_list},
2973 {"members", rpc_group_members},
2974 {"rename", rpc_group_rename},
2975 {NULL, NULL}
2978 if (argc == 0) {
2979 return run_rpc_command(NULL, PI_SAMR, 0,
2980 rpc_group_list_internals,
2981 argc, argv);
2984 return net_run_function(argc, argv, func, rpc_group_usage);
2987 /****************************************************************************/
2989 static int rpc_share_usage(int argc, const char **argv)
2991 return net_help_share(argc, argv);
2994 /**
2995 * Add a share on a remote RPC server
2997 * All parameters are provided by the run_rpc_command function, except for
2998 * argc, argv which are passes through.
3000 * @param domain_sid The domain sid acquired from the remote server
3001 * @param cli A cli_state connected to the server.
3002 * @param mem_ctx Talloc context, destoyed on completion of the function.
3003 * @param argc Standard main() style argc
3004 * @param argv Standard main() style argv. Initial components are already
3005 * stripped
3007 * @return Normal NTSTATUS return.
3009 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3010 const char *domain_name,
3011 struct cli_state *cli,
3012 struct rpc_pipe_client *pipe_hnd,
3013 TALLOC_CTX *mem_ctx,int argc,
3014 const char **argv)
3016 WERROR result;
3017 char *sharename;
3018 char *path;
3019 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3020 uint32 num_users=0, perms=0;
3021 char *password=NULL; /* don't allow a share password */
3022 uint32 level = 2;
3024 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3025 return NT_STATUS_NO_MEMORY;
3028 path = strchr(sharename, '=');
3029 if (!path)
3030 return NT_STATUS_UNSUCCESSFUL;
3031 *path++ = '\0';
3033 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
3034 opt_comment, perms, opt_maxusers,
3035 num_users, path, password,
3036 level, NULL);
3037 return werror_to_ntstatus(result);
3040 static int rpc_share_add(int argc, const char **argv)
3042 if ((argc < 1) || !strchr(argv[0], '=')) {
3043 DEBUG(1,("Sharename or path not specified on add\n"));
3044 return rpc_share_usage(argc, argv);
3046 return run_rpc_command(NULL, PI_SRVSVC, 0,
3047 rpc_share_add_internals,
3048 argc, argv);
3051 /**
3052 * Delete a share on a remote RPC server
3054 * All parameters are provided by the run_rpc_command function, except for
3055 * argc, argv which are passes through.
3057 * @param domain_sid The domain sid acquired from the remote server
3058 * @param cli A cli_state connected to the server.
3059 * @param mem_ctx Talloc context, destoyed on completion of the function.
3060 * @param argc Standard main() style argc
3061 * @param argv Standard main() style argv. Initial components are already
3062 * stripped
3064 * @return Normal NTSTATUS return.
3066 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3067 const char *domain_name,
3068 struct cli_state *cli,
3069 struct rpc_pipe_client *pipe_hnd,
3070 TALLOC_CTX *mem_ctx,
3071 int argc,
3072 const char **argv)
3074 WERROR result;
3076 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
3077 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3080 /**
3081 * Delete a share on a remote RPC server
3083 * @param domain_sid The domain sid acquired from the remote server
3084 * @param argc Standard main() style argc
3085 * @param argv Standard main() style argv. Initial components are already
3086 * stripped
3088 * @return A shell status integer (0 for success)
3090 static int rpc_share_delete(int argc, const char **argv)
3092 if (argc < 1) {
3093 DEBUG(1,("Sharename not specified on delete\n"));
3094 return rpc_share_usage(argc, argv);
3096 return run_rpc_command(NULL, PI_SRVSVC, 0,
3097 rpc_share_del_internals,
3098 argc, argv);
3102 * Formatted print of share info
3104 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3107 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3109 fstring netname = "", remark = "";
3111 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3112 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3114 if (opt_long_list_entries) {
3115 d_printf("%-12s %-8.8s %-50s\n",
3116 netname, share_type[info1->info_1.type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)], remark);
3117 } else {
3118 d_printf("%s\n", netname);
3123 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3124 TALLOC_CTX *mem_ctx,
3125 uint32 level,
3126 int argc,
3127 const char **argv,
3128 SRV_SHARE_INFO_CTR *ctr)
3130 WERROR result;
3131 SRV_SHARE_INFO info;
3133 /* no specific share requested, enumerate all */
3134 if (argc == 0) {
3136 ENUM_HND hnd;
3137 uint32 preferred_len = 0xffffffff;
3139 init_enum_hnd(&hnd, 0);
3141 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3142 preferred_len, &hnd);
3145 /* request just one share */
3146 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3148 if (!W_ERROR_IS_OK(result))
3149 goto done;
3151 /* construct ctr */
3152 ZERO_STRUCTP(ctr);
3154 ctr->info_level = ctr->switch_value = level;
3155 ctr->ptr_share_info = ctr->ptr_entries = 1;
3156 ctr->num_entries = ctr->num_entries2 = 1;
3158 switch (level) {
3159 case 1:
3161 char *s;
3162 SRV_SHARE_INFO_1 *info1;
3164 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3165 if (ctr->share.info1 == NULL) {
3166 result = WERR_NOMEM;
3167 goto done;
3169 info1 = ctr->share.info1;
3171 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3173 /* Copy pointer crap */
3175 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3177 /* Duplicate strings */
3179 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3180 if (s)
3181 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3183 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3184 if (s)
3185 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3187 case 2:
3189 char *s;
3190 SRV_SHARE_INFO_2 *info2;
3192 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3193 if (ctr->share.info2 == NULL) {
3194 result = WERR_NOMEM;
3195 goto done;
3197 info2 = ctr->share.info2;
3199 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3201 /* Copy pointer crap */
3203 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3205 /* Duplicate strings */
3207 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3208 if (s)
3209 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3211 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3212 if (s)
3213 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3215 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_path);
3216 if (s)
3217 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3219 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3220 if (s)
3221 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3223 case 502:
3225 char *s;
3226 SRV_SHARE_INFO_502 *info502;
3228 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3229 if (ctr->share.info502 == NULL) {
3230 result = WERR_NOMEM;
3231 goto done;
3233 info502 = ctr->share.info502;
3235 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3237 /* Copy pointer crap */
3239 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3241 /* Duplicate strings */
3243 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3244 if (s)
3245 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3247 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3248 if (s)
3249 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3251 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_path);
3252 if (s)
3253 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3255 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3256 if (s)
3257 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3259 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3263 } /* switch */
3265 done:
3266 return result;
3269 /**
3270 * List shares on a remote RPC server
3272 * All parameters are provided by the run_rpc_command function, except for
3273 * argc, argv which are passes through.
3275 * @param domain_sid The domain sid acquired from the remote server
3276 * @param cli A cli_state connected to the server.
3277 * @param mem_ctx Talloc context, destoyed on completion of the function.
3278 * @param argc Standard main() style argc
3279 * @param argv Standard main() style argv. Initial components are already
3280 * stripped
3282 * @return Normal NTSTATUS return.
3285 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3286 const char *domain_name,
3287 struct cli_state *cli,
3288 struct rpc_pipe_client *pipe_hnd,
3289 TALLOC_CTX *mem_ctx,
3290 int argc,
3291 const char **argv)
3293 SRV_SHARE_INFO_CTR ctr;
3294 WERROR result;
3295 uint32 i, level = 1;
3297 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3298 if (!W_ERROR_IS_OK(result))
3299 goto done;
3301 /* Display results */
3303 if (opt_long_list_entries) {
3304 d_printf(
3305 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3306 "\nShare name Type Description\n"\
3307 "---------- ---- -----------\n");
3309 for (i = 0; i < ctr.num_entries; i++)
3310 display_share_info_1(&ctr.share.info1[i]);
3311 done:
3312 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3315 /***
3316 * 'net rpc share list' entrypoint.
3317 * @param argc Standard main() style argc
3318 * @param argv Standard main() style argv. Initial components are already
3319 * stripped
3321 static int rpc_share_list(int argc, const char **argv)
3323 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3326 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3328 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3329 d_printf("skipping [%s]: not a file share.\n", netname);
3330 return False;
3333 if (!cli_tdis(cli))
3334 return False;
3336 return True;
3339 static BOOL check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3341 /* only support disk shares */
3342 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3343 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3344 return False;
3347 /* skip builtin shares */
3348 /* FIXME: should print$ be added too ? */
3349 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3350 strequal(netname,"global"))
3351 return False;
3353 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3354 printf("excluding [%s]\n", netname);
3355 return False;
3358 return check_share_availability(cli, netname);
3361 /**
3362 * Migrate shares from a remote RPC server to the local RPC srever
3364 * All parameters are provided by the run_rpc_command function, except for
3365 * argc, argv which are passes through.
3367 * @param domain_sid The domain sid acquired from the remote server
3368 * @param cli A cli_state connected to the server.
3369 * @param mem_ctx Talloc context, destoyed on completion of the function.
3370 * @param argc Standard main() style argc
3371 * @param argv Standard main() style argv. Initial components are already
3372 * stripped
3374 * @return Normal NTSTATUS return.
3377 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3378 const char *domain_name,
3379 struct cli_state *cli,
3380 struct rpc_pipe_client *pipe_hnd,
3381 TALLOC_CTX *mem_ctx,
3382 int argc,
3383 const char **argv)
3385 WERROR result;
3386 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3387 SRV_SHARE_INFO_CTR ctr_src;
3388 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3389 char *password = NULL; /* don't allow a share password */
3390 uint32 i;
3391 struct rpc_pipe_client *srvsvc_pipe = NULL;
3392 struct cli_state *cli_dst = NULL;
3393 uint32 level = 502; /* includes secdesc */
3395 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3396 if (!W_ERROR_IS_OK(result))
3397 goto done;
3399 /* connect destination PI_SRVSVC */
3400 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3401 if (!NT_STATUS_IS_OK(nt_status))
3402 return nt_status;
3405 for (i = 0; i < ctr_src.num_entries; i++) {
3407 fstring netname = "", remark = "", path = "";
3408 /* reset error-code */
3409 nt_status = NT_STATUS_UNSUCCESSFUL;
3411 rpcstr_pull_unistr2_fstring(
3412 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3413 rpcstr_pull_unistr2_fstring(
3414 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3415 rpcstr_pull_unistr2_fstring(
3416 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3418 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3419 continue;
3421 /* finally add the share on the dst server */
3423 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3424 netname, path, remark);
3426 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3427 ctr_src.share.info502[i].info_502.perms,
3428 ctr_src.share.info502[i].info_502.max_uses,
3429 ctr_src.share.info502[i].info_502.num_uses,
3430 path, password, level,
3431 NULL);
3433 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3434 printf(" [%s] does already exist\n", netname);
3435 continue;
3438 if (!W_ERROR_IS_OK(result)) {
3439 printf("cannot add share: %s\n", dos_errstr(result));
3440 goto done;
3445 nt_status = NT_STATUS_OK;
3447 done:
3448 if (cli_dst) {
3449 cli_shutdown(cli_dst);
3452 return nt_status;
3456 /**
3457 * Migrate shares from a rpc-server to another
3459 * @param argc Standard main() style argc
3460 * @param argv Standard main() style argv. Initial components are already
3461 * stripped
3463 * @return A shell status integer (0 for success)
3465 static int rpc_share_migrate_shares(int argc, const char **argv)
3468 if (!opt_host) {
3469 printf("no server to migrate\n");
3470 return -1;
3473 return run_rpc_command(NULL, PI_SRVSVC, 0,
3474 rpc_share_migrate_shares_internals,
3475 argc, argv);
3479 * Copy a file/dir
3481 * @param f file_info
3482 * @param mask current search mask
3483 * @param state arg-pointer
3486 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3488 static NTSTATUS nt_status;
3489 static struct copy_clistate *local_state;
3490 static fstring filename, new_mask;
3491 fstring dir;
3492 char *old_dir;
3494 local_state = (struct copy_clistate *)state;
3495 nt_status = NT_STATUS_UNSUCCESSFUL;
3497 if (strequal(f->name, ".") || strequal(f->name, ".."))
3498 return;
3500 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3502 /* DIRECTORY */
3503 if (f->mode & aDIR) {
3505 DEBUG(3,("got dir: %s\n", f->name));
3507 fstrcpy(dir, local_state->cwd);
3508 fstrcat(dir, "\\");
3509 fstrcat(dir, f->name);
3511 switch (net_mode_share)
3513 case NET_MODE_SHARE_MIGRATE:
3514 /* create that directory */
3515 nt_status = net_copy_file(local_state->mem_ctx,
3516 local_state->cli_share_src,
3517 local_state->cli_share_dst,
3518 dir, dir,
3519 opt_acls? True : False,
3520 opt_attrs? True : False,
3521 opt_timestamps? True : False,
3522 False);
3523 break;
3524 default:
3525 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3526 return;
3529 if (!NT_STATUS_IS_OK(nt_status))
3530 printf("could not handle dir %s: %s\n",
3531 dir, nt_errstr(nt_status));
3533 /* search below that directory */
3534 fstrcpy(new_mask, dir);
3535 fstrcat(new_mask, "\\*");
3537 old_dir = local_state->cwd;
3538 local_state->cwd = dir;
3539 if (!sync_files(local_state, new_mask))
3540 printf("could not handle files\n");
3541 local_state->cwd = old_dir;
3543 return;
3547 /* FILE */
3548 fstrcpy(filename, local_state->cwd);
3549 fstrcat(filename, "\\");
3550 fstrcat(filename, f->name);
3552 DEBUG(3,("got file: %s\n", filename));
3554 switch (net_mode_share)
3556 case NET_MODE_SHARE_MIGRATE:
3557 nt_status = net_copy_file(local_state->mem_ctx,
3558 local_state->cli_share_src,
3559 local_state->cli_share_dst,
3560 filename, filename,
3561 opt_acls? True : False,
3562 opt_attrs? True : False,
3563 opt_timestamps? True: False,
3564 True);
3565 break;
3566 default:
3567 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3568 return;
3571 if (!NT_STATUS_IS_OK(nt_status))
3572 printf("could not handle file %s: %s\n",
3573 filename, nt_errstr(nt_status));
3578 * sync files, can be called recursivly to list files
3579 * and then call copy_fn for each file
3581 * @param cp_clistate pointer to the copy_clistate we work with
3582 * @param mask the current search mask
3584 * @return Boolean result
3586 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3588 struct cli_state *targetcli;
3589 pstring targetpath;
3591 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3593 if ( !cli_resolve_path( "", cp_clistate->cli_share_src, mask, &targetcli, targetpath ) ) {
3594 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3595 mask, cli_errstr(cp_clistate->cli_share_src));
3596 return False;
3599 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3600 d_fprintf(stderr, "listing %s failed with error: %s\n",
3601 mask, cli_errstr(targetcli));
3602 return False;
3605 return True;
3610 * Set the top level directory permissions before we do any further copies.
3611 * Should set up ACL inheritance.
3614 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3615 const char *sharename)
3617 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3619 switch (net_mode_share) {
3620 case NET_MODE_SHARE_MIGRATE:
3621 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3622 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3623 cp_clistate->cli_share_src,
3624 cp_clistate->cli_share_dst,
3625 "\\", "\\",
3626 opt_acls? True : False,
3627 opt_attrs? True : False,
3628 opt_timestamps? True: False,
3629 False);
3630 break;
3631 default:
3632 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3633 break;
3636 if (!NT_STATUS_IS_OK(nt_status)) {
3637 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3638 sharename, nt_errstr(nt_status));
3639 return False;
3642 return True;
3645 /**
3646 * Sync all files inside a remote share to another share (over smb)
3648 * All parameters are provided by the run_rpc_command function, except for
3649 * argc, argv which are passes through.
3651 * @param domain_sid The domain sid acquired from the remote server
3652 * @param cli A cli_state connected to the server.
3653 * @param mem_ctx Talloc context, destoyed on completion of the function.
3654 * @param argc Standard main() style argc
3655 * @param argv Standard main() style argv. Initial components are already
3656 * stripped
3658 * @return Normal NTSTATUS return.
3661 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3662 const char *domain_name,
3663 struct cli_state *cli,
3664 struct rpc_pipe_client *pipe_hnd,
3665 TALLOC_CTX *mem_ctx,
3666 int argc,
3667 const char **argv)
3669 WERROR result;
3670 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3671 SRV_SHARE_INFO_CTR ctr_src;
3672 uint32 i;
3673 uint32 level = 502;
3674 struct copy_clistate cp_clistate;
3675 BOOL got_src_share = False;
3676 BOOL got_dst_share = False;
3677 pstring mask = "\\*";
3678 char *dst = NULL;
3680 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3682 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3684 if (!W_ERROR_IS_OK(result))
3685 goto done;
3687 for (i = 0; i < ctr_src.num_entries; i++) {
3689 fstring netname = "";
3691 rpcstr_pull_unistr2_fstring(
3692 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3694 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3695 continue;
3697 /* one might not want to mirror whole discs :) */
3698 if (strequal(netname, "print$") || netname[1] == '$') {
3699 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3700 continue;
3703 switch (net_mode_share)
3705 case NET_MODE_SHARE_MIGRATE:
3706 printf("syncing");
3707 break;
3708 default:
3709 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3710 break;
3712 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3713 netname,
3714 opt_acls ? "including" : "without",
3715 opt_attrs ? "including" : "without",
3716 opt_timestamps ? "(preserving timestamps)" : "");
3718 cp_clistate.mem_ctx = mem_ctx;
3719 cp_clistate.cli_share_src = NULL;
3720 cp_clistate.cli_share_dst = NULL;
3721 cp_clistate.cwd = NULL;
3722 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3724 /* open share source */
3725 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3726 &cli->dest_ip, cli->desthost,
3727 netname, "A:");
3728 if (!NT_STATUS_IS_OK(nt_status))
3729 goto done;
3731 got_src_share = True;
3733 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3734 /* open share destination */
3735 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3736 NULL, dst, netname, "A:");
3737 if (!NT_STATUS_IS_OK(nt_status))
3738 goto done;
3740 got_dst_share = True;
3743 if (!copy_top_level_perms(&cp_clistate, netname)) {
3744 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3745 nt_status = NT_STATUS_UNSUCCESSFUL;
3746 goto done;
3749 if (!sync_files(&cp_clistate, mask)) {
3750 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3751 nt_status = NT_STATUS_UNSUCCESSFUL;
3752 goto done;
3756 nt_status = NT_STATUS_OK;
3758 done:
3760 if (got_src_share)
3761 cli_shutdown(cp_clistate.cli_share_src);
3763 if (got_dst_share)
3764 cli_shutdown(cp_clistate.cli_share_dst);
3766 return nt_status;
3770 static int rpc_share_migrate_files(int argc, const char **argv)
3773 if (!opt_host) {
3774 printf("no server to migrate\n");
3775 return -1;
3778 return run_rpc_command(NULL, PI_SRVSVC, 0,
3779 rpc_share_migrate_files_internals,
3780 argc, argv);
3783 /**
3784 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3786 * All parameters are provided by the run_rpc_command function, except for
3787 * argc, argv which are passes through.
3789 * @param domain_sid The domain sid acquired from the remote server
3790 * @param cli A cli_state connected to the server.
3791 * @param mem_ctx Talloc context, destoyed on completion of the function.
3792 * @param argc Standard main() style argc
3793 * @param argv Standard main() style argv. Initial components are already
3794 * stripped
3796 * @return Normal NTSTATUS return.
3799 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3800 const char *domain_name,
3801 struct cli_state *cli,
3802 struct rpc_pipe_client *pipe_hnd,
3803 TALLOC_CTX *mem_ctx,
3804 int argc,
3805 const char **argv)
3807 WERROR result;
3808 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3809 SRV_SHARE_INFO_CTR ctr_src;
3810 SRV_SHARE_INFO info;
3811 uint32 i;
3812 struct rpc_pipe_client *srvsvc_pipe = NULL;
3813 struct cli_state *cli_dst = NULL;
3814 uint32 level = 502; /* includes secdesc */
3816 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3818 if (!W_ERROR_IS_OK(result))
3819 goto done;
3821 /* connect destination PI_SRVSVC */
3822 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3823 if (!NT_STATUS_IS_OK(nt_status))
3824 return nt_status;
3827 for (i = 0; i < ctr_src.num_entries; i++) {
3829 fstring netname = "", remark = "", path = "";
3830 /* reset error-code */
3831 nt_status = NT_STATUS_UNSUCCESSFUL;
3833 rpcstr_pull_unistr2_fstring(
3834 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3835 rpcstr_pull_unistr2_fstring(
3836 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3837 rpcstr_pull_unistr2_fstring(
3838 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3840 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3841 continue;
3843 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3844 netname, path, remark);
3846 if (opt_verbose)
3847 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
3849 /* init info */
3850 ZERO_STRUCT(info);
3852 info.switch_value = level;
3853 info.ptr_share_ctr = 1;
3855 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3856 info.share.info502 = ctr_src.share.info502[i];
3858 /* finally modify the share on the dst server */
3859 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
3861 if (!W_ERROR_IS_OK(result)) {
3862 printf("cannot set share-acl: %s\n", dos_errstr(result));
3863 goto done;
3868 nt_status = NT_STATUS_OK;
3870 done:
3871 if (cli_dst) {
3872 cli_shutdown(cli_dst);
3875 return nt_status;
3879 /**
3880 * Migrate share-acls from a rpc-server to another
3882 * @param argc Standard main() style argc
3883 * @param argv Standard main() style argv. Initial components are already
3884 * stripped
3886 * @return A shell status integer (0 for success)
3888 static int rpc_share_migrate_security(int argc, const char **argv)
3891 if (!opt_host) {
3892 printf("no server to migrate\n");
3893 return -1;
3896 return run_rpc_command(NULL, PI_SRVSVC, 0,
3897 rpc_share_migrate_security_internals,
3898 argc, argv);
3901 /**
3902 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3903 * from one server to another
3905 * @param argc Standard main() style argc
3906 * @param argv Standard main() style argv. Initial components are already
3907 * stripped
3909 * @return A shell status integer (0 for success)
3912 static int rpc_share_migrate_all(int argc, const char **argv)
3914 int ret;
3916 if (!opt_host) {
3917 printf("no server to migrate\n");
3918 return -1;
3921 /* order is important. we don't want to be locked out by the share-acl
3922 * before copying files - gd */
3924 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3925 if (ret)
3926 return ret;
3928 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3929 if (ret)
3930 return ret;
3932 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3936 /**
3937 * 'net rpc share migrate' entrypoint.
3938 * @param argc Standard main() style argc
3939 * @param argv Standard main() style argv. Initial components are already
3940 * stripped
3942 static int rpc_share_migrate(int argc, const char **argv)
3945 struct functable func[] = {
3946 {"all", rpc_share_migrate_all},
3947 {"files", rpc_share_migrate_files},
3948 {"help", rpc_share_usage},
3949 {"security", rpc_share_migrate_security},
3950 {"shares", rpc_share_migrate_shares},
3951 {NULL, NULL}
3954 net_mode_share = NET_MODE_SHARE_MIGRATE;
3956 return net_run_function(argc, argv, func, rpc_share_usage);
3959 struct full_alias {
3960 DOM_SID sid;
3961 uint32 num_members;
3962 DOM_SID *members;
3965 static int num_server_aliases;
3966 static struct full_alias *server_aliases;
3969 * Add an alias to the static list.
3971 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3973 if (server_aliases == NULL)
3974 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3976 server_aliases[num_server_aliases] = *alias;
3977 num_server_aliases += 1;
3981 * For a specific domain on the server, fetch all the aliases
3982 * and their members. Add all of them to the server_aliases.
3985 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3986 TALLOC_CTX *mem_ctx,
3987 POLICY_HND *connect_pol,
3988 const DOM_SID *domain_sid)
3990 uint32 start_idx, max_entries, num_entries, i;
3991 struct acct_info *groups;
3992 NTSTATUS result;
3993 POLICY_HND domain_pol;
3995 /* Get domain policy handle */
3997 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3998 MAXIMUM_ALLOWED_ACCESS,
3999 domain_sid, &domain_pol);
4000 if (!NT_STATUS_IS_OK(result))
4001 return result;
4003 start_idx = 0;
4004 max_entries = 250;
4006 do {
4007 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
4008 &start_idx, max_entries,
4009 &groups, &num_entries);
4011 for (i = 0; i < num_entries; i++) {
4013 POLICY_HND alias_pol;
4014 struct full_alias alias;
4015 DOM_SID *members;
4016 int j;
4018 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
4019 MAXIMUM_ALLOWED_ACCESS,
4020 groups[i].rid,
4021 &alias_pol);
4022 if (!NT_STATUS_IS_OK(result))
4023 goto done;
4025 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
4026 &alias_pol,
4027 &alias.num_members,
4028 &members);
4029 if (!NT_STATUS_IS_OK(result))
4030 goto done;
4032 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
4033 if (!NT_STATUS_IS_OK(result))
4034 goto done;
4036 alias.members = NULL;
4038 if (alias.num_members > 0) {
4039 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4041 for (j = 0; j < alias.num_members; j++)
4042 sid_copy(&alias.members[j],
4043 &members[j]);
4046 sid_copy(&alias.sid, domain_sid);
4047 sid_append_rid(&alias.sid, groups[i].rid);
4049 push_alias(mem_ctx, &alias);
4051 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4053 result = NT_STATUS_OK;
4055 done:
4056 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
4058 return result;
4062 * Dump server_aliases as names for debugging purposes.
4065 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4066 const char *domain_name,
4067 struct cli_state *cli,
4068 struct rpc_pipe_client *pipe_hnd,
4069 TALLOC_CTX *mem_ctx,
4070 int argc,
4071 const char **argv)
4073 int i;
4074 NTSTATUS result;
4075 POLICY_HND lsa_pol;
4077 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4078 SEC_RIGHTS_MAXIMUM_ALLOWED,
4079 &lsa_pol);
4080 if (!NT_STATUS_IS_OK(result))
4081 return result;
4083 for (i=0; i<num_server_aliases; i++) {
4084 char **names;
4085 char **domains;
4086 uint32 *types;
4087 int j;
4089 struct full_alias *alias = &server_aliases[i];
4091 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4092 &alias->sid,
4093 &domains, &names, &types);
4094 if (!NT_STATUS_IS_OK(result))
4095 continue;
4097 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4099 if (alias->num_members == 0) {
4100 DEBUG(1, ("\n"));
4101 continue;
4104 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4105 alias->num_members,
4106 alias->members,
4107 &domains, &names, &types);
4109 if (!NT_STATUS_IS_OK(result) &&
4110 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4111 continue;
4113 for (j=0; j<alias->num_members; j++)
4114 DEBUG(1, ("%s\\%s (%d); ",
4115 domains[j] ? domains[j] : "*unknown*",
4116 names[j] ? names[j] : "*unknown*",types[j]));
4117 DEBUG(1, ("\n"));
4120 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
4122 return NT_STATUS_OK;
4126 * Fetch a list of all server aliases and their members into
4127 * server_aliases.
4130 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4131 const char *domain_name,
4132 struct cli_state *cli,
4133 struct rpc_pipe_client *pipe_hnd,
4134 TALLOC_CTX *mem_ctx,
4135 int argc,
4136 const char **argv)
4138 NTSTATUS result;
4139 POLICY_HND connect_pol;
4141 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4142 &connect_pol);
4144 if (!NT_STATUS_IS_OK(result))
4145 goto done;
4147 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4148 &global_sid_Builtin);
4150 if (!NT_STATUS_IS_OK(result))
4151 goto done;
4153 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4154 domain_sid);
4156 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4157 done:
4158 return result;
4161 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4163 token->num_sids = 4;
4165 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4167 token->user_sids[0] = *user_sid;
4168 sid_copy(&token->user_sids[1], &global_sid_World);
4169 sid_copy(&token->user_sids[2], &global_sid_Network);
4170 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4173 static void free_user_token(NT_USER_TOKEN *token)
4175 SAFE_FREE(token->user_sids);
4178 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4180 int i;
4182 for (i=0; i<token->num_sids; i++) {
4183 if (sid_compare(sid, &token->user_sids[i]) == 0)
4184 return True;
4186 return False;
4189 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4191 if (is_sid_in_token(token, sid))
4192 return;
4194 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4195 if (!token->user_sids) {
4196 return;
4199 sid_copy(&token->user_sids[token->num_sids], sid);
4201 token->num_sids += 1;
4204 struct user_token {
4205 fstring name;
4206 NT_USER_TOKEN token;
4209 static void dump_user_token(struct user_token *token)
4211 int i;
4213 d_printf("%s\n", token->name);
4215 for (i=0; i<token->token.num_sids; i++) {
4216 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4220 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4222 int i;
4224 for (i=0; i<alias->num_members; i++) {
4225 if (sid_compare(sid, &alias->members[i]) == 0)
4226 return True;
4229 return False;
4232 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4234 int i;
4236 for (i=0; i<num_server_aliases; i++) {
4237 if (is_alias_member(&sid, &server_aliases[i]))
4238 add_sid_to_token(token, &server_aliases[i].sid);
4243 * We got a user token with all the SIDs we can know about without asking the
4244 * server directly. These are the user and domain group sids. All of these can
4245 * be members of aliases. So scan the list of aliases for each of the SIDs and
4246 * add them to the token.
4249 static void collect_alias_memberships(NT_USER_TOKEN *token)
4251 int num_global_sids = token->num_sids;
4252 int i;
4254 for (i=0; i<num_global_sids; i++) {
4255 collect_sid_memberships(token, token->user_sids[i]);
4259 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4261 struct winbindd_request request;
4262 struct winbindd_response response;
4263 fstring full_name;
4264 NSS_STATUS result;
4266 DOM_SID user_sid;
4268 int i;
4270 fstr_sprintf(full_name, "%s%c%s",
4271 domain, *lp_winbind_separator(), user);
4273 /* First let's find out the user sid */
4275 ZERO_STRUCT(request);
4276 ZERO_STRUCT(response);
4278 fstrcpy(request.data.name.dom_name, domain);
4279 fstrcpy(request.data.name.name, user);
4281 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4283 if (result != NSS_STATUS_SUCCESS) {
4284 DEBUG(1, ("winbind could not find %s\n", full_name));
4285 return False;
4288 if (response.data.sid.type != SID_NAME_USER) {
4289 DEBUG(1, ("%s is not a user\n", full_name));
4290 return False;
4293 string_to_sid(&user_sid, response.data.sid.sid);
4295 init_user_token(token, &user_sid);
4297 /* And now the groups winbind knows about */
4299 ZERO_STRUCT(response);
4301 fstrcpy(request.data.username, full_name);
4303 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4305 if (result != NSS_STATUS_SUCCESS) {
4306 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4307 return False;
4310 for (i = 0; i < response.data.num_entries; i++) {
4311 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4312 DOM_SID sid;
4314 struct winbindd_request sidrequest;
4315 struct winbindd_response sidresponse;
4317 ZERO_STRUCT(sidrequest);
4318 ZERO_STRUCT(sidresponse);
4320 sidrequest.data.gid = gid;
4322 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4323 &sidrequest, &sidresponse);
4325 if (result != NSS_STATUS_SUCCESS) {
4326 DEBUG(1, ("winbind could not find SID of gid %d\n",
4327 gid));
4328 return False;
4331 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4333 string_to_sid(&sid, sidresponse.data.sid.sid);
4334 add_sid_to_token(token, &sid);
4337 SAFE_FREE(response.extra_data.data);
4339 return True;
4343 * Get a list of all user tokens we want to look at
4346 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4348 struct winbindd_request request;
4349 struct winbindd_response response;
4350 const char *extra_data;
4351 fstring name;
4352 int i;
4353 struct user_token *result;
4355 if (lp_winbind_use_default_domain() &&
4356 (opt_target_workgroup == NULL)) {
4357 d_fprintf(stderr, "winbind use default domain = yes set, "
4358 "please specify a workgroup\n");
4359 return False;
4362 /* Send request to winbind daemon */
4364 ZERO_STRUCT(request);
4365 ZERO_STRUCT(response);
4367 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4368 NSS_STATUS_SUCCESS)
4369 return False;
4371 /* Look through extra data */
4373 if (!response.extra_data.data)
4374 return False;
4376 extra_data = (const char *)response.extra_data.data;
4377 *num_tokens = 0;
4379 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4380 *num_tokens += 1;
4383 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4385 if (result == NULL) {
4386 DEBUG(1, ("Could not malloc sid array\n"));
4387 return False;
4390 extra_data = (const char *)response.extra_data.data;
4391 i=0;
4393 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4395 fstring domain, user;
4396 char *p;
4398 fstrcpy(result[i].name, name);
4400 p = strchr(name, *lp_winbind_separator());
4402 DEBUG(3, ("%s\n", name));
4404 if (p == NULL) {
4405 fstrcpy(domain, opt_target_workgroup);
4406 fstrcpy(user, name);
4407 } else {
4408 *p++ = '\0';
4409 fstrcpy(domain, name);
4410 strupper_m(domain);
4411 fstrcpy(user, p);
4414 get_user_sids(domain, user, &(result[i].token));
4415 i+=1;
4418 SAFE_FREE(response.extra_data.data);
4420 *user_tokens = result;
4422 return True;
4425 static BOOL get_user_tokens_from_file(FILE *f,
4426 int *num_tokens,
4427 struct user_token **tokens)
4429 struct user_token *token = NULL;
4431 while (!feof(f)) {
4432 fstring line;
4434 if (fgets(line, sizeof(line)-1, f) == NULL) {
4435 return True;
4438 if (line[strlen(line)-1] == '\n')
4439 line[strlen(line)-1] = '\0';
4441 if (line[0] == ' ') {
4442 /* We have a SID */
4444 DOM_SID sid;
4445 string_to_sid(&sid, &line[1]);
4447 if (token == NULL) {
4448 DEBUG(0, ("File does not begin with username"));
4449 return False;
4452 add_sid_to_token(&token->token, &sid);
4453 continue;
4456 /* And a new user... */
4458 *num_tokens += 1;
4459 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4460 if (*tokens == NULL) {
4461 DEBUG(0, ("Could not realloc tokens\n"));
4462 return False;
4465 token = &((*tokens)[*num_tokens-1]);
4467 fstrcpy(token->name, line);
4468 token->token.num_sids = 0;
4469 token->token.user_sids = NULL;
4470 continue;
4473 return False;
4478 * Show the list of all users that have access to a share
4481 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4482 TALLOC_CTX *mem_ctx,
4483 const char *netname,
4484 int num_tokens,
4485 struct user_token *tokens)
4487 int fnum;
4488 SEC_DESC *share_sd = NULL;
4489 SEC_DESC *root_sd = NULL;
4490 struct cli_state *cli = pipe_hnd->cli;
4491 int i;
4492 SRV_SHARE_INFO info;
4493 WERROR result;
4494 uint16 cnum;
4496 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4497 502, &info);
4499 if (!W_ERROR_IS_OK(result)) {
4500 DEBUG(1, ("Coult not query secdesc for share %s\n",
4501 netname));
4502 return;
4505 share_sd = info.share.info502.info_502_str.sd;
4506 if (share_sd == NULL) {
4507 DEBUG(1, ("Got no secdesc for share %s\n",
4508 netname));
4511 cnum = cli->cnum;
4513 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4514 return;
4517 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4519 if (fnum != -1) {
4520 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4523 for (i=0; i<num_tokens; i++) {
4524 uint32 acc_granted;
4525 NTSTATUS status;
4527 if (share_sd != NULL) {
4528 if (!se_access_check(share_sd, &tokens[i].token,
4529 1, &acc_granted, &status)) {
4530 DEBUG(1, ("Could not check share_sd for "
4531 "user %s\n",
4532 tokens[i].name));
4533 continue;
4536 if (!NT_STATUS_IS_OK(status))
4537 continue;
4540 if (root_sd == NULL) {
4541 d_printf(" %s\n", tokens[i].name);
4542 continue;
4545 if (!se_access_check(root_sd, &tokens[i].token,
4546 1, &acc_granted, &status)) {
4547 DEBUG(1, ("Could not check root_sd for user %s\n",
4548 tokens[i].name));
4549 continue;
4552 if (!NT_STATUS_IS_OK(status))
4553 continue;
4555 d_printf(" %s\n", tokens[i].name);
4558 if (fnum != -1)
4559 cli_close(cli, fnum);
4560 cli_tdis(cli);
4561 cli->cnum = cnum;
4563 return;
4566 struct share_list {
4567 int num_shares;
4568 char **shares;
4571 static void collect_share(const char *name, uint32 m,
4572 const char *comment, void *state)
4574 struct share_list *share_list = (struct share_list *)state;
4576 if (m != STYPE_DISKTREE)
4577 return;
4579 share_list->num_shares += 1;
4580 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4581 if (!share_list->shares) {
4582 share_list->num_shares = 0;
4583 return;
4585 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4588 static void rpc_share_userlist_usage(void)
4590 return;
4593 /**
4594 * List shares on a remote RPC server, including the security descriptors
4596 * All parameters are provided by the run_rpc_command function, except for
4597 * argc, argv which are passes through.
4599 * @param domain_sid The domain sid acquired from the remote server
4600 * @param cli A cli_state connected to the server.
4601 * @param mem_ctx Talloc context, destoyed on completion of the function.
4602 * @param argc Standard main() style argc
4603 * @param argv Standard main() style argv. Initial components are already
4604 * stripped
4606 * @return Normal NTSTATUS return.
4609 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4610 const char *domain_name,
4611 struct cli_state *cli,
4612 struct rpc_pipe_client *pipe_hnd,
4613 TALLOC_CTX *mem_ctx,
4614 int argc,
4615 const char **argv)
4617 int ret;
4618 BOOL r;
4619 ENUM_HND hnd;
4620 uint32 i;
4621 FILE *f;
4623 struct user_token *tokens = NULL;
4624 int num_tokens = 0;
4626 struct share_list share_list;
4628 if (argc > 1) {
4629 rpc_share_userlist_usage();
4630 return NT_STATUS_UNSUCCESSFUL;
4633 if (argc == 0) {
4634 f = stdin;
4635 } else {
4636 f = fopen(argv[0], "r");
4639 if (f == NULL) {
4640 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4641 return NT_STATUS_UNSUCCESSFUL;
4644 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4646 if (f != stdin)
4647 fclose(f);
4649 if (!r) {
4650 DEBUG(0, ("Could not read users from file\n"));
4651 return NT_STATUS_UNSUCCESSFUL;
4654 for (i=0; i<num_tokens; i++)
4655 collect_alias_memberships(&tokens[i].token);
4657 init_enum_hnd(&hnd, 0);
4659 share_list.num_shares = 0;
4660 share_list.shares = NULL;
4662 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4664 if (ret == -1) {
4665 DEBUG(0, ("Error returning browse list: %s\n",
4666 cli_errstr(cli)));
4667 goto done;
4670 for (i = 0; i < share_list.num_shares; i++) {
4671 char *netname = share_list.shares[i];
4673 if (netname[strlen(netname)-1] == '$')
4674 continue;
4676 d_printf("%s\n", netname);
4678 show_userlist(pipe_hnd, mem_ctx, netname,
4679 num_tokens, tokens);
4681 done:
4682 for (i=0; i<num_tokens; i++) {
4683 free_user_token(&tokens[i].token);
4685 SAFE_FREE(tokens);
4686 SAFE_FREE(share_list.shares);
4688 return NT_STATUS_OK;
4691 static int rpc_share_allowedusers(int argc, const char **argv)
4693 int result;
4695 result = run_rpc_command(NULL, PI_SAMR, 0,
4696 rpc_aliaslist_internals,
4697 argc, argv);
4698 if (result != 0)
4699 return result;
4701 result = run_rpc_command(NULL, PI_LSARPC, 0,
4702 rpc_aliaslist_dump,
4703 argc, argv);
4704 if (result != 0)
4705 return result;
4707 return run_rpc_command(NULL, PI_SRVSVC, 0,
4708 rpc_share_allowedusers_internals,
4709 argc, argv);
4712 int net_usersidlist(int argc, const char **argv)
4714 int num_tokens = 0;
4715 struct user_token *tokens = NULL;
4716 int i;
4718 if (argc != 0) {
4719 net_usersidlist_usage(argc, argv);
4720 return 0;
4723 if (!get_user_tokens(&num_tokens, &tokens)) {
4724 DEBUG(0, ("Could not get the user/sid list\n"));
4725 return 0;
4728 for (i=0; i<num_tokens; i++) {
4729 dump_user_token(&tokens[i]);
4730 free_user_token(&tokens[i].token);
4733 SAFE_FREE(tokens);
4734 return 1;
4737 int net_usersidlist_usage(int argc, const char **argv)
4739 d_printf("net usersidlist\n"
4740 "\tprints out a list of all users the running winbind knows\n"
4741 "\tabout, together with all their SIDs. This is used as\n"
4742 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4744 net_common_flags_usage(argc, argv);
4745 return -1;
4748 /**
4749 * 'net rpc share' entrypoint.
4750 * @param argc Standard main() style argc
4751 * @param argv Standard main() style argv. Initial components are already
4752 * stripped
4755 int net_rpc_share(int argc, const char **argv)
4757 struct functable func[] = {
4758 {"add", rpc_share_add},
4759 {"delete", rpc_share_delete},
4760 {"allowedusers", rpc_share_allowedusers},
4761 {"migrate", rpc_share_migrate},
4762 {"list", rpc_share_list},
4763 {NULL, NULL}
4766 if (argc == 0)
4767 return run_rpc_command(NULL, PI_SRVSVC, 0,
4768 rpc_share_list_internals,
4769 argc, argv);
4771 return net_run_function(argc, argv, func, rpc_share_usage);
4774 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4775 struct rpc_sh_ctx *ctx,
4776 struct rpc_pipe_client *pipe_hnd,
4777 int argc, const char **argv)
4779 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4780 ctx->cli, pipe_hnd, mem_ctx,
4781 argc, argv);
4784 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4785 struct rpc_sh_ctx *ctx,
4786 struct rpc_pipe_client *pipe_hnd,
4787 int argc, const char **argv)
4789 WERROR result;
4791 if ((argc < 2) || (argc > 3)) {
4792 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4793 ctx->whoami);
4794 return NT_STATUS_INVALID_PARAMETER;
4797 result = rpccli_srvsvc_net_share_add(
4798 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
4799 (argc == 3) ? argv[2] : "",
4800 0, 0, 0, argv[1], NULL, 2, NULL);
4802 return werror_to_ntstatus(result);
4805 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4806 struct rpc_sh_ctx *ctx,
4807 struct rpc_pipe_client *pipe_hnd,
4808 int argc, const char **argv)
4810 WERROR result;
4812 if (argc != 1) {
4813 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4814 return NT_STATUS_INVALID_PARAMETER;
4817 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
4818 return werror_to_ntstatus(result);
4821 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4822 struct rpc_sh_ctx *ctx,
4823 struct rpc_pipe_client *pipe_hnd,
4824 int argc, const char **argv)
4826 SRV_SHARE_INFO info;
4827 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
4828 WERROR result;
4830 if (argc != 1) {
4831 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4832 return NT_STATUS_INVALID_PARAMETER;
4835 result = rpccli_srvsvc_net_share_get_info(
4836 pipe_hnd, mem_ctx, argv[0], 2, &info);
4837 if (!W_ERROR_IS_OK(result)) {
4838 goto done;
4841 d_printf("Name: %s\n",
4842 rpcstr_pull_unistr2_talloc(mem_ctx,
4843 &info2->info_2_str.uni_netname));
4844 d_printf("Comment: %s\n",
4845 rpcstr_pull_unistr2_talloc(mem_ctx,
4846 &info2->info_2_str.uni_remark));
4848 d_printf("Path: %s\n",
4849 rpcstr_pull_unistr2_talloc(mem_ctx,
4850 &info2->info_2_str.uni_path));
4851 d_printf("Password: %s\n",
4852 rpcstr_pull_unistr2_talloc(mem_ctx,
4853 &info2->info_2_str.uni_passwd));
4855 done:
4856 return werror_to_ntstatus(result);
4859 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4860 struct rpc_sh_ctx *ctx)
4862 static struct rpc_sh_cmd cmds[] = {
4864 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4865 "List available shares" },
4867 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4868 "Add a share" },
4870 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4871 "Delete a share" },
4873 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4874 "Get information about a share" },
4876 { NULL, NULL, 0, NULL, NULL }
4879 return cmds;
4882 /****************************************************************************/
4884 static int rpc_file_usage(int argc, const char **argv)
4886 return net_help_file(argc, argv);
4889 /**
4890 * Close a file on a remote RPC server
4892 * All parameters are provided by the run_rpc_command function, except for
4893 * argc, argv which are passes through.
4895 * @param domain_sid The domain sid acquired from the remote server
4896 * @param cli A cli_state connected to the server.
4897 * @param mem_ctx Talloc context, destoyed on completion of the function.
4898 * @param argc Standard main() style argc
4899 * @param argv Standard main() style argv. Initial components are already
4900 * stripped
4902 * @return Normal NTSTATUS return.
4904 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4905 const char *domain_name,
4906 struct cli_state *cli,
4907 struct rpc_pipe_client *pipe_hnd,
4908 TALLOC_CTX *mem_ctx,
4909 int argc,
4910 const char **argv)
4912 WERROR result;
4913 result = rpccli_srvsvc_net_file_close(pipe_hnd, mem_ctx, atoi(argv[0]));
4914 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4917 /**
4918 * Close a file on a remote RPC server
4920 * @param argc Standard main() style argc
4921 * @param argv Standard main() style argv. Initial components are already
4922 * stripped
4924 * @return A shell status integer (0 for success)
4926 static int rpc_file_close(int argc, const char **argv)
4928 if (argc < 1) {
4929 DEBUG(1, ("No fileid given on close\n"));
4930 return(rpc_file_usage(argc, argv));
4933 return run_rpc_command(NULL, PI_SRVSVC, 0,
4934 rpc_file_close_internals,
4935 argc, argv);
4938 /**
4939 * Formatted print of open file info
4941 * @param info3 FILE_INFO_3 contents
4942 * @param str3 strings for FILE_INFO_3
4945 static void display_file_info_3( FILE_INFO_3 *info3 )
4947 fstring user = "", path = "";
4949 rpcstr_pull_unistr2_fstring(user, info3->user);
4950 rpcstr_pull_unistr2_fstring(path, info3->path);
4952 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4953 info3->id, user, info3->perms, info3->num_locks, path);
4956 /**
4957 * List open files on a remote RPC server
4959 * All parameters are provided by the run_rpc_command function, except for
4960 * argc, argv which are passes through.
4962 * @param domain_sid The domain sid acquired from the remote server
4963 * @param cli A cli_state connected to the server.
4964 * @param mem_ctx Talloc context, destoyed on completion of the function.
4965 * @param argc Standard main() style argc
4966 * @param argv Standard main() style argv. Initial components are already
4967 * stripped
4969 * @return Normal NTSTATUS return.
4972 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4973 const char *domain_name,
4974 struct cli_state *cli,
4975 struct rpc_pipe_client *pipe_hnd,
4976 TALLOC_CTX *mem_ctx,
4977 int argc,
4978 const char **argv)
4980 SRV_FILE_INFO_CTR ctr;
4981 WERROR result;
4982 ENUM_HND hnd;
4983 uint32 preferred_len = 0xffffffff, i;
4984 const char *username=NULL;
4986 init_enum_hnd(&hnd, 0);
4988 /* if argc > 0, must be user command */
4989 if (argc > 0)
4990 username = smb_xstrdup(argv[0]);
4992 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
4993 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4995 if (!W_ERROR_IS_OK(result))
4996 goto done;
4998 /* Display results */
5000 d_printf(
5001 "\nEnumerating open files on remote server:\n\n"\
5002 "\nFileId Opened by Perms Locks Path"\
5003 "\n------ --------- ----- ----- ---- \n");
5004 for (i = 0; i < ctr.num_entries; i++)
5005 display_file_info_3(&ctr.file.info3[i]);
5006 done:
5007 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5010 /**
5011 * List files for a user on a remote RPC server
5013 * @param argc Standard main() style argc
5014 * @param argv Standard main() style argv. Initial components are already
5015 * stripped
5017 * @return A shell status integer (0 for success)
5020 static int rpc_file_user(int argc, const char **argv)
5022 if (argc < 1) {
5023 DEBUG(1, ("No username given\n"));
5024 return(rpc_file_usage(argc, argv));
5027 return run_rpc_command(NULL, PI_SRVSVC, 0,
5028 rpc_file_list_internals,
5029 argc, argv);
5032 /**
5033 * 'net rpc file' entrypoint.
5034 * @param argc Standard main() style argc
5035 * @param argv Standard main() style argv. Initial components are already
5036 * stripped
5039 int net_rpc_file(int argc, const char **argv)
5041 struct functable func[] = {
5042 {"close", rpc_file_close},
5043 {"user", rpc_file_user},
5044 #if 0
5045 {"info", rpc_file_info},
5046 #endif
5047 {NULL, NULL}
5050 if (argc == 0)
5051 return run_rpc_command(NULL, PI_SRVSVC, 0,
5052 rpc_file_list_internals,
5053 argc, argv);
5055 return net_run_function(argc, argv, func, rpc_file_usage);
5058 /**
5059 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5061 * All parameters are provided by the run_rpc_command function, except for
5062 * argc, argv which are passed through.
5064 * @param domain_sid The domain sid aquired from the remote server
5065 * @param cli A cli_state connected to the server.
5066 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5067 * @param argc Standard main() style argc
5068 * @param argv Standard main() style argv. Initial components are already
5069 * stripped
5071 * @return Normal NTSTATUS return.
5074 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5075 const char *domain_name,
5076 struct cli_state *cli,
5077 struct rpc_pipe_client *pipe_hnd,
5078 TALLOC_CTX *mem_ctx,
5079 int argc,
5080 const char **argv)
5082 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5084 result = rpccli_shutdown_abort(pipe_hnd, mem_ctx);
5086 if (NT_STATUS_IS_OK(result)) {
5087 d_printf("\nShutdown successfully aborted\n");
5088 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5089 } else
5090 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5092 return result;
5095 /**
5096 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5098 * All parameters are provided by the run_rpc_command function, except for
5099 * argc, argv which are passed through.
5101 * @param domain_sid The domain sid aquired from the remote server
5102 * @param cli A cli_state connected to the server.
5103 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5104 * @param argc Standard main() style argc
5105 * @param argv Standard main() style argv. Initial components are already
5106 * stripped
5108 * @return Normal NTSTATUS return.
5111 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5112 const char *domain_name,
5113 struct cli_state *cli,
5114 struct rpc_pipe_client *pipe_hnd,
5115 TALLOC_CTX *mem_ctx,
5116 int argc,
5117 const char **argv)
5119 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5121 result = werror_to_ntstatus(rpccli_reg_abort_shutdown(pipe_hnd, mem_ctx));
5123 if (NT_STATUS_IS_OK(result)) {
5124 d_printf("\nShutdown successfully aborted\n");
5125 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5126 } else
5127 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5129 return result;
5132 /**
5133 * ABORT the Shut down of a remote RPC server
5135 * @param argc Standard main() style argc
5136 * @param argv Standard main() style argv. Initial components are already
5137 * stripped
5139 * @return A shell status integer (0 for success)
5142 static int rpc_shutdown_abort(int argc, const char **argv)
5144 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5145 rpc_shutdown_abort_internals,
5146 argc, argv);
5148 if (rc == 0)
5149 return rc;
5151 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5153 return run_rpc_command(NULL, PI_WINREG, 0,
5154 rpc_reg_shutdown_abort_internals,
5155 argc, argv);
5158 /**
5159 * Shut down a remote RPC Server via initshutdown pipe
5161 * All parameters are provided by the run_rpc_command function, except for
5162 * argc, argv which are passes through.
5164 * @param domain_sid The domain sid aquired from the remote server
5165 * @param cli A cli_state connected to the server.
5166 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5167 * @param argc Standard main() style argc
5168 * @param argc Standard main() style argv. Initial components are already
5169 * stripped
5171 * @return Normal NTSTATUS return.
5174 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5175 const char *domain_name,
5176 struct cli_state *cli,
5177 struct rpc_pipe_client *pipe_hnd,
5178 TALLOC_CTX *mem_ctx,
5179 int argc,
5180 const char **argv)
5182 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5183 const char *msg = "This machine will be shutdown shortly";
5184 uint32 timeout = 20;
5186 if (opt_comment) {
5187 msg = opt_comment;
5189 if (opt_timeout) {
5190 timeout = opt_timeout;
5193 /* create an entry */
5194 result = rpccli_shutdown_init(pipe_hnd, mem_ctx, msg, timeout, opt_reboot,
5195 opt_force);
5197 if (NT_STATUS_IS_OK(result)) {
5198 d_printf("\nShutdown of remote machine succeeded\n");
5199 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5200 } else {
5201 DEBUG(1,("Shutdown of remote machine failed!\n"));
5203 return result;
5206 /**
5207 * Shut down a remote RPC Server via winreg pipe
5209 * All parameters are provided by the run_rpc_command function, except for
5210 * argc, argv which are passes through.
5212 * @param domain_sid The domain sid aquired from the remote server
5213 * @param cli A cli_state connected to the server.
5214 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5215 * @param argc Standard main() style argc
5216 * @param argc Standard main() style argv. Initial components are already
5217 * stripped
5219 * @return Normal NTSTATUS return.
5222 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5223 const char *domain_name,
5224 struct cli_state *cli,
5225 struct rpc_pipe_client *pipe_hnd,
5226 TALLOC_CTX *mem_ctx,
5227 int argc,
5228 const char **argv)
5230 WERROR result;
5231 const char *msg = "This machine will be shutdown shortly";
5232 uint32 timeout = 20;
5233 #if 0
5234 poptContext pc;
5235 int rc;
5237 struct poptOption long_options[] = {
5238 {"message", 'm', POPT_ARG_STRING, &msg},
5239 {"timeout", 't', POPT_ARG_INT, &timeout},
5240 {"reboot", 'r', POPT_ARG_NONE, &reboot},
5241 {"force", 'f', POPT_ARG_NONE, &force},
5242 { 0, 0, 0, 0}
5245 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
5246 POPT_CONTEXT_KEEP_FIRST);
5248 rc = poptGetNextOpt(pc);
5250 if (rc < -1) {
5251 /* an error occurred during option processing */
5252 DEBUG(0, ("%s: %s\n",
5253 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
5254 poptStrerror(rc)));
5255 return NT_STATUS_INVALID_PARAMETER;
5257 #endif
5258 if (opt_comment) {
5259 msg = opt_comment;
5261 if (opt_timeout) {
5262 timeout = opt_timeout;
5265 /* create an entry */
5266 result = rpccli_reg_shutdown(pipe_hnd, mem_ctx, msg, timeout, opt_reboot, opt_force);
5268 if (W_ERROR_IS_OK(result)) {
5269 d_printf("\nShutdown of remote machine succeeded\n");
5270 } else {
5271 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5272 if (W_ERROR_EQUAL(result,WERR_MACHINE_LOCKED))
5273 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5274 else
5275 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(result));
5278 return werror_to_ntstatus(result);
5281 /**
5282 * Shut down a remote RPC server
5284 * @param argc Standard main() style argc
5285 * @param argc Standard main() style argv. Initial components are already
5286 * stripped
5288 * @return A shell status integer (0 for success)
5291 static int rpc_shutdown(int argc, const char **argv)
5293 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5294 rpc_init_shutdown_internals,
5295 argc, argv);
5297 if (rc) {
5298 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5299 rc = run_rpc_command(NULL, PI_WINREG, 0,
5300 rpc_reg_shutdown_internals, argc, argv);
5303 return rc;
5306 /***************************************************************************
5307 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5309 ***************************************************************************/
5312 * Add interdomain trust account to the RPC server.
5313 * All parameters (except for argc and argv) are passed by run_rpc_command
5314 * function.
5316 * @param domain_sid The domain sid acquired from the server
5317 * @param cli A cli_state connected to the server.
5318 * @param mem_ctx Talloc context, destoyed on completion of the function.
5319 * @param argc Standard main() style argc
5320 * @param argc Standard main() style argv. Initial components are already
5321 * stripped
5323 * @return normal NTSTATUS return code
5326 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5327 const char *domain_name,
5328 struct cli_state *cli,
5329 struct rpc_pipe_client *pipe_hnd,
5330 TALLOC_CTX *mem_ctx,
5331 int argc,
5332 const char **argv)
5334 POLICY_HND connect_pol, domain_pol, user_pol;
5335 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5336 char *acct_name;
5337 uint32 acb_info;
5338 uint32 unknown, user_rid;
5340 if (argc != 2) {
5341 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5342 return NT_STATUS_INVALID_PARAMETER;
5346 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5349 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5350 return NT_STATUS_NO_MEMORY;
5353 strupper_m(acct_name);
5355 /* Get samr policy handle */
5356 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5357 &connect_pol);
5358 if (!NT_STATUS_IS_OK(result)) {
5359 goto done;
5362 /* Get domain policy handle */
5363 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5364 MAXIMUM_ALLOWED_ACCESS,
5365 domain_sid, &domain_pol);
5366 if (!NT_STATUS_IS_OK(result)) {
5367 goto done;
5370 /* Create trusting domain's account */
5371 acb_info = ACB_NORMAL;
5372 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5373 mimir: yes, most probably it is */
5375 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5376 acct_name, acb_info, unknown,
5377 &user_pol, &user_rid);
5378 if (!NT_STATUS_IS_OK(result)) {
5379 goto done;
5383 SAM_USERINFO_CTR ctr;
5384 SAM_USER_INFO_23 p23;
5385 NTTIME notime;
5386 char nostr[] = "";
5387 LOGON_HRS hrs;
5388 uchar pwbuf[516];
5390 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5392 ZERO_STRUCT(ctr);
5393 ZERO_STRUCT(p23);
5394 ZERO_STRUCT(notime);
5395 hrs.max_len = 1260;
5396 hrs.offset = 0;
5397 hrs.len = 21;
5398 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5399 acb_info = ACB_DOMTRUST;
5401 init_sam_user_info23A(&p23, &notime, &notime, &notime,
5402 &notime, &notime, &notime,
5403 nostr, nostr, nostr, nostr, nostr,
5404 nostr, nostr, nostr, nostr, nostr,
5405 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5406 0, 0, (char *)pwbuf);
5407 ctr.switch_value = 23;
5408 ctr.info.id23 = &p23;
5409 p23.passmustchange = 0;
5411 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5412 &cli->user_session_key, &ctr);
5414 if (!NT_STATUS_IS_OK(result)) {
5415 DEBUG(0,("Could not set trust account password: %s\n",
5416 nt_errstr(result)));
5417 goto done;
5421 done:
5422 SAFE_FREE(acct_name);
5423 return result;
5427 * Create interdomain trust account for a remote domain.
5429 * @param argc standard argc
5430 * @param argv standard argv without initial components
5432 * @return Integer status (0 means success)
5435 static int rpc_trustdom_add(int argc, const char **argv)
5437 if (argc > 0) {
5438 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5439 argc, argv);
5440 } else {
5441 d_printf("Usage: net rpc trustdom add <domain>\n");
5442 return -1;
5448 * Remove interdomain trust account from the RPC server.
5449 * All parameters (except for argc and argv) are passed by run_rpc_command
5450 * function.
5452 * @param domain_sid The domain sid acquired from the server
5453 * @param cli A cli_state connected to the server.
5454 * @param mem_ctx Talloc context, destoyed on completion of the function.
5455 * @param argc Standard main() style argc
5456 * @param argc Standard main() style argv. Initial components are already
5457 * stripped
5459 * @return normal NTSTATUS return code
5462 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5463 const char *domain_name,
5464 struct cli_state *cli,
5465 struct rpc_pipe_client *pipe_hnd,
5466 TALLOC_CTX *mem_ctx,
5467 int argc,
5468 const char **argv)
5470 POLICY_HND connect_pol, domain_pol, user_pol;
5471 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5472 char *acct_name;
5473 const char **names;
5474 DOM_SID trust_acct_sid;
5475 uint32 *user_rids, num_rids, *name_types;
5476 uint32 flags = 0x000003e8; /* Unknown */
5478 if (argc != 1) {
5479 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5480 return NT_STATUS_INVALID_PARAMETER;
5484 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5486 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5488 if (acct_name == NULL)
5489 return NT_STATUS_NO_MEMORY;
5491 strupper_m(acct_name);
5493 if ((names = TALLOC_ARRAY(mem_ctx, const char *, 1)) == NULL) {
5494 return NT_STATUS_NO_MEMORY;
5496 names[0] = acct_name;
5499 /* Get samr policy handle */
5500 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5501 &connect_pol);
5502 if (!NT_STATUS_IS_OK(result)) {
5503 goto done;
5506 /* Get domain policy handle */
5507 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5508 MAXIMUM_ALLOWED_ACCESS,
5509 domain_sid, &domain_pol);
5510 if (!NT_STATUS_IS_OK(result)) {
5511 goto done;
5514 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5515 names, &num_rids,
5516 &user_rids, &name_types);
5518 if (!NT_STATUS_IS_OK(result)) {
5519 goto done;
5522 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5523 MAXIMUM_ALLOWED_ACCESS,
5524 user_rids[0], &user_pol);
5526 if (!NT_STATUS_IS_OK(result)) {
5527 goto done;
5530 /* append the rid to the domain sid */
5531 sid_copy(&trust_acct_sid, domain_sid);
5532 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5533 goto done;
5536 /* remove the sid */
5538 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5539 &trust_acct_sid);
5541 if (!NT_STATUS_IS_OK(result)) {
5542 goto done;
5545 /* Delete user */
5547 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5549 if (!NT_STATUS_IS_OK(result)) {
5550 goto done;
5553 if (!NT_STATUS_IS_OK(result)) {
5554 DEBUG(0,("Could not set trust account password: %s\n",
5555 nt_errstr(result)));
5556 goto done;
5559 done:
5560 return result;
5564 * Delete interdomain trust account for a remote domain.
5566 * @param argc standard argc
5567 * @param argv standard argv without initial components
5569 * @return Integer status (0 means success)
5572 static int rpc_trustdom_del(int argc, const char **argv)
5574 if (argc > 0) {
5575 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5576 argc, argv);
5577 } else {
5578 d_printf("Usage: net rpc trustdom del <domain>\n");
5579 return -1;
5585 * Establish trust relationship to a trusting domain.
5586 * Interdomain account must already be created on remote PDC.
5588 * @param argc standard argc
5589 * @param argv standard argv without initial components
5591 * @return Integer status (0 means success)
5594 static int rpc_trustdom_establish(int argc, const char **argv)
5596 struct cli_state *cli = NULL;
5597 struct in_addr server_ip;
5598 struct rpc_pipe_client *pipe_hnd = NULL;
5599 POLICY_HND connect_hnd;
5600 TALLOC_CTX *mem_ctx;
5601 NTSTATUS nt_status;
5602 DOM_SID *domain_sid;
5604 char* domain_name;
5605 char* domain_name_pol;
5606 char* acct_name;
5607 fstring pdc_name;
5610 * Connect to \\server\ipc$ as 'our domain' account with password
5613 if (argc != 1) {
5614 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5615 return -1;
5618 domain_name = smb_xstrdup(argv[0]);
5619 strupper_m(domain_name);
5621 /* account name used at first is our domain's name with '$' */
5622 asprintf(&acct_name, "%s$", lp_workgroup());
5623 strupper_m(acct_name);
5626 * opt_workgroup will be used by connection functions further,
5627 * hence it should be set to remote domain name instead of ours
5629 if (opt_workgroup) {
5630 opt_workgroup = smb_xstrdup(domain_name);
5633 opt_user_name = acct_name;
5635 /* find the domain controller */
5636 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5637 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5638 return -1;
5641 /* connect to ipc$ as username/password */
5642 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5643 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5645 /* Is it trusting domain account for sure ? */
5646 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5647 nt_errstr(nt_status)));
5648 return -1;
5651 /* store who we connected to */
5653 saf_store( domain_name, pdc_name );
5656 * Connect to \\server\ipc$ again (this time anonymously)
5659 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5661 if (NT_STATUS_IS_ERR(nt_status)) {
5662 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5663 domain_name, nt_errstr(nt_status)));
5664 return -1;
5668 * Use NetServerEnum2 to make sure we're talking to a proper server
5671 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5672 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5673 for domain %s\n", domain_name));
5674 cli_shutdown(cli);
5675 return -1;
5678 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5679 "domain %s", domain_name))) {
5680 DEBUG(0, ("talloc_init() failed\n"));
5681 cli_shutdown(cli);
5682 return -1;
5686 * Call LsaOpenPolicy and LsaQueryInfo
5689 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5690 if (!pipe_hnd) {
5691 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5692 cli_shutdown(cli);
5693 talloc_destroy(mem_ctx);
5694 return -1;
5697 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5698 &connect_hnd);
5699 if (NT_STATUS_IS_ERR(nt_status)) {
5700 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5701 nt_errstr(nt_status)));
5702 cli_shutdown(cli);
5703 talloc_destroy(mem_ctx);
5704 return -1;
5707 /* Querying info level 5 */
5709 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5710 5 /* info level */,
5711 &domain_name_pol, &domain_sid);
5712 if (NT_STATUS_IS_ERR(nt_status)) {
5713 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5714 nt_errstr(nt_status)));
5715 cli_shutdown(cli);
5716 talloc_destroy(mem_ctx);
5717 return -1;
5720 /* There should be actually query info level 3 (following nt serv behaviour),
5721 but I still don't know if it's _really_ necessary */
5724 * Store the password in secrets db
5727 if (!secrets_store_trusted_domain_password(domain_name,
5728 opt_password,
5729 domain_sid)) {
5730 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5731 cli_shutdown(cli);
5732 talloc_destroy(mem_ctx);
5733 return -1;
5737 * Close the pipes and clean up
5740 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5741 if (NT_STATUS_IS_ERR(nt_status)) {
5742 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5743 nt_errstr(nt_status)));
5744 cli_shutdown(cli);
5745 talloc_destroy(mem_ctx);
5746 return -1;
5749 cli_shutdown(cli);
5751 talloc_destroy(mem_ctx);
5753 d_printf("Trust to domain %s established\n", domain_name);
5754 return 0;
5758 * Revoke trust relationship to the remote domain
5760 * @param argc standard argc
5761 * @param argv standard argv without initial components
5763 * @return Integer status (0 means success)
5766 static int rpc_trustdom_revoke(int argc, const char **argv)
5768 char* domain_name;
5770 if (argc < 1) return -1;
5772 /* generate upper cased domain name */
5773 domain_name = smb_xstrdup(argv[0]);
5774 strupper_m(domain_name);
5776 /* delete password of the trust */
5777 if (!trusted_domain_password_delete(domain_name)) {
5778 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5779 domain_name));
5780 return -1;
5783 return 0;
5787 * Usage for 'net rpc trustdom' command
5789 * @param argc standard argc
5790 * @param argv standard argv without inital components
5792 * @return Integer status returned to shell
5795 static int rpc_trustdom_usage(int argc, const char **argv)
5797 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5798 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5799 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5800 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5801 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5802 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5803 return -1;
5807 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5808 const char *domain_name,
5809 struct cli_state *cli,
5810 struct rpc_pipe_client *pipe_hnd,
5811 TALLOC_CTX *mem_ctx,
5812 int argc,
5813 const char **argv)
5815 fstring str_sid;
5816 sid_to_string(str_sid, domain_sid);
5817 d_printf("%s\n", str_sid);
5818 return NT_STATUS_OK;
5821 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5823 fstring ascii_sid, padding;
5824 int pad_len, col_len = 20;
5826 /* convert sid into ascii string */
5827 sid_to_string(ascii_sid, dom_sid);
5829 /* calculate padding space for d_printf to look nicer */
5830 pad_len = col_len - strlen(trusted_dom_name);
5831 padding[pad_len] = 0;
5832 do padding[--pad_len] = ' '; while (pad_len);
5834 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5837 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5838 TALLOC_CTX *mem_ctx,
5839 POLICY_HND *pol,
5840 DOM_SID dom_sid,
5841 const char *trusted_dom_name)
5843 NTSTATUS nt_status;
5844 LSA_TRUSTED_DOMAIN_INFO *info;
5845 char *cleartextpwd = NULL;
5846 DATA_BLOB data;
5848 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5850 if (NT_STATUS_IS_ERR(nt_status)) {
5851 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5852 nt_errstr(nt_status)));
5853 goto done;
5856 data = data_blob(NULL, info->password.password.length);
5858 memcpy(data.data, info->password.password.data, info->password.password.length);
5859 data.length = info->password.password.length;
5861 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5863 if (cleartextpwd == NULL) {
5864 DEBUG(0,("retrieved NULL password\n"));
5865 nt_status = NT_STATUS_UNSUCCESSFUL;
5866 goto done;
5869 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5870 cleartextpwd,
5871 &dom_sid)) {
5872 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5873 nt_status = NT_STATUS_UNSUCCESSFUL;
5874 goto done;
5877 #ifdef DEBUG_PASSWORD
5878 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5879 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5880 #endif
5882 done:
5883 SAFE_FREE(cleartextpwd);
5884 data_blob_free(&data);
5886 return nt_status;
5889 static int rpc_trustdom_vampire(int argc, const char **argv)
5891 /* common variables */
5892 TALLOC_CTX* mem_ctx;
5893 struct cli_state *cli = NULL;
5894 struct rpc_pipe_client *pipe_hnd = NULL;
5895 NTSTATUS nt_status;
5896 const char *domain_name = NULL;
5897 DOM_SID *queried_dom_sid;
5898 POLICY_HND connect_hnd;
5900 /* trusted domains listing variables */
5901 unsigned int num_domains, enum_ctx = 0;
5902 int i;
5903 DOM_SID *domain_sids;
5904 char **trusted_dom_names;
5905 fstring pdc_name;
5906 char *dummy;
5909 * Listing trusted domains (stored in secrets.tdb, if local)
5912 mem_ctx = talloc_init("trust relationships vampire");
5915 * set domain and pdc name to local samba server (default)
5916 * or to remote one given in command line
5919 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5920 domain_name = opt_workgroup;
5921 opt_target_workgroup = opt_workgroup;
5922 } else {
5923 fstrcpy(pdc_name, global_myname());
5924 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5925 opt_target_workgroup = domain_name;
5928 /* open \PIPE\lsarpc and open policy handle */
5929 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5930 DEBUG(0, ("Couldn't connect to domain controller\n"));
5931 talloc_destroy(mem_ctx);
5932 return -1;
5935 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5936 if (!pipe_hnd) {
5937 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5938 nt_errstr(nt_status) ));
5939 cli_shutdown(cli);
5940 talloc_destroy(mem_ctx);
5941 return -1;
5944 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5945 &connect_hnd);
5946 if (NT_STATUS_IS_ERR(nt_status)) {
5947 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5948 nt_errstr(nt_status)));
5949 cli_shutdown(cli);
5950 talloc_destroy(mem_ctx);
5951 return -1;
5954 /* query info level 5 to obtain sid of a domain being queried */
5955 nt_status = rpccli_lsa_query_info_policy(
5956 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5957 &dummy, &queried_dom_sid);
5959 if (NT_STATUS_IS_ERR(nt_status)) {
5960 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5961 nt_errstr(nt_status)));
5962 cli_shutdown(cli);
5963 talloc_destroy(mem_ctx);
5964 return -1;
5968 * Keep calling LsaEnumTrustdom over opened pipe until
5969 * the end of enumeration is reached
5972 d_printf("Vampire trusted domains:\n\n");
5974 do {
5975 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5976 &num_domains,
5977 &trusted_dom_names, &domain_sids);
5979 if (NT_STATUS_IS_ERR(nt_status)) {
5980 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5981 nt_errstr(nt_status)));
5982 cli_shutdown(cli);
5983 talloc_destroy(mem_ctx);
5984 return -1;
5987 for (i = 0; i < num_domains; i++) {
5989 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5991 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5992 domain_sids[i], trusted_dom_names[i]);
5993 if (!NT_STATUS_IS_OK(nt_status)) {
5994 cli_shutdown(cli);
5995 talloc_destroy(mem_ctx);
5996 return -1;
6001 * in case of no trusted domains say something rather
6002 * than just display blank line
6004 if (!num_domains) d_printf("none\n");
6006 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6008 /* close this connection before doing next one */
6009 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
6010 if (NT_STATUS_IS_ERR(nt_status)) {
6011 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6012 nt_errstr(nt_status)));
6013 cli_shutdown(cli);
6014 talloc_destroy(mem_ctx);
6015 return -1;
6018 /* close lsarpc pipe and connection to IPC$ */
6019 cli_shutdown(cli);
6021 talloc_destroy(mem_ctx);
6022 return 0;
6025 static int rpc_trustdom_list(int argc, const char **argv)
6027 /* common variables */
6028 TALLOC_CTX* mem_ctx;
6029 struct cli_state *cli = NULL, *remote_cli = NULL;
6030 struct rpc_pipe_client *pipe_hnd = NULL;
6031 NTSTATUS nt_status;
6032 const char *domain_name = NULL;
6033 DOM_SID *queried_dom_sid;
6034 fstring padding;
6035 int ascii_dom_name_len;
6036 POLICY_HND connect_hnd;
6038 /* trusted domains listing variables */
6039 unsigned int num_domains, enum_ctx = 0;
6040 int i, pad_len, col_len = 20;
6041 DOM_SID *domain_sids;
6042 char **trusted_dom_names;
6043 fstring pdc_name;
6044 char *dummy;
6046 /* trusting domains listing variables */
6047 POLICY_HND domain_hnd;
6048 char **trusting_dom_names;
6049 uint32 *trusting_dom_rids;
6052 * Listing trusted domains (stored in secrets.tdb, if local)
6055 mem_ctx = talloc_init("trust relationships listing");
6058 * set domain and pdc name to local samba server (default)
6059 * or to remote one given in command line
6062 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6063 domain_name = opt_workgroup;
6064 opt_target_workgroup = opt_workgroup;
6065 } else {
6066 fstrcpy(pdc_name, global_myname());
6067 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6068 opt_target_workgroup = domain_name;
6071 /* open \PIPE\lsarpc and open policy handle */
6072 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
6073 DEBUG(0, ("Couldn't connect to domain controller\n"));
6074 talloc_destroy(mem_ctx);
6075 return -1;
6078 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6079 if (!pipe_hnd) {
6080 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6081 nt_errstr(nt_status) ));
6082 cli_shutdown(cli);
6083 talloc_destroy(mem_ctx);
6084 return -1;
6087 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6088 &connect_hnd);
6089 if (NT_STATUS_IS_ERR(nt_status)) {
6090 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6091 nt_errstr(nt_status)));
6092 cli_shutdown(cli);
6093 talloc_destroy(mem_ctx);
6094 return -1;
6097 /* query info level 5 to obtain sid of a domain being queried */
6098 nt_status = rpccli_lsa_query_info_policy(
6099 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
6100 &dummy, &queried_dom_sid);
6102 if (NT_STATUS_IS_ERR(nt_status)) {
6103 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6104 nt_errstr(nt_status)));
6105 cli_shutdown(cli);
6106 talloc_destroy(mem_ctx);
6107 return -1;
6111 * Keep calling LsaEnumTrustdom over opened pipe until
6112 * the end of enumeration is reached
6115 d_printf("Trusted domains list:\n\n");
6117 do {
6118 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
6119 &num_domains,
6120 &trusted_dom_names, &domain_sids);
6122 if (NT_STATUS_IS_ERR(nt_status)) {
6123 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6124 nt_errstr(nt_status)));
6125 cli_shutdown(cli);
6126 talloc_destroy(mem_ctx);
6127 return -1;
6130 for (i = 0; i < num_domains; i++) {
6131 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6135 * in case of no trusted domains say something rather
6136 * than just display blank line
6138 if (!num_domains) d_printf("none\n");
6140 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6142 /* close this connection before doing next one */
6143 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
6144 if (NT_STATUS_IS_ERR(nt_status)) {
6145 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6146 nt_errstr(nt_status)));
6147 cli_shutdown(cli);
6148 talloc_destroy(mem_ctx);
6149 return -1;
6152 cli_rpc_pipe_close(pipe_hnd);
6155 * Listing trusting domains (stored in passdb backend, if local)
6158 d_printf("\nTrusting domains list:\n\n");
6161 * Open \PIPE\samr and get needed policy handles
6163 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6164 if (!pipe_hnd) {
6165 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6166 cli_shutdown(cli);
6167 talloc_destroy(mem_ctx);
6168 return -1;
6171 /* SamrConnect */
6172 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6173 &connect_hnd);
6174 if (!NT_STATUS_IS_OK(nt_status)) {
6175 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6176 nt_errstr(nt_status)));
6177 cli_shutdown(cli);
6178 talloc_destroy(mem_ctx);
6179 return -1;
6182 /* SamrOpenDomain - we have to open domain policy handle in order to be
6183 able to enumerate accounts*/
6184 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6185 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6186 queried_dom_sid, &domain_hnd);
6187 if (!NT_STATUS_IS_OK(nt_status)) {
6188 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6189 nt_errstr(nt_status)));
6190 cli_shutdown(cli);
6191 talloc_destroy(mem_ctx);
6192 return -1;
6196 * perform actual enumeration
6199 enum_ctx = 0; /* reset enumeration context from last enumeration */
6200 do {
6202 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6203 &enum_ctx, ACB_DOMTRUST, 0xffff,
6204 &trusting_dom_names, &trusting_dom_rids,
6205 &num_domains);
6206 if (NT_STATUS_IS_ERR(nt_status)) {
6207 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6208 nt_errstr(nt_status)));
6209 cli_shutdown(cli);
6210 talloc_destroy(mem_ctx);
6211 return -1;
6214 for (i = 0; i < num_domains; i++) {
6217 * get each single domain's sid (do we _really_ need this ?):
6218 * 1) connect to domain's pdc
6219 * 2) query the pdc for domain's sid
6222 /* get rid of '$' tail */
6223 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6224 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6225 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6227 /* calculate padding space for d_printf to look nicer */
6228 pad_len = col_len - strlen(trusting_dom_names[i]);
6229 padding[pad_len] = 0;
6230 do padding[--pad_len] = ' '; while (pad_len);
6232 /* set opt_* variables to remote domain */
6233 strupper_m(trusting_dom_names[i]);
6234 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6235 opt_target_workgroup = opt_workgroup;
6237 d_printf("%s%s", trusting_dom_names[i], padding);
6239 /* connect to remote domain controller */
6240 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6241 if (remote_cli) {
6242 /* query for domain's sid */
6243 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6244 d_fprintf(stderr, "couldn't get domain's sid\n");
6246 cli_shutdown(remote_cli);
6248 } else {
6249 d_fprintf(stderr, "domain controller is not responding\n");
6253 if (!num_domains) d_printf("none\n");
6255 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6257 /* close opened samr and domain policy handles */
6258 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6259 if (!NT_STATUS_IS_OK(nt_status)) {
6260 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6263 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6264 if (!NT_STATUS_IS_OK(nt_status)) {
6265 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6268 /* close samr pipe and connection to IPC$ */
6269 cli_shutdown(cli);
6271 talloc_destroy(mem_ctx);
6272 return 0;
6276 * Entrypoint for 'net rpc trustdom' code
6278 * @param argc standard argc
6279 * @param argv standard argv without initial components
6281 * @return Integer status (0 means success)
6284 static int rpc_trustdom(int argc, const char **argv)
6286 struct functable func[] = {
6287 {"add", rpc_trustdom_add},
6288 {"del", rpc_trustdom_del},
6289 {"establish", rpc_trustdom_establish},
6290 {"revoke", rpc_trustdom_revoke},
6291 {"help", rpc_trustdom_usage},
6292 {"list", rpc_trustdom_list},
6293 {"vampire", rpc_trustdom_vampire},
6294 {NULL, NULL}
6297 if (argc == 0) {
6298 rpc_trustdom_usage(argc, argv);
6299 return -1;
6302 return (net_run_function(argc, argv, func, rpc_user_usage));
6306 * Check if a server will take rpc commands
6307 * @param flags Type of server to connect to (PDC, DMB, localhost)
6308 * if the host is not explicitly specified
6309 * @return BOOL (true means rpc supported)
6311 BOOL net_rpc_check(unsigned flags)
6313 struct cli_state *cli;
6314 BOOL ret = False;
6315 struct in_addr server_ip;
6316 char *server_name = NULL;
6317 NTSTATUS status;
6319 /* flags (i.e. server type) may depend on command */
6320 if (!net_find_server(NULL, flags, &server_ip, &server_name))
6321 return False;
6323 if ((cli = cli_initialise()) == NULL) {
6324 return False;
6327 status = cli_connect(cli, server_name, &server_ip);
6328 if (!NT_STATUS_IS_OK(status))
6329 goto done;
6330 if (!attempt_netbios_session_request(&cli, global_myname(),
6331 server_name, &server_ip))
6332 goto done;
6333 if (!cli_negprot(cli))
6334 goto done;
6335 if (cli->protocol < PROTOCOL_NT1)
6336 goto done;
6338 ret = True;
6339 done:
6340 cli_shutdown(cli);
6341 return ret;
6344 /* dump sam database via samsync rpc calls */
6345 static int rpc_samdump(int argc, const char **argv) {
6346 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6347 argc, argv);
6350 /* syncronise sam database via samsync rpc calls */
6351 static int rpc_vampire(int argc, const char **argv) {
6352 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6353 argc, argv);
6356 /**
6357 * Migrate everything from a print-server
6359 * @param argc Standard main() style argc
6360 * @param argv Standard main() style argv. Initial components are already
6361 * stripped
6363 * @return A shell status integer (0 for success)
6365 * The order is important !
6366 * To successfully add drivers the print-queues have to exist !
6367 * Applying ACLs should be the last step, because you're easily locked out
6370 static int rpc_printer_migrate_all(int argc, const char **argv)
6372 int ret;
6374 if (!opt_host) {
6375 printf("no server to migrate\n");
6376 return -1;
6379 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6380 if (ret)
6381 return ret;
6383 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6384 if (ret)
6385 return ret;
6387 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6388 if (ret)
6389 return ret;
6391 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6392 if (ret)
6393 return ret;
6395 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6399 /**
6400 * Migrate print-drivers from a print-server
6402 * @param argc Standard main() style argc
6403 * @param argv Standard main() style argv. Initial components are already
6404 * stripped
6406 * @return A shell status integer (0 for success)
6408 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6410 if (!opt_host) {
6411 printf("no server to migrate\n");
6412 return -1;
6415 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6416 rpc_printer_migrate_drivers_internals,
6417 argc, argv);
6420 /**
6421 * Migrate print-forms from a print-server
6423 * @param argc Standard main() style argc
6424 * @param argv Standard main() style argv. Initial components are already
6425 * stripped
6427 * @return A shell status integer (0 for success)
6429 static int rpc_printer_migrate_forms(int argc, const char **argv)
6431 if (!opt_host) {
6432 printf("no server to migrate\n");
6433 return -1;
6436 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6437 rpc_printer_migrate_forms_internals,
6438 argc, argv);
6441 /**
6442 * Migrate printers from a print-server
6444 * @param argc Standard main() style argc
6445 * @param argv Standard main() style argv. Initial components are already
6446 * stripped
6448 * @return A shell status integer (0 for success)
6450 static int rpc_printer_migrate_printers(int argc, const char **argv)
6452 if (!opt_host) {
6453 printf("no server to migrate\n");
6454 return -1;
6457 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6458 rpc_printer_migrate_printers_internals,
6459 argc, argv);
6462 /**
6463 * Migrate printer-ACLs from a print-server
6465 * @param argc Standard main() style argc
6466 * @param argv Standard main() style argv. Initial components are already
6467 * stripped
6469 * @return A shell status integer (0 for success)
6471 static int rpc_printer_migrate_security(int argc, const char **argv)
6473 if (!opt_host) {
6474 printf("no server to migrate\n");
6475 return -1;
6478 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6479 rpc_printer_migrate_security_internals,
6480 argc, argv);
6483 /**
6484 * Migrate printer-settings from a print-server
6486 * @param argc Standard main() style argc
6487 * @param argv Standard main() style argv. Initial components are already
6488 * stripped
6490 * @return A shell status integer (0 for success)
6492 static int rpc_printer_migrate_settings(int argc, const char **argv)
6494 if (!opt_host) {
6495 printf("no server to migrate\n");
6496 return -1;
6499 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6500 rpc_printer_migrate_settings_internals,
6501 argc, argv);
6504 /**
6505 * 'net rpc printer' entrypoint.
6506 * @param argc Standard main() style argc
6507 * @param argv Standard main() style argv. Initial components are already
6508 * stripped
6511 int rpc_printer_migrate(int argc, const char **argv)
6514 /* ouch: when addriver and setdriver are called from within
6515 rpc_printer_migrate_drivers_internals, the printer-queue already
6516 *has* to exist */
6518 struct functable func[] = {
6519 {"all", rpc_printer_migrate_all},
6520 {"drivers", rpc_printer_migrate_drivers},
6521 {"forms", rpc_printer_migrate_forms},
6522 {"help", rpc_printer_usage},
6523 {"printers", rpc_printer_migrate_printers},
6524 {"security", rpc_printer_migrate_security},
6525 {"settings", rpc_printer_migrate_settings},
6526 {NULL, NULL}
6529 return net_run_function(argc, argv, func, rpc_printer_usage);
6533 /**
6534 * List printers on a remote RPC server
6536 * @param argc Standard main() style argc
6537 * @param argv Standard main() style argv. Initial components are already
6538 * stripped
6540 * @return A shell status integer (0 for success)
6542 static int rpc_printer_list(int argc, const char **argv)
6545 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6546 rpc_printer_list_internals,
6547 argc, argv);
6550 /**
6551 * List printer-drivers on a remote RPC server
6553 * @param argc Standard main() style argc
6554 * @param argv Standard main() style argv. Initial components are already
6555 * stripped
6557 * @return A shell status integer (0 for success)
6559 static int rpc_printer_driver_list(int argc, const char **argv)
6562 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6563 rpc_printer_driver_list_internals,
6564 argc, argv);
6567 /**
6568 * Publish printer in ADS via MSRPC
6570 * @param argc Standard main() style argc
6571 * @param argv Standard main() style argv. Initial components are already
6572 * stripped
6574 * @return A shell status integer (0 for success)
6576 static int rpc_printer_publish_publish(int argc, const char **argv)
6579 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6580 rpc_printer_publish_publish_internals,
6581 argc, argv);
6584 /**
6585 * Update printer in ADS via MSRPC
6587 * @param argc Standard main() style argc
6588 * @param argv Standard main() style argv. Initial components are already
6589 * stripped
6591 * @return A shell status integer (0 for success)
6593 static int rpc_printer_publish_update(int argc, const char **argv)
6596 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6597 rpc_printer_publish_update_internals,
6598 argc, argv);
6601 /**
6602 * UnPublish printer in ADS via MSRPC
6604 * @param argc Standard main() style argc
6605 * @param argv Standard main() style argv. Initial components are already
6606 * stripped
6608 * @return A shell status integer (0 for success)
6610 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6613 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6614 rpc_printer_publish_unpublish_internals,
6615 argc, argv);
6618 /**
6619 * List published printers via MSRPC
6621 * @param argc Standard main() style argc
6622 * @param argv Standard main() style argv. Initial components are already
6623 * stripped
6625 * @return A shell status integer (0 for success)
6627 static int rpc_printer_publish_list(int argc, const char **argv)
6630 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6631 rpc_printer_publish_list_internals,
6632 argc, argv);
6636 /**
6637 * Publish printer in ADS
6639 * @param argc Standard main() style argc
6640 * @param argv Standard main() style argv. Initial components are already
6641 * stripped
6643 * @return A shell status integer (0 for success)
6645 static int rpc_printer_publish(int argc, const char **argv)
6648 struct functable func[] = {
6649 {"publish", rpc_printer_publish_publish},
6650 {"update", rpc_printer_publish_update},
6651 {"unpublish", rpc_printer_publish_unpublish},
6652 {"list", rpc_printer_publish_list},
6653 {"help", rpc_printer_usage},
6654 {NULL, NULL}
6657 if (argc == 0)
6658 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6659 rpc_printer_publish_list_internals,
6660 argc, argv);
6662 return net_run_function(argc, argv, func, rpc_printer_usage);
6667 /**
6668 * Display rpc printer help page.
6669 * @param argc Standard main() style argc
6670 * @param argv Standard main() style argv. Initial components are already
6671 * stripped
6673 int rpc_printer_usage(int argc, const char **argv)
6675 return net_help_printer(argc, argv);
6678 /**
6679 * 'net rpc printer' entrypoint.
6680 * @param argc Standard main() style argc
6681 * @param argv Standard main() style argv. Initial components are already
6682 * stripped
6684 int net_rpc_printer(int argc, const char **argv)
6686 struct functable func[] = {
6687 {"list", rpc_printer_list},
6688 {"migrate", rpc_printer_migrate},
6689 {"driver", rpc_printer_driver_list},
6690 {"publish", rpc_printer_publish},
6691 {NULL, NULL}
6694 if (argc == 0)
6695 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6696 rpc_printer_list_internals,
6697 argc, argv);
6699 return net_run_function(argc, argv, func, rpc_printer_usage);
6702 /****************************************************************************/
6705 /**
6706 * Basic usage function for 'net rpc'
6707 * @param argc Standard main() style argc
6708 * @param argv Standard main() style argv. Initial components are already
6709 * stripped
6712 int net_rpc_usage(int argc, const char **argv)
6714 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6715 d_printf(" net rpc join \t\t\tto join a domain \n");
6716 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6717 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6718 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6719 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6720 d_printf(" net rpc group \t\tto list groups\n");
6721 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6722 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6723 d_printf(" net rpc file \t\t\tto list open files\n");
6724 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6725 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6726 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6727 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6728 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6729 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6730 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6731 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6732 d_printf(" net rpc registry\t\tto manage registry hives\n");
6733 d_printf(" net rpc service\t\tto start, stop and query services\n");
6734 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6735 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6736 d_printf("\n");
6737 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6738 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6739 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6740 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6741 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6742 return -1;
6747 * Help function for 'net rpc'. Calls command specific help if requested
6748 * or displays usage of net rpc
6749 * @param argc Standard main() style argc
6750 * @param argv Standard main() style argv. Initial components are already
6751 * stripped
6754 int net_rpc_help(int argc, const char **argv)
6756 struct functable func[] = {
6757 {"join", rpc_join_usage},
6758 {"user", rpc_user_usage},
6759 {"group", rpc_group_usage},
6760 {"share", rpc_share_usage},
6761 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6762 {"trustdom", rpc_trustdom_usage},
6763 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6764 /*{"shutdown", rpc_shutdown_usage}, */
6765 {"vampire", rpc_vampire_usage},
6766 {NULL, NULL}
6769 if (argc == 0) {
6770 net_rpc_usage(argc, argv);
6771 return -1;
6774 return (net_run_function(argc, argv, func, rpc_user_usage));
6777 /**
6778 * 'net rpc' entrypoint.
6779 * @param argc Standard main() style argc
6780 * @param argv Standard main() style argv. Initial components are already
6781 * stripped
6784 int net_rpc(int argc, const char **argv)
6786 struct functable func[] = {
6787 {"audit", net_rpc_audit},
6788 {"info", net_rpc_info},
6789 {"join", net_rpc_join},
6790 {"oldjoin", net_rpc_oldjoin},
6791 {"testjoin", net_rpc_testjoin},
6792 {"user", net_rpc_user},
6793 {"password", rpc_user_password},
6794 {"group", net_rpc_group},
6795 {"share", net_rpc_share},
6796 {"file", net_rpc_file},
6797 {"printer", net_rpc_printer},
6798 {"changetrustpw", net_rpc_changetrustpw},
6799 {"trustdom", rpc_trustdom},
6800 {"abortshutdown", rpc_shutdown_abort},
6801 {"shutdown", rpc_shutdown},
6802 {"samdump", rpc_samdump},
6803 {"vampire", rpc_vampire},
6804 {"getsid", net_rpc_getsid},
6805 {"rights", net_rpc_rights},
6806 {"service", net_rpc_service},
6807 {"registry", net_rpc_registry},
6808 {"shell", net_rpc_shell},
6809 {"help", net_rpc_help},
6810 {NULL, NULL}
6812 return net_run_function(argc, argv, func, net_rpc_usage);