r19263: Be more accurate in telling what the sitename problem is in this DEBUG
[Samba.git] / source3 / utils / net_rpc.c
blobc22ffb9878f7ef8a9a3746747d6fdd8afa22417f
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)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "includes.h"
25 #include "utils/net.h"
27 static int net_mode_share;
29 /**
30 * @file net_rpc.c
32 * @brief RPC based subcommands for the 'net' utility.
34 * This file should contain much of the functionality that used to
35 * be found in rpcclient, execpt that the commands should change
36 * less often, and the fucntionality should be sane (the user is not
37 * expected to know a rid/sid before they conduct an operation etc.)
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
41 **/
44 /**
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
51 **/
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
54 DOM_SID **domain_sid, char **domain_name)
56 struct rpc_pipe_client *lsa_pipe;
57 POLICY_HND pol;
58 NTSTATUS result = NT_STATUS_OK;
59 uint32 info_class = 5;
61 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
62 if (!lsa_pipe) {
63 d_fprintf(stderr, "Could not initialise lsa pipe\n");
64 return result;
67 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False,
68 SEC_RIGHTS_MAXIMUM_ALLOWED,
69 &pol);
70 if (!NT_STATUS_IS_OK(result)) {
71 d_fprintf(stderr, "open_policy failed: %s\n",
72 nt_errstr(result));
73 return result;
76 result = rpccli_lsa_query_info_policy(lsa_pipe, mem_ctx, &pol,
77 info_class, domain_name,
78 domain_sid);
79 if (!NT_STATUS_IS_OK(result)) {
80 d_fprintf(stderr, "lsaquery failed: %s\n",
81 nt_errstr(result));
82 return result;
85 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
86 cli_rpc_pipe_close(lsa_pipe);
88 return NT_STATUS_OK;
91 /**
92 * Run a single RPC command, from start to finish.
94 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
95 * @param conn_flag a NET_FLAG_ combination. Passed to
96 * net_make_ipc_connection.
97 * @param argc Standard main() style argc
98 * @param argc Standard main() style argv. Initial components are already
99 * stripped
100 * @return A shell status integer (0 for success)
103 int run_rpc_command(struct cli_state *cli_arg,
104 const int pipe_idx,
105 int conn_flags,
106 rpc_command_fn fn,
107 int argc,
108 const char **argv)
110 struct cli_state *cli = NULL;
111 struct rpc_pipe_client *pipe_hnd = NULL;
112 TALLOC_CTX *mem_ctx;
113 NTSTATUS nt_status;
114 DOM_SID *domain_sid;
115 char *domain_name;
117 /* make use of cli_state handed over as an argument, if possible */
118 if (!cli_arg) {
119 cli = net_make_ipc_connection(conn_flags);
120 } else {
121 cli = cli_arg;
124 if (!cli) {
125 return -1;
128 /* Create mem_ctx */
130 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
131 DEBUG(0, ("talloc_init() failed\n"));
132 cli_shutdown(cli);
133 return -1;
136 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
137 &domain_name);
138 if (!NT_STATUS_IS_OK(nt_status)) {
139 cli_shutdown(cli);
140 return -1;
143 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
144 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
145 /* Always try and create an schannel netlogon pipe. */
146 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
147 PIPE_AUTH_LEVEL_PRIVACY,
148 domain_name,
149 &nt_status);
150 if (!pipe_hnd) {
151 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
152 nt_errstr(nt_status) ));
153 cli_shutdown(cli);
154 return -1;
156 } else {
157 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
158 if (!pipe_hnd) {
159 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
160 cli_get_pipe_name(pipe_idx),
161 nt_errstr(nt_status) ));
162 cli_shutdown(cli);
163 return -1;
168 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
170 if (!NT_STATUS_IS_OK(nt_status)) {
171 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
172 } else {
173 DEBUG(5, ("rpc command function succedded\n"));
176 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
177 if (pipe_hnd) {
178 cli_rpc_pipe_close(pipe_hnd);
182 /* close the connection only if it was opened here */
183 if (!cli_arg) {
184 cli_shutdown(cli);
187 talloc_destroy(mem_ctx);
188 return (!NT_STATUS_IS_OK(nt_status));
191 /**
192 * Force a change of the trust acccount password.
194 * All parameters are provided by the run_rpc_command function, except for
195 * argc, argv which are passes through.
197 * @param domain_sid The domain sid aquired from the remote server
198 * @param cli A cli_state connected to the server.
199 * @param mem_ctx Talloc context, destoyed on compleation of the function.
200 * @param argc Standard main() style argc
201 * @param argc Standard main() style argv. Initial components are already
202 * stripped
204 * @return Normal NTSTATUS return.
207 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
208 const char *domain_name,
209 struct cli_state *cli,
210 struct rpc_pipe_client *pipe_hnd,
211 TALLOC_CTX *mem_ctx,
212 int argc,
213 const char **argv)
216 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
219 /**
220 * Force a change of the trust acccount password.
222 * @param argc Standard main() style argc
223 * @param argc Standard main() style argv. Initial components are already
224 * stripped
226 * @return A shell status integer (0 for success)
229 int net_rpc_changetrustpw(int argc, const char **argv)
231 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
232 rpc_changetrustpw_internals,
233 argc, argv);
236 /**
237 * Join a domain, the old way.
239 * This uses 'machinename' as the inital password, and changes it.
241 * The password should be created with 'server manager' or equiv first.
243 * All parameters are provided by the run_rpc_command function, except for
244 * argc, argv which are passes through.
246 * @param domain_sid The domain sid aquired from the remote server
247 * @param cli A cli_state connected to the server.
248 * @param mem_ctx Talloc context, destoyed on compleation of the function.
249 * @param argc Standard main() style argc
250 * @param argc Standard main() style argv. Initial components are already
251 * stripped
253 * @return Normal NTSTATUS return.
256 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
257 const char *domain_name,
258 struct cli_state *cli,
259 struct rpc_pipe_client *pipe_hnd,
260 TALLOC_CTX *mem_ctx,
261 int argc,
262 const char **argv)
265 fstring trust_passwd;
266 unsigned char orig_trust_passwd_hash[16];
267 NTSTATUS result;
268 uint32 sec_channel_type;
270 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
271 if (!pipe_hnd) {
272 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
273 "error was %s\n",
274 cli->desthost,
275 nt_errstr(result) ));
276 return result;
280 check what type of join - if the user want's to join as
281 a BDC, the server must agree that we are a BDC.
283 if (argc >= 0) {
284 sec_channel_type = get_sec_channel_type(argv[0]);
285 } else {
286 sec_channel_type = get_sec_channel_type(NULL);
289 fstrcpy(trust_passwd, global_myname());
290 strlower_m(trust_passwd);
293 * Machine names can be 15 characters, but the max length on
294 * a password is 14. --jerry
297 trust_passwd[14] = '\0';
299 E_md4hash(trust_passwd, orig_trust_passwd_hash);
301 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
302 orig_trust_passwd_hash,
303 sec_channel_type);
305 if (NT_STATUS_IS_OK(result))
306 printf("Joined domain %s.\n",opt_target_workgroup);
309 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
310 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
311 result = NT_STATUS_UNSUCCESSFUL;
314 return result;
317 /**
318 * Join a domain, the old way.
320 * @param argc Standard main() style argc
321 * @param argc Standard main() style argv. Initial components are already
322 * stripped
324 * @return A shell status integer (0 for success)
327 static int net_rpc_perform_oldjoin(int argc, const char **argv)
329 return run_rpc_command(NULL, PI_NETLOGON,
330 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
331 rpc_oldjoin_internals,
332 argc, argv);
335 /**
336 * Join a domain, the old way. This function exists to allow
337 * the message to be displayed when oldjoin was explicitly
338 * requested, but not when it was implied by "net rpc join"
340 * @param argc Standard main() style argc
341 * @param argc Standard main() style argv. Initial components are already
342 * stripped
344 * @return A shell status integer (0 for success)
347 static int net_rpc_oldjoin(int argc, const char **argv)
349 int rc = net_rpc_perform_oldjoin(argc, argv);
351 if (rc) {
352 d_fprintf(stderr, "Failed to join domain\n");
355 return rc;
358 /**
359 * Basic usage function for 'net rpc join'
360 * @param argc Standard main() style argc
361 * @param argc Standard main() style argv. Initial components are already
362 * stripped
365 static int rpc_join_usage(int argc, const char **argv)
367 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
368 "\t to join a domain with admin username & password\n"\
369 "\t\t password will be prompted if needed and none is specified\n"\
370 "\t <type> can be (default MEMBER)\n"\
371 "\t\t BDC - Join as a BDC\n"\
372 "\t\t PDC - Join as a PDC\n"\
373 "\t\t MEMBER - Join as a MEMBER server\n");
375 net_common_flags_usage(argc, argv);
376 return -1;
379 /**
380 * 'net rpc join' entrypoint.
381 * @param argc Standard main() style argc
382 * @param argc Standard main() style argv. Initial components are already
383 * stripped
385 * Main 'net_rpc_join()' (where the admain username/password is used) is
386 * in net_rpc_join.c
387 * Try to just change the password, but if that doesn't work, use/prompt
388 * for a username/password.
391 int net_rpc_join(int argc, const char **argv)
393 if (lp_server_role() == ROLE_STANDALONE) {
394 d_printf("cannot join as standalone machine\n");
395 return -1;
398 if (strlen(global_myname()) > 15) {
399 d_printf("Our netbios name can be at most 15 chars long, "
400 "\"%s\" is %u chars long\n",
401 global_myname(), (unsigned int)strlen(global_myname()));
402 return -1;
405 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
406 return 0;
408 return net_rpc_join_newstyle(argc, argv);
411 /**
412 * display info about a rpc domain
414 * All parameters are provided by the run_rpc_command function, except for
415 * argc, argv which are passed through.
417 * @param domain_sid The domain sid acquired from the remote server
418 * @param cli A cli_state connected to the server.
419 * @param mem_ctx Talloc context, destoyed on completion of the function.
420 * @param argc Standard main() style argc
421 * @param argv Standard main() style argv. Initial components are already
422 * stripped
424 * @return Normal NTSTATUS return.
427 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
428 const char *domain_name,
429 struct cli_state *cli,
430 struct rpc_pipe_client *pipe_hnd,
431 TALLOC_CTX *mem_ctx,
432 int argc,
433 const char **argv)
435 POLICY_HND connect_pol, domain_pol;
436 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
437 SAM_UNK_CTR ctr;
438 fstring sid_str;
440 sid_to_string(sid_str, domain_sid);
442 /* Get sam policy handle */
443 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
444 &connect_pol);
445 if (!NT_STATUS_IS_OK(result)) {
446 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
447 goto done;
450 /* Get domain policy handle */
451 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
452 MAXIMUM_ALLOWED_ACCESS,
453 domain_sid, &domain_pol);
454 if (!NT_STATUS_IS_OK(result)) {
455 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
456 goto done;
459 ZERO_STRUCT(ctr);
460 result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
461 2, &ctr);
462 if (NT_STATUS_IS_OK(result)) {
463 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
464 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
465 d_printf("Domain SID: %s\n", sid_str);
466 d_printf("Sequence number: %llu\n", (unsigned long long)ctr.info.inf2.seq_num);
467 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
468 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
469 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
470 talloc_destroy(ctx);
473 done:
474 return result;
477 /**
478 * 'net rpc info' entrypoint.
479 * @param argc Standard main() style argc
480 * @param argc Standard main() style argv. Initial components are already
481 * stripped
484 int net_rpc_info(int argc, const char **argv)
486 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
487 rpc_info_internals,
488 argc, argv);
491 /**
492 * Fetch domain SID into the local secrets.tdb
494 * All parameters are provided by the run_rpc_command function, except for
495 * argc, argv which are passes through.
497 * @param domain_sid The domain sid acquired from the remote server
498 * @param cli A cli_state connected to the server.
499 * @param mem_ctx Talloc context, destoyed on completion of the function.
500 * @param argc Standard main() style argc
501 * @param argv Standard main() style argv. Initial components are already
502 * stripped
504 * @return Normal NTSTATUS return.
507 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
508 const char *domain_name,
509 struct cli_state *cli,
510 struct rpc_pipe_client *pipe_hnd,
511 TALLOC_CTX *mem_ctx,
512 int argc,
513 const char **argv)
515 fstring sid_str;
517 sid_to_string(sid_str, domain_sid);
518 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
519 sid_str, domain_name);
521 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
522 DEBUG(0,("Can't store domain SID\n"));
523 return NT_STATUS_UNSUCCESSFUL;
526 return NT_STATUS_OK;
529 /**
530 * 'net rpc getsid' entrypoint.
531 * @param argc Standard main() style argc
532 * @param argc Standard main() style argv. Initial components are already
533 * stripped
536 int net_rpc_getsid(int argc, const char **argv)
538 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
539 rpc_getsid_internals,
540 argc, argv);
543 /****************************************************************************/
546 * Basic usage function for 'net rpc user'
547 * @param argc Standard main() style argc.
548 * @param argv Standard main() style argv. Initial components are already
549 * stripped.
552 static int rpc_user_usage(int argc, const char **argv)
554 return net_help_user(argc, argv);
557 /**
558 * Add a new user to a remote RPC server
560 * All parameters are provided by the run_rpc_command function, except for
561 * argc, argv which are passes through.
563 * @param domain_sid The domain sid acquired from the remote server
564 * @param cli A cli_state connected to the server.
565 * @param mem_ctx Talloc context, destoyed on completion of the function.
566 * @param argc Standard main() style argc
567 * @param argv Standard main() style argv. Initial components are already
568 * stripped
570 * @return Normal NTSTATUS return.
573 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
574 const char *domain_name,
575 struct cli_state *cli,
576 struct rpc_pipe_client *pipe_hnd,
577 TALLOC_CTX *mem_ctx,
578 int argc, const char **argv)
581 POLICY_HND connect_pol, domain_pol, user_pol;
582 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
583 const char *acct_name;
584 uint32 acb_info;
585 uint32 unknown, user_rid;
587 if (argc < 1) {
588 d_printf("User must be specified\n");
589 rpc_user_usage(argc, argv);
590 return NT_STATUS_OK;
593 acct_name = argv[0];
595 /* Get sam policy handle */
597 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
598 &connect_pol);
599 if (!NT_STATUS_IS_OK(result)) {
600 goto done;
603 /* Get domain policy handle */
605 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
606 MAXIMUM_ALLOWED_ACCESS,
607 domain_sid, &domain_pol);
608 if (!NT_STATUS_IS_OK(result)) {
609 goto done;
612 /* Create domain user */
614 acb_info = ACB_NORMAL;
615 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
617 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
618 acct_name, acb_info, unknown,
619 &user_pol, &user_rid);
620 if (!NT_STATUS_IS_OK(result)) {
621 goto done;
624 if (argc == 2) {
626 uint32 *user_rids, num_rids, *name_types;
627 uint32 flags = 0x000003e8; /* Unknown */
628 SAM_USERINFO_CTR ctr;
629 SAM_USER_INFO_24 p24;
630 uchar pwbuf[516];
632 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
633 flags, 1, &acct_name,
634 &num_rids, &user_rids,
635 &name_types);
637 if (!NT_STATUS_IS_OK(result)) {
638 goto done;
641 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
642 MAXIMUM_ALLOWED_ACCESS,
643 user_rids[0], &user_pol);
645 if (!NT_STATUS_IS_OK(result)) {
646 goto done;
649 /* Set password on account */
651 ZERO_STRUCT(ctr);
652 ZERO_STRUCT(p24);
654 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
656 init_sam_user_info24(&p24, (char *)pwbuf,24);
658 ctr.switch_value = 24;
659 ctr.info.id24 = &p24;
661 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
662 &cli->user_session_key, &ctr);
664 if (!NT_STATUS_IS_OK(result)) {
665 d_fprintf(stderr, "Failed to set password for user %s - %s\n",
666 acct_name, nt_errstr(result));
668 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
670 if (!NT_STATUS_IS_OK(result)) {
671 d_fprintf(stderr, "Failed to delete user %s - %s\n",
672 acct_name, nt_errstr(result));
673 return result;
678 done:
679 if (!NT_STATUS_IS_OK(result)) {
680 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name,
681 nt_errstr(result));
682 } else {
683 d_printf("Added user %s\n", acct_name);
685 return result;
688 /**
689 * Add a new user to a remote RPC server
691 * @param argc Standard main() style argc
692 * @param argv Standard main() style argv. Initial components are already
693 * stripped
695 * @return A shell status integer (0 for success)
698 static int rpc_user_add(int argc, const char **argv)
700 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
701 argc, argv);
704 /**
705 * Delete a user from a remote RPC server
707 * All parameters are provided by the run_rpc_command function, except for
708 * argc, argv which are passes through.
710 * @param domain_sid The domain sid acquired from the remote server
711 * @param cli A cli_state connected to the server.
712 * @param mem_ctx Talloc context, destoyed on completion of the function.
713 * @param argc Standard main() style argc
714 * @param argv Standard main() style argv. Initial components are already
715 * stripped
717 * @return Normal NTSTATUS return.
720 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
721 const char *domain_name,
722 struct cli_state *cli,
723 struct rpc_pipe_client *pipe_hnd,
724 TALLOC_CTX *mem_ctx,
725 int argc,
726 const char **argv)
728 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
729 POLICY_HND connect_pol, domain_pol, user_pol;
731 if (argc < 1) {
732 d_printf("User must be specified\n");
733 rpc_user_usage(argc, argv);
734 return NT_STATUS_OK;
736 /* Get sam policy and domain handles */
738 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
739 &connect_pol);
741 if (!NT_STATUS_IS_OK(result)) {
742 goto done;
745 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
746 MAXIMUM_ALLOWED_ACCESS,
747 domain_sid, &domain_pol);
749 if (!NT_STATUS_IS_OK(result)) {
750 goto done;
753 /* Get handle on user */
756 uint32 *user_rids, num_rids, *name_types;
757 uint32 flags = 0x000003e8; /* Unknown */
759 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
760 flags, 1, &argv[0],
761 &num_rids, &user_rids,
762 &name_types);
764 if (!NT_STATUS_IS_OK(result)) {
765 goto done;
768 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
769 MAXIMUM_ALLOWED_ACCESS,
770 user_rids[0], &user_pol);
772 if (!NT_STATUS_IS_OK(result)) {
773 goto done;
777 /* Delete user */
779 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
781 if (!NT_STATUS_IS_OK(result)) {
782 goto done;
785 /* Display results */
786 if (!NT_STATUS_IS_OK(result)) {
787 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
788 } else {
789 d_printf("Deleted user account\n");
792 done:
793 return result;
796 /**
797 * Rename a user on a remote RPC server
799 * All parameters are provided by the run_rpc_command function, except for
800 * argc, argv which are passes through.
802 * @param domain_sid The domain sid acquired from the remote server
803 * @param cli A cli_state connected to the server.
804 * @param mem_ctx Talloc context, destoyed on completion of the function.
805 * @param argc Standard main() style argc
806 * @param argv Standard main() style argv. Initial components are already
807 * stripped
809 * @return Normal NTSTATUS return.
812 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
813 const char *domain_name,
814 struct cli_state *cli,
815 struct rpc_pipe_client *pipe_hnd,
816 TALLOC_CTX *mem_ctx,
817 int argc,
818 const char **argv)
820 POLICY_HND connect_pol, domain_pol, user_pol;
821 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
822 uint32 info_level = 7;
823 const char *old_name, *new_name;
824 uint32 *user_rid;
825 uint32 flags = 0x000003e8; /* Unknown */
826 uint32 num_rids, *name_types;
827 uint32 num_names = 1;
828 const char **names;
829 SAM_USERINFO_CTR *user_ctr;
830 SAM_USERINFO_CTR ctr;
831 SAM_USER_INFO_7 info7;
833 if (argc != 2) {
834 d_printf("Old and new username must be specified\n");
835 rpc_user_usage(argc, argv);
836 return NT_STATUS_OK;
839 old_name = argv[0];
840 new_name = argv[1];
842 ZERO_STRUCT(ctr);
843 ZERO_STRUCT(user_ctr);
845 /* Get sam policy handle */
847 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
848 &connect_pol);
849 if (!NT_STATUS_IS_OK(result)) {
850 goto done;
853 /* Get domain policy handle */
855 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
856 MAXIMUM_ALLOWED_ACCESS,
857 domain_sid, &domain_pol);
858 if (!NT_STATUS_IS_OK(result)) {
859 goto done;
862 if ((names = TALLOC_ARRAY(mem_ctx, const char *, num_names)) == NULL) {
863 result = NT_STATUS_NO_MEMORY;
864 goto done;
866 names[0] = old_name;
867 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
868 flags, num_names, names,
869 &num_rids, &user_rid, &name_types);
870 if (!NT_STATUS_IS_OK(result)) {
871 goto done;
874 /* Open domain user */
875 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
876 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
878 if (!NT_STATUS_IS_OK(result)) {
879 goto done;
882 /* Query user info */
883 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
884 info_level, &user_ctr);
886 if (!NT_STATUS_IS_OK(result)) {
887 goto done;
890 ctr.switch_value = info_level;
891 ctr.info.id7 = &info7;
893 init_sam_user_info7(&info7, new_name);
895 /* Set new name */
896 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
897 info_level, &cli->user_session_key, &ctr);
899 if (!NT_STATUS_IS_OK(result)) {
900 goto done;
903 done:
904 if (!NT_STATUS_IS_OK(result)) {
905 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
906 nt_errstr(result));
907 } else {
908 d_printf("Renamed user from %s to %s\n", old_name, new_name);
910 return result;
913 /**
914 * Rename a user on a remote RPC server
916 * @param argc Standard main() style argc
917 * @param argv Standard main() style argv. Initial components are already
918 * stripped
920 * @return A shell status integer (0 for success)
923 static int rpc_user_rename(int argc, const char **argv)
925 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
926 argc, argv);
929 /**
930 * Delete a user from a remote RPC server
932 * @param argc Standard main() style argc
933 * @param argv Standard main() style argv. Initial components are already
934 * stripped
936 * @return A shell status integer (0 for success)
939 static int rpc_user_delete(int argc, const char **argv)
941 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
942 argc, argv);
945 /**
946 * Set a password for a user on a remote RPC server
948 * All parameters are provided by the run_rpc_command function, except for
949 * argc, argv which are passes through.
951 * @param domain_sid The domain sid acquired from the remote server
952 * @param cli A cli_state connected to the server.
953 * @param mem_ctx Talloc context, destoyed on completion of the function.
954 * @param argc Standard main() style argc
955 * @param argv Standard main() style argv. Initial components are already
956 * stripped
958 * @return Normal NTSTATUS return.
961 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
962 const char *domain_name,
963 struct cli_state *cli,
964 struct rpc_pipe_client *pipe_hnd,
965 TALLOC_CTX *mem_ctx,
966 int argc,
967 const char **argv)
969 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
970 POLICY_HND connect_pol, domain_pol, user_pol;
971 SAM_USERINFO_CTR ctr;
972 SAM_USER_INFO_24 p24;
973 uchar pwbuf[516];
974 const char *user;
975 const char *new_password;
976 char *prompt = NULL;
978 if (argc < 1) {
979 d_printf("User must be specified\n");
980 rpc_user_usage(argc, argv);
981 return NT_STATUS_OK;
984 user = argv[0];
986 if (argv[1]) {
987 new_password = argv[1];
988 } else {
989 asprintf(&prompt, "Enter new password for %s:", user);
990 new_password = getpass(prompt);
991 SAFE_FREE(prompt);
994 /* Get sam policy and domain handles */
996 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
997 &connect_pol);
999 if (!NT_STATUS_IS_OK(result)) {
1000 goto done;
1003 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1004 MAXIMUM_ALLOWED_ACCESS,
1005 domain_sid, &domain_pol);
1007 if (!NT_STATUS_IS_OK(result)) {
1008 goto done;
1011 /* Get handle on user */
1014 uint32 *user_rids, num_rids, *name_types;
1015 uint32 flags = 0x000003e8; /* Unknown */
1017 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1018 flags, 1, &user,
1019 &num_rids, &user_rids,
1020 &name_types);
1022 if (!NT_STATUS_IS_OK(result)) {
1023 goto done;
1026 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1027 MAXIMUM_ALLOWED_ACCESS,
1028 user_rids[0], &user_pol);
1030 if (!NT_STATUS_IS_OK(result)) {
1031 goto done;
1035 /* Set password on account */
1037 ZERO_STRUCT(ctr);
1038 ZERO_STRUCT(p24);
1040 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1042 init_sam_user_info24(&p24, (char *)pwbuf,24);
1044 ctr.switch_value = 24;
1045 ctr.info.id24 = &p24;
1047 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
1048 &cli->user_session_key, &ctr);
1050 if (!NT_STATUS_IS_OK(result)) {
1051 goto done;
1054 /* Display results */
1056 done:
1057 return result;
1061 /**
1062 * Set a user's password on a remote RPC server
1064 * @param argc Standard main() style argc
1065 * @param argv Standard main() style argv. Initial components are already
1066 * stripped
1068 * @return A shell status integer (0 for success)
1071 static int rpc_user_password(int argc, const char **argv)
1073 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1074 argc, argv);
1077 /**
1078 * List user's groups on a remote RPC server
1080 * All parameters are provided by the run_rpc_command function, except for
1081 * argc, argv which are passes through.
1083 * @param domain_sid The domain sid acquired from the remote server
1084 * @param cli A cli_state connected to the server.
1085 * @param mem_ctx Talloc context, destoyed on completion of the function.
1086 * @param argc Standard main() style argc
1087 * @param argv Standard main() style argv. Initial components are already
1088 * stripped
1090 * @return Normal NTSTATUS return.
1093 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1094 const char *domain_name,
1095 struct cli_state *cli,
1096 struct rpc_pipe_client *pipe_hnd,
1097 TALLOC_CTX *mem_ctx,
1098 int argc,
1099 const char **argv)
1101 POLICY_HND connect_pol, domain_pol, user_pol;
1102 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1103 uint32 *rids, num_rids, *name_types, num_names;
1104 uint32 flags = 0x000003e8; /* Unknown */
1105 int i;
1106 char **names;
1107 DOM_GID *user_gids;
1109 if (argc < 1) {
1110 d_printf("User must be specified\n");
1111 rpc_user_usage(argc, argv);
1112 return NT_STATUS_OK;
1114 /* Get sam policy handle */
1116 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1117 &connect_pol);
1118 if (!NT_STATUS_IS_OK(result)) goto done;
1120 /* Get domain policy handle */
1122 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1123 MAXIMUM_ALLOWED_ACCESS,
1124 domain_sid, &domain_pol);
1125 if (!NT_STATUS_IS_OK(result)) goto done;
1127 /* Get handle on user */
1129 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1130 flags, 1, &argv[0],
1131 &num_rids, &rids, &name_types);
1133 if (!NT_STATUS_IS_OK(result)) goto done;
1135 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1136 MAXIMUM_ALLOWED_ACCESS,
1137 rids[0], &user_pol);
1138 if (!NT_STATUS_IS_OK(result)) goto done;
1140 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1141 &num_rids, &user_gids);
1143 if (!NT_STATUS_IS_OK(result)) goto done;
1145 /* Look up rids */
1147 if (num_rids) {
1148 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1149 result = NT_STATUS_NO_MEMORY;
1150 goto done;
1153 for (i = 0; i < num_rids; i++)
1154 rids[i] = user_gids[i].g_rid;
1156 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1157 num_rids, rids,
1158 &num_names, &names, &name_types);
1160 if (!NT_STATUS_IS_OK(result)) {
1161 goto done;
1164 /* Display results */
1166 for (i = 0; i < num_names; i++)
1167 printf("%s\n", names[i]);
1169 done:
1170 return result;
1173 /**
1174 * List a user's groups from a remote RPC server
1176 * @param argc Standard main() style argc
1177 * @param argv Standard main() style argv. Initial components are already
1178 * stripped
1180 * @return A shell status integer (0 for success)
1183 static int rpc_user_info(int argc, const char **argv)
1185 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1186 argc, argv);
1189 /**
1190 * List users on a remote RPC server
1192 * All parameters are provided by the run_rpc_command function, except for
1193 * argc, argv which are passes through.
1195 * @param domain_sid The domain sid acquired from the remote server
1196 * @param cli A cli_state connected to the server.
1197 * @param mem_ctx Talloc context, destoyed on completion of the function.
1198 * @param argc Standard main() style argc
1199 * @param argv Standard main() style argv. Initial components are already
1200 * stripped
1202 * @return Normal NTSTATUS return.
1205 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1206 const char *domain_name,
1207 struct cli_state *cli,
1208 struct rpc_pipe_client *pipe_hnd,
1209 TALLOC_CTX *mem_ctx,
1210 int argc,
1211 const char **argv)
1213 POLICY_HND connect_pol, domain_pol;
1214 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1215 uint32 start_idx=0, num_entries, i, loop_count = 0;
1216 SAM_DISPINFO_CTR ctr;
1217 SAM_DISPINFO_1 info1;
1219 /* Get sam policy handle */
1221 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1222 &connect_pol);
1223 if (!NT_STATUS_IS_OK(result)) {
1224 goto done;
1227 /* Get domain policy handle */
1229 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1230 MAXIMUM_ALLOWED_ACCESS,
1231 domain_sid, &domain_pol);
1232 if (!NT_STATUS_IS_OK(result)) {
1233 goto done;
1236 /* Query domain users */
1237 ZERO_STRUCT(ctr);
1238 ZERO_STRUCT(info1);
1239 ctr.sam.info1 = &info1;
1240 if (opt_long_list_entries)
1241 d_printf("\nUser name Comment"\
1242 "\n-----------------------------\n");
1243 do {
1244 fstring user, desc;
1245 uint32 max_entries, max_size;
1247 get_query_dispinfo_params(
1248 loop_count, &max_entries, &max_size);
1250 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1251 &start_idx, 1, &num_entries,
1252 max_entries, max_size, &ctr);
1253 loop_count++;
1255 for (i = 0; i < num_entries; i++) {
1256 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1257 if (opt_long_list_entries)
1258 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1260 if (opt_long_list_entries)
1261 printf("%-21.21s %s\n", user, desc);
1262 else
1263 printf("%s\n", user);
1265 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1267 done:
1268 return result;
1271 /**
1272 * 'net rpc user' entrypoint.
1273 * @param argc Standard main() style argc
1274 * @param argc Standard main() style argv. Initial components are already
1275 * stripped
1278 int net_rpc_user(int argc, const char **argv)
1280 struct functable func[] = {
1281 {"add", rpc_user_add},
1282 {"info", rpc_user_info},
1283 {"delete", rpc_user_delete},
1284 {"password", rpc_user_password},
1285 {"rename", rpc_user_rename},
1286 {NULL, NULL}
1289 if (argc == 0) {
1290 return run_rpc_command(NULL,PI_SAMR, 0,
1291 rpc_user_list_internals,
1292 argc, argv);
1295 return net_run_function(argc, argv, func, rpc_user_usage);
1298 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1299 struct rpc_sh_ctx *ctx,
1300 struct rpc_pipe_client *pipe_hnd,
1301 int argc, const char **argv)
1303 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1304 ctx->cli, pipe_hnd, mem_ctx,
1305 argc, argv);
1308 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1309 struct rpc_sh_ctx *ctx,
1310 struct rpc_pipe_client *pipe_hnd,
1311 int argc, const char **argv)
1313 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1314 ctx->cli, pipe_hnd, mem_ctx,
1315 argc, argv);
1318 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1319 struct rpc_sh_ctx *ctx,
1320 struct rpc_pipe_client *pipe_hnd,
1321 int argc, const char **argv,
1322 NTSTATUS (*fn)(
1323 TALLOC_CTX *mem_ctx,
1324 struct rpc_sh_ctx *ctx,
1325 struct rpc_pipe_client *pipe_hnd,
1326 const POLICY_HND *user_hnd,
1327 int argc, const char **argv))
1330 POLICY_HND connect_pol, domain_pol, user_pol;
1331 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1332 DOM_SID sid;
1333 uint32 rid;
1334 enum lsa_SidType type;
1336 if (argc == 0) {
1337 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1338 return NT_STATUS_INVALID_PARAMETER;
1341 ZERO_STRUCT(connect_pol);
1342 ZERO_STRUCT(domain_pol);
1343 ZERO_STRUCT(user_pol);
1345 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1346 NULL, NULL, &sid, &type);
1347 if (!NT_STATUS_IS_OK(result)) {
1348 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1349 nt_errstr(result));
1350 goto done;
1353 if (type != SID_NAME_USER) {
1354 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1355 sid_type_lookup(type));
1356 result = NT_STATUS_NO_SUCH_USER;
1357 goto done;
1360 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1361 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1362 result = NT_STATUS_NO_SUCH_USER;
1363 goto done;
1366 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1367 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1368 if (!NT_STATUS_IS_OK(result)) {
1369 goto done;
1372 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1373 MAXIMUM_ALLOWED_ACCESS,
1374 ctx->domain_sid, &domain_pol);
1375 if (!NT_STATUS_IS_OK(result)) {
1376 goto done;
1379 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1380 MAXIMUM_ALLOWED_ACCESS,
1381 rid, &user_pol);
1382 if (!NT_STATUS_IS_OK(result)) {
1383 goto done;
1386 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1388 done:
1389 if (is_valid_policy_hnd(&user_pol)) {
1390 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1392 if (is_valid_policy_hnd(&domain_pol)) {
1393 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1395 if (is_valid_policy_hnd(&connect_pol)) {
1396 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1398 return result;
1401 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1402 struct rpc_sh_ctx *ctx,
1403 struct rpc_pipe_client *pipe_hnd,
1404 const POLICY_HND *user_hnd,
1405 int argc, const char **argv)
1407 NTSTATUS result;
1408 SAM_USERINFO_CTR *ctr;
1409 SAM_USER_INFO_21 *info;
1411 if (argc != 0) {
1412 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1413 return NT_STATUS_INVALID_PARAMETER;
1416 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1417 21, &ctr);
1418 if (!NT_STATUS_IS_OK(result)) {
1419 return result;
1422 info = ctr->info.id21;
1424 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1425 info->group_rid);
1427 return result;
1430 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1431 struct rpc_sh_ctx *ctx,
1432 struct rpc_pipe_client *pipe_hnd,
1433 int argc, const char **argv)
1435 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1436 rpc_sh_user_show_internals);
1439 #define FETCHSTR(name, rec) \
1440 do { if (strequal(ctx->thiscmd, name)) { \
1441 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1442 } while (0);
1444 #define SETSTR(name, rec, flag) \
1445 do { if (strequal(ctx->thiscmd, name)) { \
1446 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1447 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1448 usr->fields_present |= ACCT_##flag; } \
1449 } while (0);
1451 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1452 struct rpc_sh_ctx *ctx,
1453 struct rpc_pipe_client *pipe_hnd,
1454 const POLICY_HND *user_hnd,
1455 int argc, const char **argv)
1457 NTSTATUS result;
1458 SAM_USERINFO_CTR *ctr;
1459 SAM_USER_INFO_21 *usr;
1460 const char *username;
1461 const char *oldval = "";
1463 if (argc > 1) {
1464 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1465 ctx->whoami);
1466 return NT_STATUS_INVALID_PARAMETER;
1469 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1470 21, &ctr);
1471 if (!NT_STATUS_IS_OK(result)) {
1472 return result;
1475 usr = ctr->info.id21;
1477 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1479 FETCHSTR("fullname", full_name);
1480 FETCHSTR("homedir", home_dir);
1481 FETCHSTR("homedrive", dir_drive);
1482 FETCHSTR("logonscript", logon_script);
1483 FETCHSTR("profilepath", profile_path);
1484 FETCHSTR("description", acct_desc);
1486 if (argc == 0) {
1487 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1488 goto done;
1491 ZERO_STRUCTP(usr);
1493 if (strcmp(argv[0], "NULL") == 0) {
1494 argv[0] = "";
1497 SETSTR("fullname", full_name, FULL_NAME);
1498 SETSTR("homedir", home_dir, HOME_DIR);
1499 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1500 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1501 SETSTR("profilepath", profile_path, PROFILE);
1502 SETSTR("description", acct_desc, DESCRIPTION);
1504 result = rpccli_samr_set_userinfo2(
1505 pipe_hnd, mem_ctx, user_hnd, 21,
1506 &pipe_hnd->cli->user_session_key, ctr);
1508 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1509 ctx->thiscmd, oldval, argv[0]);
1511 done:
1513 return result;
1516 #define HANDLEFLG(name, rec) \
1517 do { if (strequal(ctx->thiscmd, name)) { \
1518 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1519 if (newval) { \
1520 newflags = oldflags | ACB_##rec; \
1521 } else { \
1522 newflags = oldflags & ~ACB_##rec; \
1523 } } } while (0);
1525 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1526 struct rpc_sh_ctx *ctx,
1527 struct rpc_pipe_client *pipe_hnd,
1528 int argc, const char **argv)
1530 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1531 rpc_sh_user_str_edit_internals);
1534 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1535 struct rpc_sh_ctx *ctx,
1536 struct rpc_pipe_client *pipe_hnd,
1537 const POLICY_HND *user_hnd,
1538 int argc, const char **argv)
1540 NTSTATUS result;
1541 SAM_USERINFO_CTR *ctr;
1542 SAM_USER_INFO_21 *usr;
1543 const char *username;
1544 const char *oldval = "unknown";
1545 uint32 oldflags, newflags;
1546 BOOL newval;
1548 if ((argc > 1) ||
1549 ((argc == 1) && !strequal(argv[0], "yes") &&
1550 !strequal(argv[0], "no"))) {
1551 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1552 ctx->whoami);
1553 return NT_STATUS_INVALID_PARAMETER;
1556 newval = strequal(argv[0], "yes");
1558 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1559 21, &ctr);
1560 if (!NT_STATUS_IS_OK(result)) {
1561 return result;
1564 usr = ctr->info.id21;
1566 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1567 oldflags = usr->acb_info;
1568 newflags = usr->acb_info;
1570 HANDLEFLG("disabled", DISABLED);
1571 HANDLEFLG("pwnotreq", PWNOTREQ);
1572 HANDLEFLG("autolock", AUTOLOCK);
1573 HANDLEFLG("pwnoexp", PWNOEXP);
1575 if (argc == 0) {
1576 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1577 goto done;
1580 ZERO_STRUCTP(usr);
1582 usr->acb_info = newflags;
1583 usr->fields_present = ACCT_FLAGS;
1585 result = rpccli_samr_set_userinfo2(
1586 pipe_hnd, mem_ctx, user_hnd, 21,
1587 &pipe_hnd->cli->user_session_key, ctr);
1589 if (NT_STATUS_IS_OK(result)) {
1590 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1591 ctx->thiscmd, oldval, argv[0]);
1594 done:
1596 return result;
1599 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1600 struct rpc_sh_ctx *ctx,
1601 struct rpc_pipe_client *pipe_hnd,
1602 int argc, const char **argv)
1604 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1605 rpc_sh_user_flag_edit_internals);
1608 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1609 struct rpc_sh_ctx *ctx)
1611 static struct rpc_sh_cmd cmds[] = {
1613 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1614 "Show/Set a user's full name" },
1616 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1617 "Show/Set a user's home directory" },
1619 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1620 "Show/Set a user's home drive" },
1622 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1623 "Show/Set a user's logon script" },
1625 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1626 "Show/Set a user's profile path" },
1628 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1629 "Show/Set a user's description" },
1631 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1632 "Show/Set whether a user is disabled" },
1634 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1635 "Show/Set whether a user locked out" },
1637 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1638 "Show/Set whether a user does not need a password" },
1640 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1641 "Show/Set whether a user's password does not expire" },
1643 { NULL, NULL, 0, NULL, NULL }
1646 return cmds;
1649 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1650 struct rpc_sh_ctx *ctx)
1652 static struct rpc_sh_cmd cmds[] = {
1654 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1655 "List available users" },
1657 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1658 "List the domain groups a user is member of" },
1660 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1661 "Show info about a user" },
1663 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1664 "Show/Modify a user's fields" },
1666 { NULL, NULL, 0, NULL, NULL }
1669 return cmds;
1672 /****************************************************************************/
1675 * Basic usage function for 'net rpc group'
1676 * @param argc Standard main() style argc.
1677 * @param argv Standard main() style argv. Initial components are already
1678 * stripped.
1681 static int rpc_group_usage(int argc, const char **argv)
1683 return net_help_group(argc, argv);
1687 * Delete group on a remote RPC server
1689 * All parameters are provided by the run_rpc_command function, except for
1690 * argc, argv which are passes through.
1692 * @param domain_sid The domain sid acquired from the remote server
1693 * @param cli A cli_state connected to the server.
1694 * @param mem_ctx Talloc context, destoyed on completion of the function.
1695 * @param argc Standard main() style argc
1696 * @param argv Standard main() style argv. Initial components are already
1697 * stripped
1699 * @return Normal NTSTATUS return.
1702 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1703 const char *domain_name,
1704 struct cli_state *cli,
1705 struct rpc_pipe_client *pipe_hnd,
1706 TALLOC_CTX *mem_ctx,
1707 int argc,
1708 const char **argv)
1710 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1711 BOOL group_is_primary = False;
1712 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1714 uint32 *group_rids, num_rids, *name_types, num_members,
1715 *group_attrs, group_rid;
1716 uint32 flags = 0x000003e8; /* Unknown */
1717 /* char **names; */
1718 int i;
1719 /* DOM_GID *user_gids; */
1720 SAM_USERINFO_CTR *user_ctr;
1721 fstring temp;
1723 if (argc < 1) {
1724 d_printf("specify group\n");
1725 rpc_group_usage(argc,argv);
1726 return NT_STATUS_OK; /* ok? */
1729 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1730 &connect_pol);
1732 if (!NT_STATUS_IS_OK(result)) {
1733 d_fprintf(stderr, "Request samr_connect failed\n");
1734 goto done;
1737 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1738 MAXIMUM_ALLOWED_ACCESS,
1739 domain_sid, &domain_pol);
1741 if (!NT_STATUS_IS_OK(result)) {
1742 d_fprintf(stderr, "Request open_domain failed\n");
1743 goto done;
1746 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1747 flags, 1, &argv[0],
1748 &num_rids, &group_rids,
1749 &name_types);
1751 if (!NT_STATUS_IS_OK(result)) {
1752 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1753 goto done;
1756 switch (name_types[0])
1758 case SID_NAME_DOM_GRP:
1759 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1760 MAXIMUM_ALLOWED_ACCESS,
1761 group_rids[0], &group_pol);
1762 if (!NT_STATUS_IS_OK(result)) {
1763 d_fprintf(stderr, "Request open_group failed");
1764 goto done;
1767 group_rid = group_rids[0];
1769 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1770 &num_members, &group_rids,
1771 &group_attrs);
1773 if (!NT_STATUS_IS_OK(result)) {
1774 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1775 goto done;
1778 if (opt_verbose) {
1779 d_printf("Domain Group %s (rid: %d) has %d members\n",
1780 argv[0],group_rid,num_members);
1783 /* Check if group is anyone's primary group */
1784 for (i = 0; i < num_members; i++)
1786 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1787 MAXIMUM_ALLOWED_ACCESS,
1788 group_rids[i], &user_pol);
1790 if (!NT_STATUS_IS_OK(result)) {
1791 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1792 goto done;
1795 ZERO_STRUCT(user_ctr);
1797 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1798 21, &user_ctr);
1800 if (!NT_STATUS_IS_OK(result)) {
1801 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1802 goto done;
1805 if (user_ctr->info.id21->group_rid == group_rid) {
1806 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1807 sizeof(temp)-1);
1808 if (opt_verbose)
1809 d_printf("Group is primary group of %s\n",temp);
1810 group_is_primary = True;
1813 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1816 if (group_is_primary) {
1817 d_fprintf(stderr, "Unable to delete group because some "
1818 "of it's members have it as primary group\n");
1819 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1820 goto done;
1823 /* remove all group members */
1824 for (i = 0; i < num_members; i++)
1826 if (opt_verbose)
1827 d_printf("Remove group member %d...",group_rids[i]);
1828 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1830 if (NT_STATUS_IS_OK(result)) {
1831 if (opt_verbose)
1832 d_printf("ok\n");
1833 } else {
1834 if (opt_verbose)
1835 d_printf("failed\n");
1836 goto done;
1840 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1842 break;
1843 /* removing a local group is easier... */
1844 case SID_NAME_ALIAS:
1845 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1846 MAXIMUM_ALLOWED_ACCESS,
1847 group_rids[0], &group_pol);
1849 if (!NT_STATUS_IS_OK(result)) {
1850 d_fprintf(stderr, "Request open_alias failed\n");
1851 goto done;
1854 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1855 break;
1856 default:
1857 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1858 argv[0],sid_type_lookup(name_types[0]));
1859 result = NT_STATUS_UNSUCCESSFUL;
1860 goto done;
1864 if (NT_STATUS_IS_OK(result)) {
1865 if (opt_verbose)
1866 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1867 } else {
1868 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1869 get_friendly_nt_error_msg(result));
1872 done:
1873 return result;
1877 static int rpc_group_delete(int argc, const char **argv)
1879 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1880 argc,argv);
1883 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1884 const char *domain_name,
1885 struct cli_state *cli,
1886 struct rpc_pipe_client *pipe_hnd,
1887 TALLOC_CTX *mem_ctx,
1888 int argc,
1889 const char **argv)
1891 POLICY_HND connect_pol, domain_pol, group_pol;
1892 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1893 GROUP_INFO_CTR group_info;
1895 if (argc != 1) {
1896 d_printf("Group name must be specified\n");
1897 rpc_group_usage(argc, argv);
1898 return NT_STATUS_OK;
1901 /* Get sam policy handle */
1903 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1904 &connect_pol);
1905 if (!NT_STATUS_IS_OK(result)) goto done;
1907 /* Get domain policy handle */
1909 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1910 MAXIMUM_ALLOWED_ACCESS,
1911 domain_sid, &domain_pol);
1912 if (!NT_STATUS_IS_OK(result)) goto done;
1914 /* Create the group */
1916 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1917 argv[0], MAXIMUM_ALLOWED_ACCESS,
1918 &group_pol);
1919 if (!NT_STATUS_IS_OK(result)) goto done;
1921 if (strlen(opt_comment) == 0) goto done;
1923 /* We've got a comment to set */
1925 group_info.switch_value1 = 4;
1926 init_samr_group_info4(&group_info.group.info4, opt_comment);
1928 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1929 if (!NT_STATUS_IS_OK(result)) goto done;
1931 done:
1932 if (NT_STATUS_IS_OK(result))
1933 DEBUG(5, ("add group succeeded\n"));
1934 else
1935 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1937 return result;
1940 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1941 const char *domain_name,
1942 struct cli_state *cli,
1943 struct rpc_pipe_client *pipe_hnd,
1944 TALLOC_CTX *mem_ctx,
1945 int argc,
1946 const char **argv)
1948 POLICY_HND connect_pol, domain_pol, alias_pol;
1949 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1950 ALIAS_INFO_CTR alias_info;
1952 if (argc != 1) {
1953 d_printf("Alias name must be specified\n");
1954 rpc_group_usage(argc, argv);
1955 return NT_STATUS_OK;
1958 /* Get sam policy handle */
1960 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1961 &connect_pol);
1962 if (!NT_STATUS_IS_OK(result)) goto done;
1964 /* Get domain policy handle */
1966 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1967 MAXIMUM_ALLOWED_ACCESS,
1968 domain_sid, &domain_pol);
1969 if (!NT_STATUS_IS_OK(result)) goto done;
1971 /* Create the group */
1973 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1974 argv[0], &alias_pol);
1975 if (!NT_STATUS_IS_OK(result)) goto done;
1977 if (strlen(opt_comment) == 0) goto done;
1979 /* We've got a comment to set */
1981 alias_info.level = 3;
1982 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1984 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1985 if (!NT_STATUS_IS_OK(result)) goto done;
1987 done:
1988 if (NT_STATUS_IS_OK(result))
1989 DEBUG(5, ("add alias succeeded\n"));
1990 else
1991 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1993 return result;
1996 static int rpc_group_add(int argc, const char **argv)
1998 if (opt_localgroup)
1999 return run_rpc_command(NULL, PI_SAMR, 0,
2000 rpc_alias_add_internals,
2001 argc, argv);
2003 return run_rpc_command(NULL, PI_SAMR, 0,
2004 rpc_group_add_internals,
2005 argc, argv);
2008 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2009 TALLOC_CTX *mem_ctx,
2010 const char *name,
2011 DOM_SID *sid,
2012 enum lsa_SidType *type)
2014 DOM_SID *sids = NULL;
2015 enum lsa_SidType *types = NULL;
2016 struct rpc_pipe_client *pipe_hnd;
2017 POLICY_HND lsa_pol;
2018 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2020 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2021 if (!pipe_hnd) {
2022 goto done;
2025 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2026 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2028 if (!NT_STATUS_IS_OK(result)) {
2029 goto done;
2032 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2033 &name, NULL, &sids, &types);
2035 if (NT_STATUS_IS_OK(result)) {
2036 sid_copy(sid, &sids[0]);
2037 *type = types[0];
2040 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2042 done:
2043 if (pipe_hnd) {
2044 cli_rpc_pipe_close(pipe_hnd);
2047 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2049 /* Try as S-1-5-whatever */
2051 DOM_SID tmp_sid;
2053 if (string_to_sid(&tmp_sid, name)) {
2054 sid_copy(sid, &tmp_sid);
2055 *type = SID_NAME_UNKNOWN;
2056 result = NT_STATUS_OK;
2060 return result;
2063 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2064 TALLOC_CTX *mem_ctx,
2065 const DOM_SID *group_sid,
2066 const char *member)
2068 POLICY_HND connect_pol, domain_pol;
2069 NTSTATUS result;
2070 uint32 group_rid;
2071 POLICY_HND group_pol;
2073 uint32 num_rids;
2074 uint32 *rids = NULL;
2075 uint32 *rid_types = NULL;
2077 DOM_SID sid;
2079 sid_copy(&sid, group_sid);
2081 if (!sid_split_rid(&sid, &group_rid)) {
2082 return NT_STATUS_UNSUCCESSFUL;
2085 /* Get sam policy handle */
2086 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2087 &connect_pol);
2088 if (!NT_STATUS_IS_OK(result)) {
2089 return result;
2092 /* Get domain policy handle */
2093 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2094 MAXIMUM_ALLOWED_ACCESS,
2095 &sid, &domain_pol);
2096 if (!NT_STATUS_IS_OK(result)) {
2097 return result;
2100 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2101 1, &member,
2102 &num_rids, &rids, &rid_types);
2104 if (!NT_STATUS_IS_OK(result)) {
2105 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2106 goto done;
2109 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2110 MAXIMUM_ALLOWED_ACCESS,
2111 group_rid, &group_pol);
2113 if (!NT_STATUS_IS_OK(result)) {
2114 goto done;
2117 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2119 done:
2120 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2121 return result;
2124 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2125 TALLOC_CTX *mem_ctx,
2126 const DOM_SID *alias_sid,
2127 const char *member)
2129 POLICY_HND connect_pol, domain_pol;
2130 NTSTATUS result;
2131 uint32 alias_rid;
2132 POLICY_HND alias_pol;
2134 DOM_SID member_sid;
2135 enum lsa_SidType member_type;
2137 DOM_SID sid;
2139 sid_copy(&sid, alias_sid);
2141 if (!sid_split_rid(&sid, &alias_rid)) {
2142 return NT_STATUS_UNSUCCESSFUL;
2145 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2146 &member_sid, &member_type);
2148 if (!NT_STATUS_IS_OK(result)) {
2149 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2150 return result;
2153 /* Get sam policy handle */
2154 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2155 &connect_pol);
2156 if (!NT_STATUS_IS_OK(result)) {
2157 goto done;
2160 /* Get domain policy handle */
2161 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2162 MAXIMUM_ALLOWED_ACCESS,
2163 &sid, &domain_pol);
2164 if (!NT_STATUS_IS_OK(result)) {
2165 goto done;
2168 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2169 MAXIMUM_ALLOWED_ACCESS,
2170 alias_rid, &alias_pol);
2172 if (!NT_STATUS_IS_OK(result)) {
2173 return result;
2176 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2178 if (!NT_STATUS_IS_OK(result)) {
2179 return result;
2182 done:
2183 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2184 return result;
2187 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2188 const char *domain_name,
2189 struct cli_state *cli,
2190 struct rpc_pipe_client *pipe_hnd,
2191 TALLOC_CTX *mem_ctx,
2192 int argc,
2193 const char **argv)
2195 DOM_SID group_sid;
2196 enum lsa_SidType group_type;
2198 if (argc != 2) {
2199 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2200 return NT_STATUS_UNSUCCESSFUL;
2203 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2204 &group_sid, &group_type))) {
2205 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2206 return NT_STATUS_UNSUCCESSFUL;
2209 if (group_type == SID_NAME_DOM_GRP) {
2210 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2211 &group_sid, argv[1]);
2213 if (!NT_STATUS_IS_OK(result)) {
2214 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2215 argv[1], argv[0], nt_errstr(result));
2217 return result;
2220 if (group_type == SID_NAME_ALIAS) {
2221 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2222 &group_sid, argv[1]);
2224 if (!NT_STATUS_IS_OK(result)) {
2225 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2226 argv[1], argv[0], nt_errstr(result));
2228 return result;
2231 d_fprintf(stderr, "Can only add members to global or local groups "
2232 "which %s is not\n", argv[0]);
2234 return NT_STATUS_UNSUCCESSFUL;
2237 static int rpc_group_addmem(int argc, const char **argv)
2239 return run_rpc_command(NULL, PI_SAMR, 0,
2240 rpc_group_addmem_internals,
2241 argc, argv);
2244 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2245 TALLOC_CTX *mem_ctx,
2246 const DOM_SID *group_sid,
2247 const char *member)
2249 POLICY_HND connect_pol, domain_pol;
2250 NTSTATUS result;
2251 uint32 group_rid;
2252 POLICY_HND group_pol;
2254 uint32 num_rids;
2255 uint32 *rids = NULL;
2256 uint32 *rid_types = NULL;
2258 DOM_SID sid;
2260 sid_copy(&sid, group_sid);
2262 if (!sid_split_rid(&sid, &group_rid))
2263 return NT_STATUS_UNSUCCESSFUL;
2265 /* Get sam policy handle */
2266 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2267 &connect_pol);
2268 if (!NT_STATUS_IS_OK(result))
2269 return result;
2271 /* Get domain policy handle */
2272 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2273 MAXIMUM_ALLOWED_ACCESS,
2274 &sid, &domain_pol);
2275 if (!NT_STATUS_IS_OK(result))
2276 return result;
2278 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2279 1, &member,
2280 &num_rids, &rids, &rid_types);
2282 if (!NT_STATUS_IS_OK(result)) {
2283 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2284 goto done;
2287 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2288 MAXIMUM_ALLOWED_ACCESS,
2289 group_rid, &group_pol);
2291 if (!NT_STATUS_IS_OK(result))
2292 goto done;
2294 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2296 done:
2297 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2298 return result;
2301 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2302 TALLOC_CTX *mem_ctx,
2303 const DOM_SID *alias_sid,
2304 const char *member)
2306 POLICY_HND connect_pol, domain_pol;
2307 NTSTATUS result;
2308 uint32 alias_rid;
2309 POLICY_HND alias_pol;
2311 DOM_SID member_sid;
2312 enum lsa_SidType member_type;
2314 DOM_SID sid;
2316 sid_copy(&sid, alias_sid);
2318 if (!sid_split_rid(&sid, &alias_rid))
2319 return NT_STATUS_UNSUCCESSFUL;
2321 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2322 &member_sid, &member_type);
2324 if (!NT_STATUS_IS_OK(result)) {
2325 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2326 return result;
2329 /* Get sam policy handle */
2330 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2331 &connect_pol);
2332 if (!NT_STATUS_IS_OK(result)) {
2333 goto done;
2336 /* Get domain policy handle */
2337 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2338 MAXIMUM_ALLOWED_ACCESS,
2339 &sid, &domain_pol);
2340 if (!NT_STATUS_IS_OK(result)) {
2341 goto done;
2344 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2345 MAXIMUM_ALLOWED_ACCESS,
2346 alias_rid, &alias_pol);
2348 if (!NT_STATUS_IS_OK(result))
2349 return result;
2351 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2353 if (!NT_STATUS_IS_OK(result))
2354 return result;
2356 done:
2357 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2358 return result;
2361 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2362 const char *domain_name,
2363 struct cli_state *cli,
2364 struct rpc_pipe_client *pipe_hnd,
2365 TALLOC_CTX *mem_ctx,
2366 int argc,
2367 const char **argv)
2369 DOM_SID group_sid;
2370 enum lsa_SidType group_type;
2372 if (argc != 2) {
2373 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2374 return NT_STATUS_UNSUCCESSFUL;
2377 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2378 &group_sid, &group_type))) {
2379 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2380 return NT_STATUS_UNSUCCESSFUL;
2383 if (group_type == SID_NAME_DOM_GRP) {
2384 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2385 &group_sid, argv[1]);
2387 if (!NT_STATUS_IS_OK(result)) {
2388 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2389 argv[1], argv[0], nt_errstr(result));
2391 return result;
2394 if (group_type == SID_NAME_ALIAS) {
2395 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2396 &group_sid, argv[1]);
2398 if (!NT_STATUS_IS_OK(result)) {
2399 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2400 argv[1], argv[0], nt_errstr(result));
2402 return result;
2405 d_fprintf(stderr, "Can only delete members from global or local groups "
2406 "which %s is not\n", argv[0]);
2408 return NT_STATUS_UNSUCCESSFUL;
2411 static int rpc_group_delmem(int argc, const char **argv)
2413 return run_rpc_command(NULL, PI_SAMR, 0,
2414 rpc_group_delmem_internals,
2415 argc, argv);
2418 /**
2419 * List groups on a remote RPC server
2421 * All parameters are provided by the run_rpc_command function, except for
2422 * argc, argv which are passes through.
2424 * @param domain_sid The domain sid acquired from the remote server
2425 * @param cli A cli_state connected to the server.
2426 * @param mem_ctx Talloc context, destoyed on completion of the function.
2427 * @param argc Standard main() style argc
2428 * @param argv Standard main() style argv. Initial components are already
2429 * stripped
2431 * @return Normal NTSTATUS return.
2434 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2435 const char *domain_name,
2436 struct cli_state *cli,
2437 struct rpc_pipe_client *pipe_hnd,
2438 TALLOC_CTX *mem_ctx,
2439 int argc,
2440 const char **argv)
2442 POLICY_HND connect_pol, domain_pol;
2443 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2444 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2445 struct acct_info *groups;
2446 BOOL global = False;
2447 BOOL local = False;
2448 BOOL builtin = False;
2450 if (argc == 0) {
2451 global = True;
2452 local = True;
2453 builtin = True;
2456 for (i=0; i<argc; i++) {
2457 if (strequal(argv[i], "global"))
2458 global = True;
2460 if (strequal(argv[i], "local"))
2461 local = True;
2463 if (strequal(argv[i], "builtin"))
2464 builtin = True;
2467 /* Get sam policy handle */
2469 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2470 &connect_pol);
2471 if (!NT_STATUS_IS_OK(result)) {
2472 goto done;
2475 /* Get domain policy handle */
2477 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2478 MAXIMUM_ALLOWED_ACCESS,
2479 domain_sid, &domain_pol);
2480 if (!NT_STATUS_IS_OK(result)) {
2481 goto done;
2484 /* Query domain groups */
2485 if (opt_long_list_entries)
2486 d_printf("\nGroup name Comment"\
2487 "\n-----------------------------\n");
2488 do {
2489 SAM_DISPINFO_CTR ctr;
2490 SAM_DISPINFO_3 info3;
2491 uint32 max_size;
2493 ZERO_STRUCT(ctr);
2494 ZERO_STRUCT(info3);
2495 ctr.sam.info3 = &info3;
2497 if (!global) break;
2499 get_query_dispinfo_params(
2500 loop_count, &max_entries, &max_size);
2502 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2503 &start_idx, 3, &num_entries,
2504 max_entries, max_size, &ctr);
2506 if (!NT_STATUS_IS_OK(result) &&
2507 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2508 break;
2510 for (i = 0; i < num_entries; i++) {
2512 fstring group, desc;
2514 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2515 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2517 if (opt_long_list_entries)
2518 printf("%-21.21s %-50.50s\n",
2519 group, desc);
2520 else
2521 printf("%s\n", group);
2523 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2524 /* query domain aliases */
2525 start_idx = 0;
2526 do {
2527 if (!local) break;
2529 /* The max_size field in cli_samr_enum_als_groups is more like
2530 * an account_control field with indiviual bits what to
2531 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2532 * everything. I'm too lazy (sorry) to get this through to
2533 * rpc_parse/ etc. Volker */
2535 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2536 &start_idx, 0xffff,
2537 &groups, &num_entries);
2539 if (!NT_STATUS_IS_OK(result) &&
2540 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2541 break;
2543 for (i = 0; i < num_entries; i++) {
2545 char *description = NULL;
2547 if (opt_long_list_entries) {
2549 POLICY_HND alias_pol;
2550 ALIAS_INFO_CTR ctr;
2552 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2553 &domain_pol,
2554 0x8,
2555 groups[i].rid,
2556 &alias_pol))) &&
2557 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2558 &alias_pol, 3,
2559 &ctr))) &&
2560 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2561 &alias_pol)))) {
2562 description = unistr2_tdup(mem_ctx,
2563 ctr.alias.info3.description.string);
2567 if (description != NULL) {
2568 printf("%-21.21s %-50.50s\n",
2569 groups[i].acct_name,
2570 description);
2571 } else {
2572 printf("%s\n", groups[i].acct_name);
2575 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2576 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2577 /* Get builtin policy handle */
2579 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2580 MAXIMUM_ALLOWED_ACCESS,
2581 &global_sid_Builtin, &domain_pol);
2582 if (!NT_STATUS_IS_OK(result)) {
2583 goto done;
2585 /* query builtin aliases */
2586 start_idx = 0;
2587 do {
2588 if (!builtin) break;
2590 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2591 &start_idx, max_entries,
2592 &groups, &num_entries);
2594 if (!NT_STATUS_IS_OK(result) &&
2595 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2596 break;
2598 for (i = 0; i < num_entries; i++) {
2600 char *description = NULL;
2602 if (opt_long_list_entries) {
2604 POLICY_HND alias_pol;
2605 ALIAS_INFO_CTR ctr;
2607 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2608 &domain_pol,
2609 0x8,
2610 groups[i].rid,
2611 &alias_pol))) &&
2612 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2613 &alias_pol, 3,
2614 &ctr))) &&
2615 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2616 &alias_pol)))) {
2617 description = unistr2_tdup(mem_ctx,
2618 ctr.alias.info3.description.string);
2622 if (description != NULL) {
2623 printf("%-21.21s %-50.50s\n",
2624 groups[i].acct_name,
2625 description);
2626 } else {
2627 printf("%s\n", groups[i].acct_name);
2630 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2632 done:
2633 return result;
2636 static int rpc_group_list(int argc, const char **argv)
2638 return run_rpc_command(NULL, PI_SAMR, 0,
2639 rpc_group_list_internals,
2640 argc, argv);
2643 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2644 TALLOC_CTX *mem_ctx,
2645 const char *domain_name,
2646 const DOM_SID *domain_sid,
2647 POLICY_HND *domain_pol,
2648 uint32 rid)
2650 NTSTATUS result;
2651 POLICY_HND group_pol;
2652 uint32 num_members, *group_rids, *group_attrs;
2653 uint32 num_names;
2654 char **names;
2655 uint32 *name_types;
2656 int i;
2658 fstring sid_str;
2659 sid_to_string(sid_str, domain_sid);
2661 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2662 MAXIMUM_ALLOWED_ACCESS,
2663 rid, &group_pol);
2665 if (!NT_STATUS_IS_OK(result))
2666 return result;
2668 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2669 &num_members, &group_rids,
2670 &group_attrs);
2672 if (!NT_STATUS_IS_OK(result))
2673 return result;
2675 while (num_members > 0) {
2676 int this_time = 512;
2678 if (num_members < this_time)
2679 this_time = num_members;
2681 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2682 this_time, group_rids,
2683 &num_names, &names, &name_types);
2685 if (!NT_STATUS_IS_OK(result))
2686 return result;
2688 /* We only have users as members, but make the output
2689 the same as the output of alias members */
2691 for (i = 0; i < this_time; i++) {
2693 if (opt_long_list_entries) {
2694 printf("%s-%d %s\\%s %d\n", sid_str,
2695 group_rids[i], domain_name, names[i],
2696 SID_NAME_USER);
2697 } else {
2698 printf("%s\\%s\n", domain_name, names[i]);
2702 num_members -= this_time;
2703 group_rids += 512;
2706 return NT_STATUS_OK;
2709 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2710 TALLOC_CTX *mem_ctx,
2711 POLICY_HND *domain_pol,
2712 uint32 rid)
2714 NTSTATUS result;
2715 struct rpc_pipe_client *lsa_pipe;
2716 POLICY_HND alias_pol, lsa_pol;
2717 uint32 num_members;
2718 DOM_SID *alias_sids;
2719 char **domains;
2720 char **names;
2721 enum lsa_SidType *types;
2722 int i;
2724 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2725 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2727 if (!NT_STATUS_IS_OK(result))
2728 return result;
2730 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2731 &num_members, &alias_sids);
2733 if (!NT_STATUS_IS_OK(result)) {
2734 d_fprintf(stderr, "Couldn't list alias members\n");
2735 return result;
2738 if (num_members == 0) {
2739 return NT_STATUS_OK;
2742 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2743 if (!lsa_pipe) {
2744 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2745 nt_errstr(result) );
2746 return result;
2749 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2750 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2752 if (!NT_STATUS_IS_OK(result)) {
2753 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2754 cli_rpc_pipe_close(lsa_pipe);
2755 return result;
2758 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2759 alias_sids,
2760 &domains, &names, &types);
2762 if (!NT_STATUS_IS_OK(result) &&
2763 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2764 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2765 cli_rpc_pipe_close(lsa_pipe);
2766 return result;
2769 for (i = 0; i < num_members; i++) {
2770 fstring sid_str;
2771 sid_to_string(sid_str, &alias_sids[i]);
2773 if (opt_long_list_entries) {
2774 printf("%s %s\\%s %d\n", sid_str,
2775 domains[i] ? domains[i] : "*unknown*",
2776 names[i] ? names[i] : "*unknown*", types[i]);
2777 } else {
2778 if (domains[i])
2779 printf("%s\\%s\n", domains[i], names[i]);
2780 else
2781 printf("%s\n", sid_str);
2785 cli_rpc_pipe_close(lsa_pipe);
2786 return NT_STATUS_OK;
2789 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2790 const char *domain_name,
2791 struct cli_state *cli,
2792 struct rpc_pipe_client *pipe_hnd,
2793 TALLOC_CTX *mem_ctx,
2794 int argc,
2795 const char **argv)
2797 NTSTATUS result;
2798 POLICY_HND connect_pol, domain_pol;
2799 uint32 num_rids, *rids, *rid_types;
2801 /* Get sam policy handle */
2803 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2804 &connect_pol);
2806 if (!NT_STATUS_IS_OK(result))
2807 return result;
2809 /* Get domain policy handle */
2811 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2812 MAXIMUM_ALLOWED_ACCESS,
2813 domain_sid, &domain_pol);
2815 if (!NT_STATUS_IS_OK(result))
2816 return result;
2818 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2819 1, argv, &num_rids, &rids, &rid_types);
2821 if (!NT_STATUS_IS_OK(result)) {
2823 /* Ok, did not find it in the global sam, try with builtin */
2825 DOM_SID sid_Builtin;
2827 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2829 string_to_sid(&sid_Builtin, "S-1-5-32");
2831 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2832 MAXIMUM_ALLOWED_ACCESS,
2833 &sid_Builtin, &domain_pol);
2835 if (!NT_STATUS_IS_OK(result)) {
2836 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2837 return result;
2840 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2841 1, argv, &num_rids,
2842 &rids, &rid_types);
2844 if (!NT_STATUS_IS_OK(result)) {
2845 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2846 return result;
2850 if (num_rids != 1) {
2851 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2852 return result;
2855 if (rid_types[0] == SID_NAME_DOM_GRP) {
2856 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2857 domain_sid, &domain_pol,
2858 rids[0]);
2861 if (rid_types[0] == SID_NAME_ALIAS) {
2862 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2863 rids[0]);
2866 return NT_STATUS_NO_SUCH_GROUP;
2869 static int rpc_group_members(int argc, const char **argv)
2871 if (argc != 1) {
2872 return rpc_group_usage(argc, argv);
2875 return run_rpc_command(NULL, PI_SAMR, 0,
2876 rpc_group_members_internals,
2877 argc, argv);
2880 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2881 const char *domain_name,
2882 struct cli_state *cli,
2883 struct rpc_pipe_client *pipe_hnd,
2884 TALLOC_CTX *mem_ctx,
2885 int argc,
2886 const char **argv)
2888 NTSTATUS result;
2889 POLICY_HND connect_pol, domain_pol, group_pol;
2890 uint32 num_rids, *rids, *rid_types;
2891 GROUP_INFO_CTR ctr;
2893 if (argc != 2) {
2894 d_printf("Usage: 'net rpc group rename group newname'\n");
2895 return NT_STATUS_UNSUCCESSFUL;
2898 /* Get sam policy handle */
2900 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2901 &connect_pol);
2903 if (!NT_STATUS_IS_OK(result))
2904 return result;
2906 /* Get domain policy handle */
2908 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2909 MAXIMUM_ALLOWED_ACCESS,
2910 domain_sid, &domain_pol);
2912 if (!NT_STATUS_IS_OK(result))
2913 return result;
2915 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2916 1, argv, &num_rids, &rids, &rid_types);
2918 if (num_rids != 1) {
2919 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2920 return result;
2923 if (rid_types[0] != SID_NAME_DOM_GRP) {
2924 d_fprintf(stderr, "Can only rename domain groups\n");
2925 return NT_STATUS_UNSUCCESSFUL;
2928 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2929 MAXIMUM_ALLOWED_ACCESS,
2930 rids[0], &group_pol);
2932 if (!NT_STATUS_IS_OK(result))
2933 return result;
2935 ZERO_STRUCT(ctr);
2937 ctr.switch_value1 = 2;
2938 init_samr_group_info2(&ctr.group.info2, argv[1]);
2940 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2942 if (!NT_STATUS_IS_OK(result))
2943 return result;
2945 return NT_STATUS_NO_SUCH_GROUP;
2948 static int rpc_group_rename(int argc, const char **argv)
2950 if (argc != 2) {
2951 return rpc_group_usage(argc, argv);
2954 return run_rpc_command(NULL, PI_SAMR, 0,
2955 rpc_group_rename_internals,
2956 argc, argv);
2959 /**
2960 * 'net rpc group' entrypoint.
2961 * @param argc Standard main() style argc
2962 * @param argc Standard main() style argv. Initial components are already
2963 * stripped
2966 int net_rpc_group(int argc, const char **argv)
2968 struct functable func[] = {
2969 {"add", rpc_group_add},
2970 {"delete", rpc_group_delete},
2971 {"addmem", rpc_group_addmem},
2972 {"delmem", rpc_group_delmem},
2973 {"list", rpc_group_list},
2974 {"members", rpc_group_members},
2975 {"rename", rpc_group_rename},
2976 {NULL, NULL}
2979 if (argc == 0) {
2980 return run_rpc_command(NULL, PI_SAMR, 0,
2981 rpc_group_list_internals,
2982 argc, argv);
2985 return net_run_function(argc, argv, func, rpc_group_usage);
2988 /****************************************************************************/
2990 static int rpc_share_usage(int argc, const char **argv)
2992 return net_help_share(argc, argv);
2995 /**
2996 * Add a share on a remote RPC server
2998 * All parameters are provided by the run_rpc_command function, except for
2999 * argc, argv which are passes through.
3001 * @param domain_sid The domain sid acquired from the remote server
3002 * @param cli A cli_state connected to the server.
3003 * @param mem_ctx Talloc context, destoyed on completion of the function.
3004 * @param argc Standard main() style argc
3005 * @param argv Standard main() style argv. Initial components are already
3006 * stripped
3008 * @return Normal NTSTATUS return.
3010 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3011 const char *domain_name,
3012 struct cli_state *cli,
3013 struct rpc_pipe_client *pipe_hnd,
3014 TALLOC_CTX *mem_ctx,int argc,
3015 const char **argv)
3017 NTSTATUS result;
3018 char *sharename;
3019 char *path;
3020 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3021 uint32 num_users=0, perms=0;
3022 char *password=NULL; /* don't allow a share password */
3023 uint32 level = 2;
3024 uint32 parm_error;
3025 union srvsvc_NetShareInfo info;
3026 struct srvsvc_NetShareInfo2 info2;
3028 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3029 return NT_STATUS_NO_MEMORY;
3032 path = strchr(sharename, '=');
3033 if (!path)
3034 return NT_STATUS_UNSUCCESSFUL;
3035 *path++ = '\0';
3037 info.info2 = &info2;
3039 info2.type = type;
3040 info2.comment = opt_comment;
3041 info2.permissions = perms;
3042 info2.max_users = opt_maxusers;
3043 info2.current_users = num_users;
3044 info2.path = path;
3045 info2.password = password;
3046 info2.name = sharename;
3048 result = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx, NULL, level,
3049 info, &parm_error);
3050 return result;
3053 static int rpc_share_add(int argc, const char **argv)
3055 if ((argc < 1) || !strchr(argv[0], '=')) {
3056 DEBUG(1,("Sharename or path not specified on add\n"));
3057 return rpc_share_usage(argc, argv);
3059 return run_rpc_command(NULL, PI_SRVSVC, 0,
3060 rpc_share_add_internals,
3061 argc, argv);
3064 /**
3065 * Delete a share on a remote RPC server
3067 * All parameters are provided by the run_rpc_command function, except for
3068 * argc, argv which are passes through.
3070 * @param domain_sid The domain sid acquired from the remote server
3071 * @param cli A cli_state connected to the server.
3072 * @param mem_ctx Talloc context, destoyed on completion of the function.
3073 * @param argc Standard main() style argc
3074 * @param argv Standard main() style argv. Initial components are already
3075 * stripped
3077 * @return Normal NTSTATUS return.
3079 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3080 const char *domain_name,
3081 struct cli_state *cli,
3082 struct rpc_pipe_client *pipe_hnd,
3083 TALLOC_CTX *mem_ctx,
3084 int argc,
3085 const char **argv)
3087 NTSTATUS result;
3089 result = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx, NULL, argv[0], 0);
3090 return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3093 /**
3094 * Delete a share on a remote RPC server
3096 * @param domain_sid The domain sid acquired from the remote server
3097 * @param argc Standard main() style argc
3098 * @param argv Standard main() style argv. Initial components are already
3099 * stripped
3101 * @return A shell status integer (0 for success)
3103 static int rpc_share_delete(int argc, const char **argv)
3105 if (argc < 1) {
3106 DEBUG(1,("Sharename not specified on delete\n"));
3107 return rpc_share_usage(argc, argv);
3109 return run_rpc_command(NULL, PI_SRVSVC, 0,
3110 rpc_share_del_internals,
3111 argc, argv);
3115 * Formatted print of share info
3117 * @param info1 pointer to struct srvsvc_NetShareInfo1 to format
3120 static void display_share_info_1(struct srvsvc_NetShareInfo1 *info1)
3122 if (opt_long_list_entries) {
3123 d_printf("%-12s %-8.8s %-50s\n",
3124 info1->name, share_type[info1->type], info1->comment);
3125 } else {
3126 d_printf("%s\n", info1->name);
3131 static NTSTATUS get_share_info(struct rpc_pipe_client *pipe_hnd,
3132 TALLOC_CTX *mem_ctx,
3133 uint32 level,
3134 int argc,
3135 const char **argv,
3136 union srvsvc_NetShareCtr *ctr,
3137 uint32 *numentries)
3139 union srvsvc_NetShareInfo info;
3141 /* no specific share requested, enumerate all */
3142 if (argc == 0) {
3143 uint32 hnd = 0;
3145 return rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL, &level, ctr,
3146 0xffffffff, numentries, &hnd);
3149 /* request just one share */
3150 return rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx, NULL, argv[0], level, &info);
3153 /**
3154 * List shares on a remote RPC server
3156 * All parameters are provided by the run_rpc_command function, except for
3157 * argc, argv which are passes through.
3159 * @param domain_sid The domain sid acquired from the remote server
3160 * @param cli A cli_state connected to the server.
3161 * @param mem_ctx Talloc context, destoyed on completion of the function.
3162 * @param argc Standard main() style argc
3163 * @param argv Standard main() style argv. Initial components are already
3164 * stripped
3166 * @return Normal NTSTATUS return.
3169 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3170 const char *domain_name,
3171 struct cli_state *cli,
3172 struct rpc_pipe_client *pipe_hnd,
3173 TALLOC_CTX *mem_ctx,
3174 int argc,
3175 const char **argv)
3177 union srvsvc_NetShareCtr ctr;
3178 NTSTATUS result;
3179 uint32 i, level = 1;
3180 uint32 numentries;
3182 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr,
3183 &numentries);
3184 if (!NT_STATUS_IS_OK(result))
3185 goto done;
3187 /* Display results */
3189 if (opt_long_list_entries) {
3190 d_printf(
3191 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3192 "\nShare name Type Description\n"\
3193 "---------- ---- -----------\n");
3195 for (i = 0; i < numentries; i++)
3196 display_share_info_1(&ctr.ctr1->array[i]);
3197 done:
3198 return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3201 /***
3202 * 'net rpc share list' entrypoint.
3203 * @param argc Standard main() style argc
3204 * @param argv Standard main() style argv. Initial components are already
3205 * stripped
3207 static int rpc_share_list(int argc, const char **argv)
3209 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3212 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3214 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3215 d_printf("skipping [%s]: not a file share.\n", netname);
3216 return False;
3219 if (!cli_tdis(cli))
3220 return False;
3222 return True;
3225 static BOOL check_share_sanity(struct cli_state *cli, const char *netname, uint32 type)
3227 /* only support disk shares */
3228 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3229 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3230 return False;
3233 /* skip builtin shares */
3234 /* FIXME: should print$ be added too ? */
3235 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3236 strequal(netname,"global"))
3237 return False;
3239 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3240 printf("excluding [%s]\n", netname);
3241 return False;
3244 return check_share_availability(cli, netname);
3247 /**
3248 * Migrate shares from a remote RPC server to the local RPC srever
3250 * All parameters are provided by the run_rpc_command function, except for
3251 * argc, argv which are passes through.
3253 * @param domain_sid The domain sid acquired from the remote server
3254 * @param cli A cli_state connected to the server.
3255 * @param mem_ctx Talloc context, destoyed on completion of the function.
3256 * @param argc Standard main() style argc
3257 * @param argv Standard main() style argv. Initial components are already
3258 * stripped
3260 * @return Normal NTSTATUS return.
3263 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3264 const char *domain_name,
3265 struct cli_state *cli,
3266 struct rpc_pipe_client *pipe_hnd,
3267 TALLOC_CTX *mem_ctx,
3268 int argc,
3269 const char **argv)
3271 NTSTATUS result;
3272 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3273 union srvsvc_NetShareCtr ctr_src;
3274 uint32 i;
3275 struct rpc_pipe_client *srvsvc_pipe = NULL;
3276 struct cli_state *cli_dst = NULL;
3277 uint32 level = 502; /* includes secdesc */
3278 uint32 numentries;
3280 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src,
3281 &numentries);
3282 if (!NT_STATUS_IS_OK(result))
3283 goto done;
3285 /* connect destination PI_SRVSVC */
3286 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3287 if (!NT_STATUS_IS_OK(nt_status))
3288 return nt_status;
3291 for (i = 0; i < numentries; i++) {
3292 uint32 parm_error;
3293 union srvsvc_NetShareInfo info;
3295 /* reset error-code */
3296 nt_status = NT_STATUS_UNSUCCESSFUL;
3298 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name,
3299 ctr_src.ctr502->array[i].type))
3301 continue;
3303 /* finally add the share on the dst server */
3305 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3306 ctr_src.ctr502->array[i].name,
3307 ctr_src.ctr502->array[i].path,
3308 ctr_src.ctr502->array[i].comment);
3310 info.info502 = &ctr_src.ctr502->array[i];
3312 result = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx, NULL,
3313 502, info, &parm_error);
3315 if (NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_COLLISION)) {
3316 printf(" [%s] does already exist\n", ctr_src.ctr502->array[i].name);
3317 continue;
3320 if (!NT_STATUS_IS_OK(result)) {
3321 printf("cannot add share: %s\n", nt_errstr(result));
3322 goto done;
3327 nt_status = NT_STATUS_OK;
3329 done:
3330 if (cli_dst) {
3331 cli_shutdown(cli_dst);
3334 return nt_status;
3338 /**
3339 * Migrate shares from a rpc-server to another
3341 * @param argc Standard main() style argc
3342 * @param argv Standard main() style argv. Initial components are already
3343 * stripped
3345 * @return A shell status integer (0 for success)
3347 static int rpc_share_migrate_shares(int argc, const char **argv)
3350 if (!opt_host) {
3351 printf("no server to migrate\n");
3352 return -1;
3355 return run_rpc_command(NULL, PI_SRVSVC, 0,
3356 rpc_share_migrate_shares_internals,
3357 argc, argv);
3361 * Copy a file/dir
3363 * @param f file_info
3364 * @param mask current search mask
3365 * @param state arg-pointer
3368 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3370 static NTSTATUS nt_status;
3371 static struct copy_clistate *local_state;
3372 static fstring filename, new_mask;
3373 fstring dir;
3374 char *old_dir;
3376 local_state = (struct copy_clistate *)state;
3377 nt_status = NT_STATUS_UNSUCCESSFUL;
3379 if (strequal(f->name, ".") || strequal(f->name, ".."))
3380 return;
3382 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3384 /* DIRECTORY */
3385 if (f->mode & aDIR) {
3387 DEBUG(3,("got dir: %s\n", f->name));
3389 fstrcpy(dir, local_state->cwd);
3390 fstrcat(dir, "\\");
3391 fstrcat(dir, f->name);
3393 switch (net_mode_share)
3395 case NET_MODE_SHARE_MIGRATE:
3396 /* create that directory */
3397 nt_status = net_copy_file(local_state->mem_ctx,
3398 local_state->cli_share_src,
3399 local_state->cli_share_dst,
3400 dir, dir,
3401 opt_acls? True : False,
3402 opt_attrs? True : False,
3403 opt_timestamps? True : False,
3404 False);
3405 break;
3406 default:
3407 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3408 return;
3411 if (!NT_STATUS_IS_OK(nt_status))
3412 printf("could not handle dir %s: %s\n",
3413 dir, nt_errstr(nt_status));
3415 /* search below that directory */
3416 fstrcpy(new_mask, dir);
3417 fstrcat(new_mask, "\\*");
3419 old_dir = local_state->cwd;
3420 local_state->cwd = dir;
3421 if (!sync_files(local_state, new_mask))
3422 printf("could not handle files\n");
3423 local_state->cwd = old_dir;
3425 return;
3429 /* FILE */
3430 fstrcpy(filename, local_state->cwd);
3431 fstrcat(filename, "\\");
3432 fstrcat(filename, f->name);
3434 DEBUG(3,("got file: %s\n", filename));
3436 switch (net_mode_share)
3438 case NET_MODE_SHARE_MIGRATE:
3439 nt_status = net_copy_file(local_state->mem_ctx,
3440 local_state->cli_share_src,
3441 local_state->cli_share_dst,
3442 filename, filename,
3443 opt_acls? True : False,
3444 opt_attrs? True : False,
3445 opt_timestamps? True: False,
3446 True);
3447 break;
3448 default:
3449 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3450 return;
3453 if (!NT_STATUS_IS_OK(nt_status))
3454 printf("could not handle file %s: %s\n",
3455 filename, nt_errstr(nt_status));
3460 * sync files, can be called recursivly to list files
3461 * and then call copy_fn for each file
3463 * @param cp_clistate pointer to the copy_clistate we work with
3464 * @param mask the current search mask
3466 * @return Boolean result
3468 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3471 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3473 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3474 d_fprintf(stderr, "listing %s failed with error: %s\n",
3475 mask, cli_errstr(cp_clistate->cli_share_src));
3476 return False;
3479 return True;
3484 * Set the top level directory permissions before we do any further copies.
3485 * Should set up ACL inheritance.
3488 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3489 const char *sharename)
3491 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3493 switch (net_mode_share) {
3494 case NET_MODE_SHARE_MIGRATE:
3495 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3496 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3497 cp_clistate->cli_share_src,
3498 cp_clistate->cli_share_dst,
3499 "\\", "\\",
3500 opt_acls? True : False,
3501 opt_attrs? True : False,
3502 opt_timestamps? True: False,
3503 False);
3504 break;
3505 default:
3506 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3507 break;
3510 if (!NT_STATUS_IS_OK(nt_status)) {
3511 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3512 sharename, nt_errstr(nt_status));
3513 return False;
3516 return True;
3519 /**
3520 * Sync all files inside a remote share to another share (over smb)
3522 * All parameters are provided by the run_rpc_command function, except for
3523 * argc, argv which are passes through.
3525 * @param domain_sid The domain sid acquired from the remote server
3526 * @param cli A cli_state connected to the server.
3527 * @param mem_ctx Talloc context, destoyed on completion of the function.
3528 * @param argc Standard main() style argc
3529 * @param argv Standard main() style argv. Initial components are already
3530 * stripped
3532 * @return Normal NTSTATUS return.
3535 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3536 const char *domain_name,
3537 struct cli_state *cli,
3538 struct rpc_pipe_client *pipe_hnd,
3539 TALLOC_CTX *mem_ctx,
3540 int argc,
3541 const char **argv)
3543 NTSTATUS result;
3544 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3545 union srvsvc_NetShareCtr ctr_src;
3546 uint32 i;
3547 uint32 level = 502;
3548 struct copy_clistate cp_clistate;
3549 BOOL got_src_share = False;
3550 BOOL got_dst_share = False;
3551 pstring mask = "\\*";
3552 char *dst = NULL;
3553 uint32 numentries;
3555 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3557 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src,
3558 &numentries);
3560 if (!NT_STATUS_IS_OK(result))
3561 goto done;
3563 for (i = 0; i < numentries; i++) {
3564 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name,
3565 ctr_src.ctr502->array[i].type))
3566 continue;
3568 /* one might not want to mirror whole discs :) */
3569 if (strequal(ctr_src.ctr502->array[i].name, "print$") || ctr_src.ctr502->array[i].name[1] == '$') {
3570 d_printf("skipping [%s]: builtin/hidden share\n", ctr_src.ctr502->array[i].name);
3571 continue;
3574 switch (net_mode_share)
3576 case NET_MODE_SHARE_MIGRATE:
3577 printf("syncing");
3578 break;
3579 default:
3580 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3581 break;
3583 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3584 ctr_src.ctr502->array[i].name,
3585 opt_acls ? "including" : "without",
3586 opt_attrs ? "including" : "without",
3587 opt_timestamps ? "(preserving timestamps)" : "");
3589 cp_clistate.mem_ctx = mem_ctx;
3590 cp_clistate.cli_share_src = NULL;
3591 cp_clistate.cli_share_dst = NULL;
3592 cp_clistate.cwd = NULL;
3593 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3595 /* open share source */
3596 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3597 &cli->dest_ip, cli->desthost,
3598 ctr_src.ctr502->array[i].name, "A:");
3599 if (!NT_STATUS_IS_OK(nt_status))
3600 goto done;
3602 got_src_share = True;
3604 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3605 /* open share destination */
3606 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3607 NULL, dst, ctr_src.ctr502->array[i].name, "A:");
3608 if (!NT_STATUS_IS_OK(nt_status))
3609 goto done;
3611 got_dst_share = True;
3614 if (!copy_top_level_perms(&cp_clistate, ctr_src.ctr502->array[i].name)) {
3615 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", ctr_src.ctr502->array[i].name);
3616 nt_status = NT_STATUS_UNSUCCESSFUL;
3617 goto done;
3620 if (!sync_files(&cp_clistate, mask)) {
3621 d_fprintf(stderr, "could not handle files for share: %s\n",
3622 ctr_src.ctr502->array[i].name);
3623 nt_status = NT_STATUS_UNSUCCESSFUL;
3624 goto done;
3628 nt_status = NT_STATUS_OK;
3630 done:
3632 if (got_src_share)
3633 cli_shutdown(cp_clistate.cli_share_src);
3635 if (got_dst_share)
3636 cli_shutdown(cp_clistate.cli_share_dst);
3638 return nt_status;
3642 static int rpc_share_migrate_files(int argc, const char **argv)
3645 if (!opt_host) {
3646 printf("no server to migrate\n");
3647 return -1;
3650 return run_rpc_command(NULL, PI_SRVSVC, 0,
3651 rpc_share_migrate_files_internals,
3652 argc, argv);
3655 /**
3656 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3658 * All parameters are provided by the run_rpc_command function, except for
3659 * argc, argv which are passes through.
3661 * @param domain_sid The domain sid acquired from the remote server
3662 * @param cli A cli_state connected to the server.
3663 * @param mem_ctx Talloc context, destoyed on completion of the function.
3664 * @param argc Standard main() style argc
3665 * @param argv Standard main() style argv. Initial components are already
3666 * stripped
3668 * @return Normal NTSTATUS return.
3671 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3672 const char *domain_name,
3673 struct cli_state *cli,
3674 struct rpc_pipe_client *pipe_hnd,
3675 TALLOC_CTX *mem_ctx,
3676 int argc,
3677 const char **argv)
3679 NTSTATUS result;
3680 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3681 union srvsvc_NetShareCtr ctr_src;
3682 union srvsvc_NetShareInfo info;
3683 uint32 i;
3684 struct rpc_pipe_client *srvsvc_pipe = NULL;
3685 struct cli_state *cli_dst = NULL;
3686 uint32 level = 502; /* includes secdesc */
3687 uint32 numentries;
3688 uint32 parm_error;
3690 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src,
3691 &numentries);
3693 if (!NT_STATUS_IS_OK(result))
3694 goto done;
3696 /* connect destination PI_SRVSVC */
3697 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3698 if (!NT_STATUS_IS_OK(nt_status))
3699 return nt_status;
3702 for (i = 0; i < numentries; i++) {
3703 /* reset error-code */
3704 nt_status = NT_STATUS_UNSUCCESSFUL;
3706 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name, ctr_src.ctr502->array[i].type))
3707 continue;
3709 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3710 ctr_src.ctr502->array[i].name,
3711 ctr_src.ctr502->array[i].path,
3712 ctr_src.ctr502->array[i].comment);
3714 if (opt_verbose)
3715 display_sec_desc(ctr_src.ctr502->array[i].sd);
3717 /* init info */
3718 ZERO_STRUCT(info);
3720 /* finally modify the share on the dst server */
3721 result = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx, NULL,
3722 argv[0], level, info,
3723 &parm_error);
3725 if (!NT_STATUS_IS_OK(result)) {
3726 printf("cannot set share-acl: %s\n", nt_errstr(result));
3727 goto done;
3732 nt_status = NT_STATUS_OK;
3734 done:
3735 if (cli_dst) {
3736 cli_shutdown(cli_dst);
3739 return nt_status;
3743 /**
3744 * Migrate share-acls from a rpc-server to another
3746 * @param argc Standard main() style argc
3747 * @param argv Standard main() style argv. Initial components are already
3748 * stripped
3750 * @return A shell status integer (0 for success)
3752 static int rpc_share_migrate_security(int argc, const char **argv)
3755 if (!opt_host) {
3756 printf("no server to migrate\n");
3757 return -1;
3760 return run_rpc_command(NULL, PI_SRVSVC, 0,
3761 rpc_share_migrate_security_internals,
3762 argc, argv);
3765 /**
3766 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3767 * from one server to another
3769 * @param argc Standard main() style argc
3770 * @param argv Standard main() style argv. Initial components are already
3771 * stripped
3773 * @return A shell status integer (0 for success)
3776 static int rpc_share_migrate_all(int argc, const char **argv)
3778 int ret;
3780 if (!opt_host) {
3781 printf("no server to migrate\n");
3782 return -1;
3785 /* order is important. we don't want to be locked out by the share-acl
3786 * before copying files - gd */
3788 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3789 if (ret)
3790 return ret;
3792 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3793 if (ret)
3794 return ret;
3796 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3800 /**
3801 * 'net rpc share migrate' entrypoint.
3802 * @param argc Standard main() style argc
3803 * @param argv Standard main() style argv. Initial components are already
3804 * stripped
3806 static int rpc_share_migrate(int argc, const char **argv)
3809 struct functable func[] = {
3810 {"all", rpc_share_migrate_all},
3811 {"files", rpc_share_migrate_files},
3812 {"help", rpc_share_usage},
3813 {"security", rpc_share_migrate_security},
3814 {"shares", rpc_share_migrate_shares},
3815 {NULL, NULL}
3818 net_mode_share = NET_MODE_SHARE_MIGRATE;
3820 return net_run_function(argc, argv, func, rpc_share_usage);
3823 struct full_alias {
3824 DOM_SID sid;
3825 uint32 num_members;
3826 DOM_SID *members;
3829 static int num_server_aliases;
3830 static struct full_alias *server_aliases;
3833 * Add an alias to the static list.
3835 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3837 if (server_aliases == NULL)
3838 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3840 server_aliases[num_server_aliases] = *alias;
3841 num_server_aliases += 1;
3845 * For a specific domain on the server, fetch all the aliases
3846 * and their members. Add all of them to the server_aliases.
3849 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3850 TALLOC_CTX *mem_ctx,
3851 POLICY_HND *connect_pol,
3852 const DOM_SID *domain_sid)
3854 uint32 start_idx, max_entries, num_entries, i;
3855 struct acct_info *groups;
3856 NTSTATUS result;
3857 POLICY_HND domain_pol;
3859 /* Get domain policy handle */
3861 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3862 MAXIMUM_ALLOWED_ACCESS,
3863 domain_sid, &domain_pol);
3864 if (!NT_STATUS_IS_OK(result))
3865 return result;
3867 start_idx = 0;
3868 max_entries = 250;
3870 do {
3871 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
3872 &start_idx, max_entries,
3873 &groups, &num_entries);
3875 for (i = 0; i < num_entries; i++) {
3877 POLICY_HND alias_pol;
3878 struct full_alias alias;
3879 DOM_SID *members;
3880 int j;
3882 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
3883 MAXIMUM_ALLOWED_ACCESS,
3884 groups[i].rid,
3885 &alias_pol);
3886 if (!NT_STATUS_IS_OK(result))
3887 goto done;
3889 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
3890 &alias_pol,
3891 &alias.num_members,
3892 &members);
3893 if (!NT_STATUS_IS_OK(result))
3894 goto done;
3896 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
3897 if (!NT_STATUS_IS_OK(result))
3898 goto done;
3900 alias.members = NULL;
3902 if (alias.num_members > 0) {
3903 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3905 for (j = 0; j < alias.num_members; j++)
3906 sid_copy(&alias.members[j],
3907 &members[j]);
3910 sid_copy(&alias.sid, domain_sid);
3911 sid_append_rid(&alias.sid, groups[i].rid);
3913 push_alias(mem_ctx, &alias);
3915 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3917 result = NT_STATUS_OK;
3919 done:
3920 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
3922 return result;
3926 * Dump server_aliases as names for debugging purposes.
3929 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
3930 const char *domain_name,
3931 struct cli_state *cli,
3932 struct rpc_pipe_client *pipe_hnd,
3933 TALLOC_CTX *mem_ctx,
3934 int argc,
3935 const char **argv)
3937 int i;
3938 NTSTATUS result;
3939 POLICY_HND lsa_pol;
3941 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
3942 SEC_RIGHTS_MAXIMUM_ALLOWED,
3943 &lsa_pol);
3944 if (!NT_STATUS_IS_OK(result))
3945 return result;
3947 for (i=0; i<num_server_aliases; i++) {
3948 char **names;
3949 char **domains;
3950 enum lsa_SidType *types;
3951 int j;
3953 struct full_alias *alias = &server_aliases[i];
3955 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3956 &alias->sid,
3957 &domains, &names, &types);
3958 if (!NT_STATUS_IS_OK(result))
3959 continue;
3961 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3963 if (alias->num_members == 0) {
3964 DEBUG(1, ("\n"));
3965 continue;
3968 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3969 alias->num_members,
3970 alias->members,
3971 &domains, &names, &types);
3973 if (!NT_STATUS_IS_OK(result) &&
3974 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3975 continue;
3977 for (j=0; j<alias->num_members; j++)
3978 DEBUG(1, ("%s\\%s (%d); ",
3979 domains[j] ? domains[j] : "*unknown*",
3980 names[j] ? names[j] : "*unknown*",types[j]));
3981 DEBUG(1, ("\n"));
3984 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3986 return NT_STATUS_OK;
3990 * Fetch a list of all server aliases and their members into
3991 * server_aliases.
3994 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
3995 const char *domain_name,
3996 struct cli_state *cli,
3997 struct rpc_pipe_client *pipe_hnd,
3998 TALLOC_CTX *mem_ctx,
3999 int argc,
4000 const char **argv)
4002 NTSTATUS result;
4003 POLICY_HND connect_pol;
4005 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4006 &connect_pol);
4008 if (!NT_STATUS_IS_OK(result))
4009 goto done;
4011 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4012 &global_sid_Builtin);
4014 if (!NT_STATUS_IS_OK(result))
4015 goto done;
4017 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4018 domain_sid);
4020 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4021 done:
4022 return result;
4025 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4027 token->num_sids = 4;
4029 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4031 token->user_sids[0] = *user_sid;
4032 sid_copy(&token->user_sids[1], &global_sid_World);
4033 sid_copy(&token->user_sids[2], &global_sid_Network);
4034 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4037 static void free_user_token(NT_USER_TOKEN *token)
4039 SAFE_FREE(token->user_sids);
4042 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4044 int i;
4046 for (i=0; i<token->num_sids; i++) {
4047 if (sid_compare(sid, &token->user_sids[i]) == 0)
4048 return True;
4050 return False;
4053 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4055 if (is_sid_in_token(token, sid))
4056 return;
4058 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4059 if (!token->user_sids) {
4060 return;
4063 sid_copy(&token->user_sids[token->num_sids], sid);
4065 token->num_sids += 1;
4068 struct user_token {
4069 fstring name;
4070 NT_USER_TOKEN token;
4073 static void dump_user_token(struct user_token *token)
4075 int i;
4077 d_printf("%s\n", token->name);
4079 for (i=0; i<token->token.num_sids; i++) {
4080 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4084 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4086 int i;
4088 for (i=0; i<alias->num_members; i++) {
4089 if (sid_compare(sid, &alias->members[i]) == 0)
4090 return True;
4093 return False;
4096 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4098 int i;
4100 for (i=0; i<num_server_aliases; i++) {
4101 if (is_alias_member(&sid, &server_aliases[i]))
4102 add_sid_to_token(token, &server_aliases[i].sid);
4107 * We got a user token with all the SIDs we can know about without asking the
4108 * server directly. These are the user and domain group sids. All of these can
4109 * be members of aliases. So scan the list of aliases for each of the SIDs and
4110 * add them to the token.
4113 static void collect_alias_memberships(NT_USER_TOKEN *token)
4115 int num_global_sids = token->num_sids;
4116 int i;
4118 for (i=0; i<num_global_sids; i++) {
4119 collect_sid_memberships(token, token->user_sids[i]);
4123 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4125 struct winbindd_request request;
4126 struct winbindd_response response;
4127 fstring full_name;
4128 NSS_STATUS result;
4130 DOM_SID user_sid;
4132 int i;
4134 fstr_sprintf(full_name, "%s%c%s",
4135 domain, *lp_winbind_separator(), user);
4137 /* First let's find out the user sid */
4139 ZERO_STRUCT(request);
4140 ZERO_STRUCT(response);
4142 fstrcpy(request.data.name.dom_name, domain);
4143 fstrcpy(request.data.name.name, user);
4145 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4147 if (result != NSS_STATUS_SUCCESS) {
4148 DEBUG(1, ("winbind could not find %s\n", full_name));
4149 return False;
4152 if (response.data.sid.type != SID_NAME_USER) {
4153 DEBUG(1, ("%s is not a user\n", full_name));
4154 return False;
4157 string_to_sid(&user_sid, response.data.sid.sid);
4159 init_user_token(token, &user_sid);
4161 /* And now the groups winbind knows about */
4163 ZERO_STRUCT(response);
4165 fstrcpy(request.data.username, full_name);
4167 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4169 if (result != NSS_STATUS_SUCCESS) {
4170 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4171 return False;
4174 for (i = 0; i < response.data.num_entries; i++) {
4175 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4176 DOM_SID sid;
4178 struct winbindd_request sidrequest;
4179 struct winbindd_response sidresponse;
4181 ZERO_STRUCT(sidrequest);
4182 ZERO_STRUCT(sidresponse);
4184 sidrequest.data.gid = gid;
4186 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4187 &sidrequest, &sidresponse);
4189 if (result != NSS_STATUS_SUCCESS) {
4190 DEBUG(1, ("winbind could not find SID of gid %d\n",
4191 gid));
4192 return False;
4195 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4197 string_to_sid(&sid, sidresponse.data.sid.sid);
4198 add_sid_to_token(token, &sid);
4201 SAFE_FREE(response.extra_data.data);
4203 return True;
4207 * Get a list of all user tokens we want to look at
4210 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4212 struct winbindd_request request;
4213 struct winbindd_response response;
4214 const char *extra_data;
4215 fstring name;
4216 int i;
4217 struct user_token *result;
4219 if (lp_winbind_use_default_domain() &&
4220 (opt_target_workgroup == NULL)) {
4221 d_fprintf(stderr, "winbind use default domain = yes set, "
4222 "please specify a workgroup\n");
4223 return False;
4226 /* Send request to winbind daemon */
4228 ZERO_STRUCT(request);
4229 ZERO_STRUCT(response);
4231 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4232 NSS_STATUS_SUCCESS)
4233 return False;
4235 /* Look through extra data */
4237 if (!response.extra_data.data)
4238 return False;
4240 extra_data = (const char *)response.extra_data.data;
4241 *num_tokens = 0;
4243 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4244 *num_tokens += 1;
4247 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4249 if (result == NULL) {
4250 DEBUG(1, ("Could not malloc sid array\n"));
4251 return False;
4254 extra_data = (const char *)response.extra_data.data;
4255 i=0;
4257 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4259 fstring domain, user;
4260 char *p;
4262 fstrcpy(result[i].name, name);
4264 p = strchr(name, *lp_winbind_separator());
4266 DEBUG(3, ("%s\n", name));
4268 if (p == NULL) {
4269 fstrcpy(domain, opt_target_workgroup);
4270 fstrcpy(user, name);
4271 } else {
4272 *p++ = '\0';
4273 fstrcpy(domain, name);
4274 strupper_m(domain);
4275 fstrcpy(user, p);
4278 get_user_sids(domain, user, &(result[i].token));
4279 i+=1;
4282 SAFE_FREE(response.extra_data.data);
4284 *user_tokens = result;
4286 return True;
4289 static BOOL get_user_tokens_from_file(FILE *f,
4290 int *num_tokens,
4291 struct user_token **tokens)
4293 struct user_token *token = NULL;
4295 while (!feof(f)) {
4296 fstring line;
4298 if (fgets(line, sizeof(line)-1, f) == NULL) {
4299 return True;
4302 if (line[strlen(line)-1] == '\n')
4303 line[strlen(line)-1] = '\0';
4305 if (line[0] == ' ') {
4306 /* We have a SID */
4308 DOM_SID sid;
4309 string_to_sid(&sid, &line[1]);
4311 if (token == NULL) {
4312 DEBUG(0, ("File does not begin with username"));
4313 return False;
4316 add_sid_to_token(&token->token, &sid);
4317 continue;
4320 /* And a new user... */
4322 *num_tokens += 1;
4323 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4324 if (*tokens == NULL) {
4325 DEBUG(0, ("Could not realloc tokens\n"));
4326 return False;
4329 token = &((*tokens)[*num_tokens-1]);
4331 fstrcpy(token->name, line);
4332 token->token.num_sids = 0;
4333 token->token.user_sids = NULL;
4334 continue;
4337 return False;
4342 * Show the list of all users that have access to a share
4345 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4346 TALLOC_CTX *mem_ctx,
4347 const char *netname,
4348 int num_tokens,
4349 struct user_token *tokens)
4351 int fnum;
4352 SEC_DESC *share_sd = NULL;
4353 SEC_DESC *root_sd = NULL;
4354 struct cli_state *cli = pipe_hnd->cli;
4355 int i;
4356 union srvsvc_NetShareInfo info;
4357 NTSTATUS result;
4358 uint16 cnum;
4360 result = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx, NULL, netname,
4361 502, &info);
4363 if (!NT_STATUS_IS_OK(result)) {
4364 DEBUG(1, ("Coult not query secdesc for share %s\n",
4365 netname));
4366 return;
4369 share_sd = info.info502->sd;
4370 if (share_sd == NULL) {
4371 DEBUG(1, ("Got no secdesc for share %s\n",
4372 netname));
4375 cnum = cli->cnum;
4377 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4378 return;
4381 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4383 if (fnum != -1) {
4384 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4387 for (i=0; i<num_tokens; i++) {
4388 uint32 acc_granted;
4389 NTSTATUS status;
4391 if (share_sd != NULL) {
4392 if (!se_access_check(share_sd, &tokens[i].token,
4393 1, &acc_granted, &status)) {
4394 DEBUG(1, ("Could not check share_sd for "
4395 "user %s\n",
4396 tokens[i].name));
4397 continue;
4400 if (!NT_STATUS_IS_OK(status))
4401 continue;
4404 if (root_sd == NULL) {
4405 d_printf(" %s\n", tokens[i].name);
4406 continue;
4409 if (!se_access_check(root_sd, &tokens[i].token,
4410 1, &acc_granted, &status)) {
4411 DEBUG(1, ("Could not check root_sd for user %s\n",
4412 tokens[i].name));
4413 continue;
4416 if (!NT_STATUS_IS_OK(status))
4417 continue;
4419 d_printf(" %s\n", tokens[i].name);
4422 if (fnum != -1)
4423 cli_close(cli, fnum);
4424 cli_tdis(cli);
4425 cli->cnum = cnum;
4427 return;
4430 struct share_list {
4431 int num_shares;
4432 char **shares;
4435 static void collect_share(const char *name, uint32 m,
4436 const char *comment, void *state)
4438 struct share_list *share_list = (struct share_list *)state;
4440 if (m != STYPE_DISKTREE)
4441 return;
4443 share_list->num_shares += 1;
4444 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4445 if (!share_list->shares) {
4446 share_list->num_shares = 0;
4447 return;
4449 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4452 static void rpc_share_userlist_usage(void)
4454 return;
4457 /**
4458 * List shares on a remote RPC server, including the security descriptors
4460 * All parameters are provided by the run_rpc_command function, except for
4461 * argc, argv which are passes through.
4463 * @param domain_sid The domain sid acquired from the remote server
4464 * @param cli A cli_state connected to the server.
4465 * @param mem_ctx Talloc context, destoyed on completion of the function.
4466 * @param argc Standard main() style argc
4467 * @param argv Standard main() style argv. Initial components are already
4468 * stripped
4470 * @return Normal NTSTATUS return.
4473 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4474 const char *domain_name,
4475 struct cli_state *cli,
4476 struct rpc_pipe_client *pipe_hnd,
4477 TALLOC_CTX *mem_ctx,
4478 int argc,
4479 const char **argv)
4481 int ret;
4482 BOOL r;
4483 uint32 hnd;
4484 uint32 i;
4485 FILE *f;
4487 struct user_token *tokens = NULL;
4488 int num_tokens = 0;
4490 struct share_list share_list;
4492 if (argc > 1) {
4493 rpc_share_userlist_usage();
4494 return NT_STATUS_UNSUCCESSFUL;
4497 if (argc == 0) {
4498 f = stdin;
4499 } else {
4500 f = fopen(argv[0], "r");
4503 if (f == NULL) {
4504 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4505 return NT_STATUS_UNSUCCESSFUL;
4508 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4510 if (f != stdin)
4511 fclose(f);
4513 if (!r) {
4514 DEBUG(0, ("Could not read users from file\n"));
4515 return NT_STATUS_UNSUCCESSFUL;
4518 for (i=0; i<num_tokens; i++)
4519 collect_alias_memberships(&tokens[i].token);
4521 hnd = 0;
4522 share_list.num_shares = 0;
4523 share_list.shares = NULL;
4525 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4527 if (ret == -1) {
4528 DEBUG(0, ("Error returning browse list: %s\n",
4529 cli_errstr(cli)));
4530 goto done;
4533 for (i = 0; i < share_list.num_shares; i++) {
4534 char *netname = share_list.shares[i];
4536 if (netname[strlen(netname)-1] == '$')
4537 continue;
4539 d_printf("%s\n", netname);
4541 show_userlist(pipe_hnd, mem_ctx, netname,
4542 num_tokens, tokens);
4544 done:
4545 for (i=0; i<num_tokens; i++) {
4546 free_user_token(&tokens[i].token);
4548 SAFE_FREE(tokens);
4549 SAFE_FREE(share_list.shares);
4551 return NT_STATUS_OK;
4554 static int rpc_share_allowedusers(int argc, const char **argv)
4556 int result;
4558 result = run_rpc_command(NULL, PI_SAMR, 0,
4559 rpc_aliaslist_internals,
4560 argc, argv);
4561 if (result != 0)
4562 return result;
4564 result = run_rpc_command(NULL, PI_LSARPC, 0,
4565 rpc_aliaslist_dump,
4566 argc, argv);
4567 if (result != 0)
4568 return result;
4570 return run_rpc_command(NULL, PI_SRVSVC, 0,
4571 rpc_share_allowedusers_internals,
4572 argc, argv);
4575 int net_usersidlist(int argc, const char **argv)
4577 int num_tokens = 0;
4578 struct user_token *tokens = NULL;
4579 int i;
4581 if (argc != 0) {
4582 net_usersidlist_usage(argc, argv);
4583 return 0;
4586 if (!get_user_tokens(&num_tokens, &tokens)) {
4587 DEBUG(0, ("Could not get the user/sid list\n"));
4588 return 0;
4591 for (i=0; i<num_tokens; i++) {
4592 dump_user_token(&tokens[i]);
4593 free_user_token(&tokens[i].token);
4596 SAFE_FREE(tokens);
4597 return 1;
4600 int net_usersidlist_usage(int argc, const char **argv)
4602 d_printf("net usersidlist\n"
4603 "\tprints out a list of all users the running winbind knows\n"
4604 "\tabout, together with all their SIDs. This is used as\n"
4605 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4607 net_common_flags_usage(argc, argv);
4608 return -1;
4611 /**
4612 * 'net rpc share' entrypoint.
4613 * @param argc Standard main() style argc
4614 * @param argv Standard main() style argv. Initial components are already
4615 * stripped
4618 int net_rpc_share(int argc, const char **argv)
4620 struct functable func[] = {
4621 {"add", rpc_share_add},
4622 {"delete", rpc_share_delete},
4623 {"allowedusers", rpc_share_allowedusers},
4624 {"migrate", rpc_share_migrate},
4625 {"list", rpc_share_list},
4626 {NULL, NULL}
4629 if (argc == 0)
4630 return run_rpc_command(NULL, PI_SRVSVC, 0,
4631 rpc_share_list_internals,
4632 argc, argv);
4634 return net_run_function(argc, argv, func, rpc_share_usage);
4637 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4638 struct rpc_sh_ctx *ctx,
4639 struct rpc_pipe_client *pipe_hnd,
4640 int argc, const char **argv)
4642 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4643 ctx->cli, pipe_hnd, mem_ctx,
4644 argc, argv);
4647 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4648 struct rpc_sh_ctx *ctx,
4649 struct rpc_pipe_client *pipe_hnd,
4650 int argc, const char **argv)
4652 union srvsvc_NetShareInfo info;
4653 struct srvsvc_NetShareInfo2 info2;
4654 NTSTATUS result;
4655 uint32 parm_error;
4657 if ((argc < 2) || (argc > 3)) {
4658 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4659 ctx->whoami);
4660 return NT_STATUS_INVALID_PARAMETER;
4663 info.info2 = &info2;
4664 info2.name = argv[0];
4665 info2.type = STYPE_DISKTREE;
4666 info2.comment = (argc == 3) ? argv[2] : "";
4668 result = rpccli_srvsvc_NetShareAdd(
4669 pipe_hnd, mem_ctx, NULL, 2, info, &parm_error);
4671 return result;
4674 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4675 struct rpc_sh_ctx *ctx,
4676 struct rpc_pipe_client *pipe_hnd,
4677 int argc, const char **argv)
4679 NTSTATUS result;
4681 if (argc != 1) {
4682 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4683 return NT_STATUS_INVALID_PARAMETER;
4686 result = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx, NULL, argv[0], 0);
4687 return result;
4690 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4691 struct rpc_sh_ctx *ctx,
4692 struct rpc_pipe_client *pipe_hnd,
4693 int argc, const char **argv)
4695 union srvsvc_NetShareInfo info;
4696 NTSTATUS result;
4698 if (argc != 1) {
4699 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4700 return NT_STATUS_INVALID_PARAMETER;
4703 result = rpccli_srvsvc_NetShareGetInfo(
4704 pipe_hnd, mem_ctx, NULL, argv[0], 2, &info);
4705 if (!NT_STATUS_IS_OK(result)) {
4706 goto done;
4709 d_printf("Name: %s\n", info.info2->name);
4710 d_printf("Comment: %s\n", info.info2->comment);
4711 d_printf("Path: %s\n", info.info2->path);
4712 d_printf("Password: %s\n", info.info2->password);
4714 done:
4715 return result;
4718 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4719 struct rpc_sh_ctx *ctx)
4721 static struct rpc_sh_cmd cmds[] = {
4723 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4724 "List available shares" },
4726 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4727 "Add a share" },
4729 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4730 "Delete a share" },
4732 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4733 "Get information about a share" },
4735 { NULL, NULL, 0, NULL, NULL }
4738 return cmds;
4741 /****************************************************************************/
4743 static int rpc_file_usage(int argc, const char **argv)
4745 return net_help_file(argc, argv);
4748 /**
4749 * Close a file on a remote RPC server
4751 * All parameters are provided by the run_rpc_command function, except for
4752 * argc, argv which are passes through.
4754 * @param domain_sid The domain sid acquired from the remote server
4755 * @param cli A cli_state connected to the server.
4756 * @param mem_ctx Talloc context, destoyed on completion of the function.
4757 * @param argc Standard main() style argc
4758 * @param argv Standard main() style argv. Initial components are already
4759 * stripped
4761 * @return Normal NTSTATUS return.
4763 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4764 const char *domain_name,
4765 struct cli_state *cli,
4766 struct rpc_pipe_client *pipe_hnd,
4767 TALLOC_CTX *mem_ctx,
4768 int argc,
4769 const char **argv)
4771 NTSTATUS result;
4772 result = rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx, NULL, atoi(argv[0]));
4773 return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4776 /**
4777 * Close a file on a remote RPC server
4779 * @param argc Standard main() style argc
4780 * @param argv Standard main() style argv. Initial components are already
4781 * stripped
4783 * @return A shell status integer (0 for success)
4785 static int rpc_file_close(int argc, const char **argv)
4787 if (argc < 1) {
4788 DEBUG(1, ("No fileid given on close\n"));
4789 return(rpc_file_usage(argc, argv));
4792 return run_rpc_command(NULL, PI_SRVSVC, 0,
4793 rpc_file_close_internals,
4794 argc, argv);
4797 /**
4798 * Formatted print of open file info
4800 * @param info3 FILE_INFO_3 contents
4801 * @param str3 strings for FILE_INFO_3
4804 static void display_file_info_3( struct srvsvc_NetFileInfo3 *info3 )
4806 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4807 info3->fid, info3->user, info3->permissions, info3->num_locks, info3->path);
4810 /**
4811 * List open files on a remote RPC server
4813 * All parameters are provided by the run_rpc_command function, except for
4814 * argc, argv which are passes through.
4816 * @param domain_sid The domain sid acquired from the remote server
4817 * @param cli A cli_state connected to the server.
4818 * @param mem_ctx Talloc context, destoyed on completion of the function.
4819 * @param argc Standard main() style argc
4820 * @param argv Standard main() style argv. Initial components are already
4821 * stripped
4823 * @return Normal NTSTATUS return.
4826 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4827 const char *domain_name,
4828 struct cli_state *cli,
4829 struct rpc_pipe_client *pipe_hnd,
4830 TALLOC_CTX *mem_ctx,
4831 int argc,
4832 const char **argv)
4834 union srvsvc_NetFileCtr ctr;
4835 NTSTATUS result;
4836 uint32 hnd;
4837 uint32 preferred_len = 0xffffffff, i;
4838 const char *username=NULL;
4839 uint32 level = 3;
4840 uint32 numentries;
4842 hnd = 0;
4844 /* if argc > 0, must be user command */
4845 if (argc > 0)
4846 username = smb_xstrdup(argv[0]);
4848 result = rpccli_srvsvc_NetFileEnum(pipe_hnd,
4849 mem_ctx, NULL, NULL, username, &level, &ctr, preferred_len, &numentries, &hnd);
4851 if (!NT_STATUS_IS_OK(result))
4852 goto done;
4854 /* Display results */
4856 d_printf(
4857 "\nEnumerating open files on remote server:\n\n"\
4858 "\nFileId Opened by Perms Locks Path"\
4859 "\n------ --------- ----- ----- ---- \n");
4860 for (i = 0; i < numentries; i++)
4861 display_file_info_3(&ctr.ctr3->array[i]);
4862 done:
4863 return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4866 /**
4867 * List files for a user on a remote RPC server
4869 * @param argc Standard main() style argc
4870 * @param argv Standard main() style argv. Initial components are already
4871 * stripped
4873 * @return A shell status integer (0 for success)
4876 static int rpc_file_user(int argc, const char **argv)
4878 if (argc < 1) {
4879 DEBUG(1, ("No username given\n"));
4880 return(rpc_file_usage(argc, argv));
4883 return run_rpc_command(NULL, PI_SRVSVC, 0,
4884 rpc_file_list_internals,
4885 argc, argv);
4888 /**
4889 * 'net rpc file' entrypoint.
4890 * @param argc Standard main() style argc
4891 * @param argv Standard main() style argv. Initial components are already
4892 * stripped
4895 int net_rpc_file(int argc, const char **argv)
4897 struct functable func[] = {
4898 {"close", rpc_file_close},
4899 {"user", rpc_file_user},
4900 #if 0
4901 {"info", rpc_file_info},
4902 #endif
4903 {NULL, NULL}
4906 if (argc == 0)
4907 return run_rpc_command(NULL, PI_SRVSVC, 0,
4908 rpc_file_list_internals,
4909 argc, argv);
4911 return net_run_function(argc, argv, func, rpc_file_usage);
4914 /**
4915 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4917 * All parameters are provided by the run_rpc_command function, except for
4918 * argc, argv which are passed through.
4920 * @param domain_sid The domain sid aquired from the remote server
4921 * @param cli A cli_state connected to the server.
4922 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4923 * @param argc Standard main() style argc
4924 * @param argv Standard main() style argv. Initial components are already
4925 * stripped
4927 * @return Normal NTSTATUS return.
4930 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4931 const char *domain_name,
4932 struct cli_state *cli,
4933 struct rpc_pipe_client *pipe_hnd,
4934 TALLOC_CTX *mem_ctx,
4935 int argc,
4936 const char **argv)
4938 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4940 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL);
4942 if (NT_STATUS_IS_OK(result)) {
4943 d_printf("\nShutdown successfully aborted\n");
4944 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4945 } else
4946 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4948 return result;
4951 /**
4952 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4954 * All parameters are provided by the run_rpc_command function, except for
4955 * argc, argv which are passed through.
4957 * @param domain_sid The domain sid aquired from the remote server
4958 * @param cli A cli_state connected to the server.
4959 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4960 * @param argc Standard main() style argc
4961 * @param argv Standard main() style argv. Initial components are already
4962 * stripped
4964 * @return Normal NTSTATUS return.
4967 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
4968 const char *domain_name,
4969 struct cli_state *cli,
4970 struct rpc_pipe_client *pipe_hnd,
4971 TALLOC_CTX *mem_ctx,
4972 int argc,
4973 const char **argv)
4975 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4977 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL);
4979 if (NT_STATUS_IS_OK(result)) {
4980 d_printf("\nShutdown successfully aborted\n");
4981 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4982 } else
4983 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4985 return result;
4988 /**
4989 * ABORT the Shut down of a remote RPC server
4991 * @param argc Standard main() style argc
4992 * @param argv Standard main() style argv. Initial components are already
4993 * stripped
4995 * @return A shell status integer (0 for success)
4998 static int rpc_shutdown_abort(int argc, const char **argv)
5000 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5001 rpc_shutdown_abort_internals,
5002 argc, argv);
5004 if (rc == 0)
5005 return rc;
5007 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5009 return run_rpc_command(NULL, PI_WINREG, 0,
5010 rpc_reg_shutdown_abort_internals,
5011 argc, argv);
5014 /**
5015 * Shut down a remote RPC Server via initshutdown pipe
5017 * All parameters are provided by the run_rpc_command function, except for
5018 * argc, argv which are passes through.
5020 * @param domain_sid The domain sid aquired from the remote server
5021 * @param cli A cli_state connected to the server.
5022 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5023 * @param argc Standard main() style argc
5024 * @param argc Standard main() style argv. Initial components are already
5025 * stripped
5027 * @return Normal NTSTATUS return.
5030 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5031 const char *domain_name,
5032 struct cli_state *cli,
5033 struct rpc_pipe_client *pipe_hnd,
5034 TALLOC_CTX *mem_ctx,
5035 int argc,
5036 const char **argv)
5038 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5039 const char *msg = "This machine will be shutdown shortly";
5040 uint32 timeout = 20;
5041 struct initshutdown_String msg_string;
5042 struct initshutdown_String_sub s;
5044 if (opt_comment) {
5045 msg = opt_comment;
5047 if (opt_timeout) {
5048 timeout = opt_timeout;
5051 s.name = msg;
5052 msg_string.name = &s;
5054 /* create an entry */
5055 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5056 &msg_string, timeout, opt_force, opt_reboot);
5058 if (NT_STATUS_IS_OK(result)) {
5059 d_printf("\nShutdown of remote machine succeeded\n");
5060 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5061 } else {
5062 DEBUG(1,("Shutdown of remote machine failed!\n"));
5064 return result;
5067 /**
5068 * Shut down a remote RPC Server via winreg pipe
5070 * All parameters are provided by the run_rpc_command function, except for
5071 * argc, argv which are passes through.
5073 * @param domain_sid The domain sid aquired from the remote server
5074 * @param cli A cli_state connected to the server.
5075 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5076 * @param argc Standard main() style argc
5077 * @param argc Standard main() style argv. Initial components are already
5078 * stripped
5080 * @return Normal NTSTATUS return.
5083 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5084 const char *domain_name,
5085 struct cli_state *cli,
5086 struct rpc_pipe_client *pipe_hnd,
5087 TALLOC_CTX *mem_ctx,
5088 int argc,
5089 const char **argv)
5091 const char *msg = "This machine will be shutdown shortly";
5092 uint32 timeout = 20;
5093 struct initshutdown_String msg_string;
5094 struct initshutdown_String_sub s;
5095 NTSTATUS result;
5097 if (opt_comment) {
5098 msg = opt_comment;
5100 s.name = msg;
5101 msg_string.name = &s;
5103 if (opt_timeout) {
5104 timeout = opt_timeout;
5107 /* create an entry */
5108 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5109 &msg_string, timeout, opt_force, opt_reboot);
5111 if (NT_STATUS_IS_OK(result)) {
5112 d_printf("\nShutdown of remote machine succeeded\n");
5113 } else {
5114 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5115 if ( W_ERROR_EQUAL(ntstatus_to_werror(result),WERR_MACHINE_LOCKED) )
5116 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5117 else
5118 d_fprintf(stderr, "\nresult was: %s\n", nt_errstr(result));
5121 return result;
5124 /**
5125 * Shut down a remote RPC server
5127 * @param argc Standard main() style argc
5128 * @param argc Standard main() style argv. Initial components are already
5129 * stripped
5131 * @return A shell status integer (0 for success)
5134 static int rpc_shutdown(int argc, const char **argv)
5136 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5137 rpc_init_shutdown_internals,
5138 argc, argv);
5140 if (rc) {
5141 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5142 rc = run_rpc_command(NULL, PI_WINREG, 0,
5143 rpc_reg_shutdown_internals, argc, argv);
5146 return rc;
5149 /***************************************************************************
5150 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5152 ***************************************************************************/
5155 * Add interdomain trust account to the RPC server.
5156 * All parameters (except for argc and argv) are passed by run_rpc_command
5157 * function.
5159 * @param domain_sid The domain sid acquired from the server
5160 * @param cli A cli_state connected to the server.
5161 * @param mem_ctx Talloc context, destoyed on completion of the function.
5162 * @param argc Standard main() style argc
5163 * @param argc Standard main() style argv. Initial components are already
5164 * stripped
5166 * @return normal NTSTATUS return code
5169 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5170 const char *domain_name,
5171 struct cli_state *cli,
5172 struct rpc_pipe_client *pipe_hnd,
5173 TALLOC_CTX *mem_ctx,
5174 int argc,
5175 const char **argv)
5177 POLICY_HND connect_pol, domain_pol, user_pol;
5178 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5179 char *acct_name;
5180 uint32 acb_info;
5181 uint32 unknown, user_rid;
5183 if (argc != 2) {
5184 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5185 return NT_STATUS_INVALID_PARAMETER;
5189 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5192 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5193 return NT_STATUS_NO_MEMORY;
5196 strupper_m(acct_name);
5198 /* Get samr policy handle */
5199 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5200 &connect_pol);
5201 if (!NT_STATUS_IS_OK(result)) {
5202 goto done;
5205 /* Get domain policy handle */
5206 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5207 MAXIMUM_ALLOWED_ACCESS,
5208 domain_sid, &domain_pol);
5209 if (!NT_STATUS_IS_OK(result)) {
5210 goto done;
5213 /* Create trusting domain's account */
5214 acb_info = ACB_NORMAL;
5215 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5216 mimir: yes, most probably it is */
5218 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5219 acct_name, acb_info, unknown,
5220 &user_pol, &user_rid);
5221 if (!NT_STATUS_IS_OK(result)) {
5222 goto done;
5226 SAM_USERINFO_CTR ctr;
5227 SAM_USER_INFO_23 p23;
5228 NTTIME notime;
5229 char nostr[] = "";
5230 LOGON_HRS hrs;
5231 uchar pwbuf[516];
5233 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5235 ZERO_STRUCT(ctr);
5236 ZERO_STRUCT(p23);
5237 ZERO_STRUCT(notime);
5238 hrs.max_len = 1260;
5239 hrs.offset = 0;
5240 hrs.len = 21;
5241 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5242 acb_info = ACB_DOMTRUST;
5244 init_sam_user_info23A(&p23, &notime, &notime, &notime,
5245 &notime, &notime, &notime,
5246 nostr, nostr, nostr, nostr, nostr,
5247 nostr, nostr, nostr, nostr, nostr,
5248 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5249 0, 0, (char *)pwbuf);
5250 ctr.switch_value = 23;
5251 ctr.info.id23 = &p23;
5252 p23.passmustchange = 0;
5254 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5255 &cli->user_session_key, &ctr);
5257 if (!NT_STATUS_IS_OK(result)) {
5258 DEBUG(0,("Could not set trust account password: %s\n",
5259 nt_errstr(result)));
5260 goto done;
5264 done:
5265 SAFE_FREE(acct_name);
5266 return result;
5270 * Create interdomain trust account for a remote domain.
5272 * @param argc standard argc
5273 * @param argv standard argv without initial components
5275 * @return Integer status (0 means success)
5278 static int rpc_trustdom_add(int argc, const char **argv)
5280 if (argc > 0) {
5281 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5282 argc, argv);
5283 } else {
5284 d_printf("Usage: net rpc trustdom add <domain>\n");
5285 return -1;
5291 * Remove interdomain trust account from the RPC server.
5292 * All parameters (except for argc and argv) are passed by run_rpc_command
5293 * function.
5295 * @param domain_sid The domain sid acquired from the server
5296 * @param cli A cli_state connected to the server.
5297 * @param mem_ctx Talloc context, destoyed on completion of the function.
5298 * @param argc Standard main() style argc
5299 * @param argc Standard main() style argv. Initial components are already
5300 * stripped
5302 * @return normal NTSTATUS return code
5305 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5306 const char *domain_name,
5307 struct cli_state *cli,
5308 struct rpc_pipe_client *pipe_hnd,
5309 TALLOC_CTX *mem_ctx,
5310 int argc,
5311 const char **argv)
5313 POLICY_HND connect_pol, domain_pol, user_pol;
5314 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5315 char *acct_name;
5316 const char **names;
5317 DOM_SID trust_acct_sid;
5318 uint32 *user_rids, num_rids, *name_types;
5319 uint32 flags = 0x000003e8; /* Unknown */
5321 if (argc != 1) {
5322 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5323 return NT_STATUS_INVALID_PARAMETER;
5327 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5329 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5331 if (acct_name == NULL)
5332 return NT_STATUS_NO_MEMORY;
5334 strupper_m(acct_name);
5336 if ((names = TALLOC_ARRAY(mem_ctx, const char *, 1)) == NULL) {
5337 return NT_STATUS_NO_MEMORY;
5339 names[0] = acct_name;
5342 /* Get samr policy handle */
5343 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5344 &connect_pol);
5345 if (!NT_STATUS_IS_OK(result)) {
5346 goto done;
5349 /* Get domain policy handle */
5350 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5351 MAXIMUM_ALLOWED_ACCESS,
5352 domain_sid, &domain_pol);
5353 if (!NT_STATUS_IS_OK(result)) {
5354 goto done;
5357 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5358 names, &num_rids,
5359 &user_rids, &name_types);
5361 if (!NT_STATUS_IS_OK(result)) {
5362 goto done;
5365 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5366 MAXIMUM_ALLOWED_ACCESS,
5367 user_rids[0], &user_pol);
5369 if (!NT_STATUS_IS_OK(result)) {
5370 goto done;
5373 /* append the rid to the domain sid */
5374 sid_copy(&trust_acct_sid, domain_sid);
5375 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5376 goto done;
5379 /* remove the sid */
5381 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5382 &trust_acct_sid);
5384 if (!NT_STATUS_IS_OK(result)) {
5385 goto done;
5388 /* Delete user */
5390 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5392 if (!NT_STATUS_IS_OK(result)) {
5393 goto done;
5396 if (!NT_STATUS_IS_OK(result)) {
5397 DEBUG(0,("Could not set trust account password: %s\n",
5398 nt_errstr(result)));
5399 goto done;
5402 done:
5403 return result;
5407 * Delete interdomain trust account for a remote domain.
5409 * @param argc standard argc
5410 * @param argv standard argv without initial components
5412 * @return Integer status (0 means success)
5415 static int rpc_trustdom_del(int argc, const char **argv)
5417 if (argc > 0) {
5418 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5419 argc, argv);
5420 } else {
5421 d_printf("Usage: net rpc trustdom del <domain>\n");
5422 return -1;
5428 * Establish trust relationship to a trusting domain.
5429 * Interdomain account must already be created on remote PDC.
5431 * @param argc standard argc
5432 * @param argv standard argv without initial components
5434 * @return Integer status (0 means success)
5437 static int rpc_trustdom_establish(int argc, const char **argv)
5439 struct cli_state *cli = NULL;
5440 struct in_addr server_ip;
5441 struct rpc_pipe_client *pipe_hnd = NULL;
5442 POLICY_HND connect_hnd;
5443 TALLOC_CTX *mem_ctx;
5444 NTSTATUS nt_status;
5445 DOM_SID *domain_sid;
5447 char* domain_name;
5448 char* domain_name_pol;
5449 char* acct_name;
5450 fstring pdc_name;
5453 * Connect to \\server\ipc$ as 'our domain' account with password
5456 if (argc != 1) {
5457 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5458 return -1;
5461 domain_name = smb_xstrdup(argv[0]);
5462 strupper_m(domain_name);
5464 /* account name used at first is our domain's name with '$' */
5465 asprintf(&acct_name, "%s$", lp_workgroup());
5466 strupper_m(acct_name);
5469 * opt_workgroup will be used by connection functions further,
5470 * hence it should be set to remote domain name instead of ours
5472 if (opt_workgroup) {
5473 opt_workgroup = smb_xstrdup(domain_name);
5476 opt_user_name = acct_name;
5478 /* find the domain controller */
5479 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5480 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5481 return -1;
5484 /* connect to ipc$ as username/password */
5485 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5486 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5488 /* Is it trusting domain account for sure ? */
5489 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5490 nt_errstr(nt_status)));
5491 return -1;
5494 /* store who we connected to */
5496 saf_store( domain_name, pdc_name );
5499 * Connect to \\server\ipc$ again (this time anonymously)
5502 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5504 if (NT_STATUS_IS_ERR(nt_status)) {
5505 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5506 domain_name, nt_errstr(nt_status)));
5510 * Use NetServerEnum2 to make sure we're talking to a proper server
5513 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5514 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5515 for domain %s\n", domain_name));
5518 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5519 "domain %s", domain_name))) {
5520 DEBUG(0, ("talloc_init() failed\n"));
5521 cli_shutdown(cli);
5522 return -1;
5526 * Call LsaOpenPolicy and LsaQueryInfo
5529 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5530 if (!pipe_hnd) {
5531 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5532 cli_shutdown(cli);
5533 return -1;
5536 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5537 &connect_hnd);
5538 if (NT_STATUS_IS_ERR(nt_status)) {
5539 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5540 nt_errstr(nt_status)));
5541 cli_shutdown(cli);
5542 return -1;
5545 /* Querying info level 5 */
5547 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5548 5 /* info level */,
5549 &domain_name_pol, &domain_sid);
5550 if (NT_STATUS_IS_ERR(nt_status)) {
5551 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5552 nt_errstr(nt_status)));
5553 cli_shutdown(cli);
5554 return -1;
5557 /* There should be actually query info level 3 (following nt serv behaviour),
5558 but I still don't know if it's _really_ necessary */
5561 * Store the password in secrets db
5564 if (!secrets_store_trusted_domain_password(domain_name,
5565 opt_password,
5566 domain_sid)) {
5567 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5568 cli_shutdown(cli);
5569 return -1;
5573 * Close the pipes and clean up
5576 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5577 if (NT_STATUS_IS_ERR(nt_status)) {
5578 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5579 nt_errstr(nt_status)));
5580 cli_shutdown(cli);
5581 return -1;
5584 cli_shutdown(cli);
5586 talloc_destroy(mem_ctx);
5588 d_printf("Trust to domain %s established\n", domain_name);
5589 return 0;
5593 * Revoke trust relationship to the remote domain
5595 * @param argc standard argc
5596 * @param argv standard argv without initial components
5598 * @return Integer status (0 means success)
5601 static int rpc_trustdom_revoke(int argc, const char **argv)
5603 char* domain_name;
5605 if (argc < 1) return -1;
5607 /* generate upper cased domain name */
5608 domain_name = smb_xstrdup(argv[0]);
5609 strupper_m(domain_name);
5611 /* delete password of the trust */
5612 if (!trusted_domain_password_delete(domain_name)) {
5613 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5614 domain_name));
5615 return -1;
5618 return 0;
5622 * Usage for 'net rpc trustdom' command
5624 * @param argc standard argc
5625 * @param argv standard argv without inital components
5627 * @return Integer status returned to shell
5630 static int rpc_trustdom_usage(int argc, const char **argv)
5632 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5633 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5634 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5635 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5636 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5637 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5638 return -1;
5642 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5643 const char *domain_name,
5644 struct cli_state *cli,
5645 struct rpc_pipe_client *pipe_hnd,
5646 TALLOC_CTX *mem_ctx,
5647 int argc,
5648 const char **argv)
5650 fstring str_sid;
5651 sid_to_string(str_sid, domain_sid);
5652 d_printf("%s\n", str_sid);
5653 return NT_STATUS_OK;
5656 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5658 fstring ascii_sid, padding;
5659 int pad_len, col_len = 20;
5661 /* convert sid into ascii string */
5662 sid_to_string(ascii_sid, dom_sid);
5664 /* calculate padding space for d_printf to look nicer */
5665 pad_len = col_len - strlen(trusted_dom_name);
5666 padding[pad_len] = 0;
5667 do padding[--pad_len] = ' '; while (pad_len);
5669 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5672 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5673 TALLOC_CTX *mem_ctx,
5674 POLICY_HND *pol,
5675 DOM_SID dom_sid,
5676 const char *trusted_dom_name)
5678 NTSTATUS nt_status;
5679 LSA_TRUSTED_DOMAIN_INFO *info;
5680 char *cleartextpwd = NULL;
5681 DATA_BLOB data;
5683 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5685 if (NT_STATUS_IS_ERR(nt_status)) {
5686 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5687 nt_errstr(nt_status)));
5688 goto done;
5691 data = data_blob(NULL, info->password.password.length);
5693 memcpy(data.data, info->password.password.data, info->password.password.length);
5694 data.length = info->password.password.length;
5696 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5698 if (cleartextpwd == NULL) {
5699 DEBUG(0,("retrieved NULL password\n"));
5700 nt_status = NT_STATUS_UNSUCCESSFUL;
5701 goto done;
5704 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5705 cleartextpwd,
5706 &dom_sid)) {
5707 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5708 nt_status = NT_STATUS_UNSUCCESSFUL;
5709 goto done;
5712 #ifdef DEBUG_PASSWORD
5713 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5714 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5715 #endif
5717 done:
5718 SAFE_FREE(cleartextpwd);
5719 data_blob_free(&data);
5721 return nt_status;
5724 static int rpc_trustdom_vampire(int argc, const char **argv)
5726 /* common variables */
5727 TALLOC_CTX* mem_ctx;
5728 struct cli_state *cli = NULL;
5729 struct rpc_pipe_client *pipe_hnd = NULL;
5730 NTSTATUS nt_status;
5731 const char *domain_name = NULL;
5732 DOM_SID *queried_dom_sid;
5733 POLICY_HND connect_hnd;
5735 /* trusted domains listing variables */
5736 unsigned int num_domains, enum_ctx = 0;
5737 int i;
5738 DOM_SID *domain_sids;
5739 char **trusted_dom_names;
5740 fstring pdc_name;
5741 char *dummy;
5744 * Listing trusted domains (stored in secrets.tdb, if local)
5747 mem_ctx = talloc_init("trust relationships vampire");
5750 * set domain and pdc name to local samba server (default)
5751 * or to remote one given in command line
5754 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5755 domain_name = opt_workgroup;
5756 opt_target_workgroup = opt_workgroup;
5757 } else {
5758 fstrcpy(pdc_name, global_myname());
5759 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5760 opt_target_workgroup = domain_name;
5763 /* open \PIPE\lsarpc and open policy handle */
5764 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5765 DEBUG(0, ("Couldn't connect to domain controller\n"));
5766 return -1;
5769 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5770 if (!pipe_hnd) {
5771 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5772 nt_errstr(nt_status) ));
5773 cli_shutdown(cli);
5774 return -1;
5777 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5778 &connect_hnd);
5779 if (NT_STATUS_IS_ERR(nt_status)) {
5780 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5781 nt_errstr(nt_status)));
5782 cli_shutdown(cli);
5783 return -1;
5786 /* query info level 5 to obtain sid of a domain being queried */
5787 nt_status = rpccli_lsa_query_info_policy(
5788 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5789 &dummy, &queried_dom_sid);
5791 if (NT_STATUS_IS_ERR(nt_status)) {
5792 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5793 nt_errstr(nt_status)));
5794 cli_shutdown(cli);
5795 return -1;
5799 * Keep calling LsaEnumTrustdom over opened pipe until
5800 * the end of enumeration is reached
5803 d_printf("Vampire trusted domains:\n\n");
5805 do {
5806 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5807 &num_domains,
5808 &trusted_dom_names, &domain_sids);
5810 if (NT_STATUS_IS_ERR(nt_status)) {
5811 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5812 nt_errstr(nt_status)));
5813 cli_shutdown(cli);
5814 return -1;
5817 for (i = 0; i < num_domains; i++) {
5819 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5821 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5822 domain_sids[i], trusted_dom_names[i]);
5823 if (!NT_STATUS_IS_OK(nt_status)) {
5824 cli_shutdown(cli);
5825 return -1;
5830 * in case of no trusted domains say something rather
5831 * than just display blank line
5833 if (!num_domains) d_printf("none\n");
5835 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5837 /* close this connection before doing next one */
5838 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5839 if (NT_STATUS_IS_ERR(nt_status)) {
5840 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5841 nt_errstr(nt_status)));
5842 cli_shutdown(cli);
5843 return -1;
5846 /* close lsarpc pipe and connection to IPC$ */
5847 cli_shutdown(cli);
5849 talloc_destroy(mem_ctx);
5850 return 0;
5853 static int rpc_trustdom_list(int argc, const char **argv)
5855 /* common variables */
5856 TALLOC_CTX* mem_ctx;
5857 struct cli_state *cli = NULL, *remote_cli = NULL;
5858 struct rpc_pipe_client *pipe_hnd = NULL;
5859 NTSTATUS nt_status;
5860 const char *domain_name = NULL;
5861 DOM_SID *queried_dom_sid;
5862 fstring padding;
5863 int ascii_dom_name_len;
5864 POLICY_HND connect_hnd;
5866 /* trusted domains listing variables */
5867 unsigned int num_domains, enum_ctx = 0;
5868 int i, pad_len, col_len = 20;
5869 DOM_SID *domain_sids;
5870 char **trusted_dom_names;
5871 fstring pdc_name;
5872 char *dummy;
5874 /* trusting domains listing variables */
5875 POLICY_HND domain_hnd;
5876 char **trusting_dom_names;
5877 uint32 *trusting_dom_rids;
5880 * Listing trusted domains (stored in secrets.tdb, if local)
5883 mem_ctx = talloc_init("trust relationships listing");
5886 * set domain and pdc name to local samba server (default)
5887 * or to remote one given in command line
5890 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5891 domain_name = opt_workgroup;
5892 opt_target_workgroup = opt_workgroup;
5893 } else {
5894 fstrcpy(pdc_name, global_myname());
5895 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5896 opt_target_workgroup = domain_name;
5899 /* open \PIPE\lsarpc and open policy handle */
5900 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5901 DEBUG(0, ("Couldn't connect to domain controller\n"));
5902 return -1;
5905 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5906 if (!pipe_hnd) {
5907 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5908 nt_errstr(nt_status) ));
5909 return -1;
5912 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5913 &connect_hnd);
5914 if (NT_STATUS_IS_ERR(nt_status)) {
5915 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5916 nt_errstr(nt_status)));
5917 return -1;
5920 /* query info level 5 to obtain sid of a domain being queried */
5921 nt_status = rpccli_lsa_query_info_policy(
5922 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5923 &dummy, &queried_dom_sid);
5925 if (NT_STATUS_IS_ERR(nt_status)) {
5926 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5927 nt_errstr(nt_status)));
5928 return -1;
5932 * Keep calling LsaEnumTrustdom over opened pipe until
5933 * the end of enumeration is reached
5936 d_printf("Trusted domains list:\n\n");
5938 do {
5939 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5940 &num_domains,
5941 &trusted_dom_names, &domain_sids);
5943 if (NT_STATUS_IS_ERR(nt_status)) {
5944 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5945 nt_errstr(nt_status)));
5946 return -1;
5949 for (i = 0; i < num_domains; i++) {
5950 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5954 * in case of no trusted domains say something rather
5955 * than just display blank line
5957 if (!num_domains) d_printf("none\n");
5959 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5961 /* close this connection before doing next one */
5962 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5963 if (NT_STATUS_IS_ERR(nt_status)) {
5964 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5965 nt_errstr(nt_status)));
5966 return -1;
5969 cli_rpc_pipe_close(pipe_hnd);
5972 * Listing trusting domains (stored in passdb backend, if local)
5975 d_printf("\nTrusting domains list:\n\n");
5978 * Open \PIPE\samr and get needed policy handles
5980 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
5981 if (!pipe_hnd) {
5982 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
5983 return -1;
5986 /* SamrConnect */
5987 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
5988 &connect_hnd);
5989 if (!NT_STATUS_IS_OK(nt_status)) {
5990 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
5991 nt_errstr(nt_status)));
5992 return -1;
5995 /* SamrOpenDomain - we have to open domain policy handle in order to be
5996 able to enumerate accounts*/
5997 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
5998 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
5999 queried_dom_sid, &domain_hnd);
6000 if (!NT_STATUS_IS_OK(nt_status)) {
6001 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6002 nt_errstr(nt_status)));
6003 return -1;
6007 * perform actual enumeration
6010 enum_ctx = 0; /* reset enumeration context from last enumeration */
6011 do {
6013 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6014 &enum_ctx, ACB_DOMTRUST, 0xffff,
6015 &trusting_dom_names, &trusting_dom_rids,
6016 &num_domains);
6017 if (NT_STATUS_IS_ERR(nt_status)) {
6018 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6019 nt_errstr(nt_status)));
6020 return -1;
6023 for (i = 0; i < num_domains; i++) {
6026 * get each single domain's sid (do we _really_ need this ?):
6027 * 1) connect to domain's pdc
6028 * 2) query the pdc for domain's sid
6031 /* get rid of '$' tail */
6032 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6033 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6034 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6036 /* calculate padding space for d_printf to look nicer */
6037 pad_len = col_len - strlen(trusting_dom_names[i]);
6038 padding[pad_len] = 0;
6039 do padding[--pad_len] = ' '; while (pad_len);
6041 /* set opt_* variables to remote domain */
6042 strupper_m(trusting_dom_names[i]);
6043 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6044 opt_target_workgroup = opt_workgroup;
6046 d_printf("%s%s", trusting_dom_names[i], padding);
6048 /* connect to remote domain controller */
6049 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6050 if (remote_cli) {
6051 /* query for domain's sid */
6052 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6053 d_fprintf(stderr, "couldn't get domain's sid\n");
6055 cli_shutdown(remote_cli);
6057 } else {
6058 d_fprintf(stderr, "domain controller is not responding\n");
6062 if (!num_domains) d_printf("none\n");
6064 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6066 /* close opened samr and domain policy handles */
6067 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6068 if (!NT_STATUS_IS_OK(nt_status)) {
6069 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6072 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6073 if (!NT_STATUS_IS_OK(nt_status)) {
6074 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6077 /* close samr pipe and connection to IPC$ */
6078 cli_shutdown(cli);
6080 talloc_destroy(mem_ctx);
6081 return 0;
6085 * Entrypoint for 'net rpc trustdom' code
6087 * @param argc standard argc
6088 * @param argv standard argv without initial components
6090 * @return Integer status (0 means success)
6093 static int rpc_trustdom(int argc, const char **argv)
6095 struct functable func[] = {
6096 {"add", rpc_trustdom_add},
6097 {"del", rpc_trustdom_del},
6098 {"establish", rpc_trustdom_establish},
6099 {"revoke", rpc_trustdom_revoke},
6100 {"help", rpc_trustdom_usage},
6101 {"list", rpc_trustdom_list},
6102 {"vampire", rpc_trustdom_vampire},
6103 {NULL, NULL}
6106 if (argc == 0) {
6107 rpc_trustdom_usage(argc, argv);
6108 return -1;
6111 return (net_run_function(argc, argv, func, rpc_user_usage));
6115 * Check if a server will take rpc commands
6116 * @param flags Type of server to connect to (PDC, DMB, localhost)
6117 * if the host is not explicitly specified
6118 * @return BOOL (true means rpc supported)
6120 BOOL net_rpc_check(unsigned flags)
6122 struct cli_state *cli;
6123 BOOL ret = False;
6124 struct in_addr server_ip;
6125 char *server_name = NULL;
6127 /* flags (i.e. server type) may depend on command */
6128 if (!net_find_server(NULL, flags, &server_ip, &server_name))
6129 return False;
6131 if ((cli = cli_initialise()) == NULL) {
6132 return False;
6135 if (!cli_connect(cli, server_name, &server_ip))
6136 goto done;
6137 if (!attempt_netbios_session_request(&cli, global_myname(),
6138 server_name, &server_ip))
6139 goto done;
6140 if (!cli_negprot(cli))
6141 goto done;
6142 if (cli->protocol < PROTOCOL_NT1)
6143 goto done;
6145 ret = True;
6146 done:
6147 cli_shutdown(cli);
6148 return ret;
6151 /* dump sam database via samsync rpc calls */
6152 static int rpc_samdump(int argc, const char **argv) {
6153 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6154 argc, argv);
6157 /* syncronise sam database via samsync rpc calls */
6158 static int rpc_vampire(int argc, const char **argv) {
6159 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6160 argc, argv);
6163 /**
6164 * Migrate everything from a print-server
6166 * @param argc Standard main() style argc
6167 * @param argv Standard main() style argv. Initial components are already
6168 * stripped
6170 * @return A shell status integer (0 for success)
6172 * The order is important !
6173 * To successfully add drivers the print-queues have to exist !
6174 * Applying ACLs should be the last step, because you're easily locked out
6177 static int rpc_printer_migrate_all(int argc, const char **argv)
6179 int ret;
6181 if (!opt_host) {
6182 printf("no server to migrate\n");
6183 return -1;
6186 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6187 if (ret)
6188 return ret;
6190 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6191 if (ret)
6192 return ret;
6194 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6195 if (ret)
6196 return ret;
6198 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6199 if (ret)
6200 return ret;
6202 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6206 /**
6207 * Migrate print-drivers from a print-server
6209 * @param argc Standard main() style argc
6210 * @param argv Standard main() style argv. Initial components are already
6211 * stripped
6213 * @return A shell status integer (0 for success)
6215 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6217 if (!opt_host) {
6218 printf("no server to migrate\n");
6219 return -1;
6222 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6223 rpc_printer_migrate_drivers_internals,
6224 argc, argv);
6227 /**
6228 * Migrate print-forms from a print-server
6230 * @param argc Standard main() style argc
6231 * @param argv Standard main() style argv. Initial components are already
6232 * stripped
6234 * @return A shell status integer (0 for success)
6236 static int rpc_printer_migrate_forms(int argc, const char **argv)
6238 if (!opt_host) {
6239 printf("no server to migrate\n");
6240 return -1;
6243 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6244 rpc_printer_migrate_forms_internals,
6245 argc, argv);
6248 /**
6249 * Migrate printers from a print-server
6251 * @param argc Standard main() style argc
6252 * @param argv Standard main() style argv. Initial components are already
6253 * stripped
6255 * @return A shell status integer (0 for success)
6257 static int rpc_printer_migrate_printers(int argc, const char **argv)
6259 if (!opt_host) {
6260 printf("no server to migrate\n");
6261 return -1;
6264 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6265 rpc_printer_migrate_printers_internals,
6266 argc, argv);
6269 /**
6270 * Migrate printer-ACLs from a print-server
6272 * @param argc Standard main() style argc
6273 * @param argv Standard main() style argv. Initial components are already
6274 * stripped
6276 * @return A shell status integer (0 for success)
6278 static int rpc_printer_migrate_security(int argc, const char **argv)
6280 if (!opt_host) {
6281 printf("no server to migrate\n");
6282 return -1;
6285 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6286 rpc_printer_migrate_security_internals,
6287 argc, argv);
6290 /**
6291 * Migrate printer-settings from a print-server
6293 * @param argc Standard main() style argc
6294 * @param argv Standard main() style argv. Initial components are already
6295 * stripped
6297 * @return A shell status integer (0 for success)
6299 static int rpc_printer_migrate_settings(int argc, const char **argv)
6301 if (!opt_host) {
6302 printf("no server to migrate\n");
6303 return -1;
6306 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6307 rpc_printer_migrate_settings_internals,
6308 argc, argv);
6311 /**
6312 * 'net rpc printer' entrypoint.
6313 * @param argc Standard main() style argc
6314 * @param argv Standard main() style argv. Initial components are already
6315 * stripped
6318 int rpc_printer_migrate(int argc, const char **argv)
6321 /* ouch: when addriver and setdriver are called from within
6322 rpc_printer_migrate_drivers_internals, the printer-queue already
6323 *has* to exist */
6325 struct functable func[] = {
6326 {"all", rpc_printer_migrate_all},
6327 {"drivers", rpc_printer_migrate_drivers},
6328 {"forms", rpc_printer_migrate_forms},
6329 {"help", rpc_printer_usage},
6330 {"printers", rpc_printer_migrate_printers},
6331 {"security", rpc_printer_migrate_security},
6332 {"settings", rpc_printer_migrate_settings},
6333 {NULL, NULL}
6336 return net_run_function(argc, argv, func, rpc_printer_usage);
6340 /**
6341 * List printers on a remote RPC server
6343 * @param argc Standard main() style argc
6344 * @param argv Standard main() style argv. Initial components are already
6345 * stripped
6347 * @return A shell status integer (0 for success)
6349 static int rpc_printer_list(int argc, const char **argv)
6352 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6353 rpc_printer_list_internals,
6354 argc, argv);
6357 /**
6358 * List printer-drivers on a remote RPC server
6360 * @param argc Standard main() style argc
6361 * @param argv Standard main() style argv. Initial components are already
6362 * stripped
6364 * @return A shell status integer (0 for success)
6366 static int rpc_printer_driver_list(int argc, const char **argv)
6369 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6370 rpc_printer_driver_list_internals,
6371 argc, argv);
6374 /**
6375 * Publish printer in ADS via MSRPC
6377 * @param argc Standard main() style argc
6378 * @param argv Standard main() style argv. Initial components are already
6379 * stripped
6381 * @return A shell status integer (0 for success)
6383 static int rpc_printer_publish_publish(int argc, const char **argv)
6386 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6387 rpc_printer_publish_publish_internals,
6388 argc, argv);
6391 /**
6392 * Update printer in ADS via MSRPC
6394 * @param argc Standard main() style argc
6395 * @param argv Standard main() style argv. Initial components are already
6396 * stripped
6398 * @return A shell status integer (0 for success)
6400 static int rpc_printer_publish_update(int argc, const char **argv)
6403 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6404 rpc_printer_publish_update_internals,
6405 argc, argv);
6408 /**
6409 * UnPublish printer in ADS via MSRPC
6411 * @param argc Standard main() style argc
6412 * @param argv Standard main() style argv. Initial components are already
6413 * stripped
6415 * @return A shell status integer (0 for success)
6417 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6420 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6421 rpc_printer_publish_unpublish_internals,
6422 argc, argv);
6425 /**
6426 * List published printers via MSRPC
6428 * @param argc Standard main() style argc
6429 * @param argv Standard main() style argv. Initial components are already
6430 * stripped
6432 * @return A shell status integer (0 for success)
6434 static int rpc_printer_publish_list(int argc, const char **argv)
6437 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6438 rpc_printer_publish_list_internals,
6439 argc, argv);
6443 /**
6444 * Publish printer in ADS
6446 * @param argc Standard main() style argc
6447 * @param argv Standard main() style argv. Initial components are already
6448 * stripped
6450 * @return A shell status integer (0 for success)
6452 static int rpc_printer_publish(int argc, const char **argv)
6455 struct functable func[] = {
6456 {"publish", rpc_printer_publish_publish},
6457 {"update", rpc_printer_publish_update},
6458 {"unpublish", rpc_printer_publish_unpublish},
6459 {"list", rpc_printer_publish_list},
6460 {"help", rpc_printer_usage},
6461 {NULL, NULL}
6464 if (argc == 0)
6465 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6466 rpc_printer_publish_list_internals,
6467 argc, argv);
6469 return net_run_function(argc, argv, func, rpc_printer_usage);
6474 /**
6475 * Display rpc printer help page.
6476 * @param argc Standard main() style argc
6477 * @param argv Standard main() style argv. Initial components are already
6478 * stripped
6480 int rpc_printer_usage(int argc, const char **argv)
6482 return net_help_printer(argc, argv);
6485 /**
6486 * 'net rpc printer' entrypoint.
6487 * @param argc Standard main() style argc
6488 * @param argv Standard main() style argv. Initial components are already
6489 * stripped
6491 int net_rpc_printer(int argc, const char **argv)
6493 struct functable func[] = {
6494 {"list", rpc_printer_list},
6495 {"migrate", rpc_printer_migrate},
6496 {"driver", rpc_printer_driver_list},
6497 {"publish", rpc_printer_publish},
6498 {NULL, NULL}
6501 if (argc == 0)
6502 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6503 rpc_printer_list_internals,
6504 argc, argv);
6506 return net_run_function(argc, argv, func, rpc_printer_usage);
6509 /****************************************************************************/
6512 /**
6513 * Basic usage function for 'net rpc'
6514 * @param argc Standard main() style argc
6515 * @param argv Standard main() style argv. Initial components are already
6516 * stripped
6519 int net_rpc_usage(int argc, const char **argv)
6521 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6522 d_printf(" net rpc join \t\t\tto join a domain \n");
6523 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6524 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6525 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6526 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6527 d_printf(" net rpc group \t\tto list groups\n");
6528 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6529 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6530 d_printf(" net rpc file \t\t\tto list open files\n");
6531 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6532 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6533 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6534 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6535 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6536 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6537 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6538 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6539 d_printf(" net rpc registry\t\tto manage registry hives\n");
6540 d_printf(" net rpc service\t\tto start, stop and query services\n");
6541 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6542 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6543 d_printf("\n");
6544 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6545 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6546 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6547 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6548 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6549 return -1;
6554 * Help function for 'net rpc'. Calls command specific help if requested
6555 * or displays usage of net rpc
6556 * @param argc Standard main() style argc
6557 * @param argv Standard main() style argv. Initial components are already
6558 * stripped
6561 int net_rpc_help(int argc, const char **argv)
6563 struct functable func[] = {
6564 {"join", rpc_join_usage},
6565 {"user", rpc_user_usage},
6566 {"group", rpc_group_usage},
6567 {"share", rpc_share_usage},
6568 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6569 {"trustdom", rpc_trustdom_usage},
6570 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6571 /*{"shutdown", rpc_shutdown_usage}, */
6572 {"vampire", rpc_vampire_usage},
6573 {NULL, NULL}
6576 if (argc == 0) {
6577 net_rpc_usage(argc, argv);
6578 return -1;
6581 return (net_run_function(argc, argv, func, rpc_user_usage));
6584 /**
6585 * 'net rpc' entrypoint.
6586 * @param argc Standard main() style argc
6587 * @param argv Standard main() style argv. Initial components are already
6588 * stripped
6591 int net_rpc(int argc, const char **argv)
6593 struct functable func[] = {
6594 {"audit", net_rpc_audit},
6595 {"info", net_rpc_info},
6596 {"join", net_rpc_join},
6597 {"oldjoin", net_rpc_oldjoin},
6598 {"testjoin", net_rpc_testjoin},
6599 {"user", net_rpc_user},
6600 {"password", rpc_user_password},
6601 {"group", net_rpc_group},
6602 {"share", net_rpc_share},
6603 {"file", net_rpc_file},
6604 {"printer", net_rpc_printer},
6605 {"changetrustpw", net_rpc_changetrustpw},
6606 {"trustdom", rpc_trustdom},
6607 {"abortshutdown", rpc_shutdown_abort},
6608 {"shutdown", rpc_shutdown},
6609 {"samdump", rpc_samdump},
6610 {"vampire", rpc_vampire},
6611 {"getsid", net_rpc_getsid},
6612 {"rights", net_rpc_rights},
6613 {"service", net_rpc_service},
6614 {"registry", net_rpc_registry},
6615 {"shell", net_rpc_shell},
6616 {"help", net_rpc_help},
6617 {NULL, NULL}
6619 return net_run_function(argc, argv, func, net_rpc_usage);