r7415: * big change -- volker's new async winbindd from trunk
[Samba/gbeck.git] / source / utils / net_rpc.c
blob000e77345efe56f5f6fb62115125e1806234d212
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)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "includes.h"
23 #include "utils/net.h"
25 /**
26 * @file net_rpc.c
28 * @brief RPC based subcommands for the 'net' utility.
30 * This file should contain much of the functionality that used to
31 * be found in rpcclient, execpt that the commands should change
32 * less often, and the fucntionality should be sane (the user is not
33 * expected to know a rid/sid before they conduct an operation etc.)
35 * @todo Perhaps eventually these should be split out into a number
36 * of files, as this could get quite big.
37 **/
40 /**
41 * Many of the RPC functions need the domain sid. This function gets
42 * it at the start of every run
44 * @param cli A cli_state already connected to the remote machine
46 * @return The Domain SID of the remote machine.
47 **/
49 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx, char **domain_name)
51 DOM_SID *domain_sid;
52 POLICY_HND pol;
53 NTSTATUS result = NT_STATUS_OK;
54 uint32 info_class = 5;
56 if (!cli_nt_session_open (cli, PI_LSARPC)) {
57 fprintf(stderr, "could not initialise lsa pipe\n");
58 goto error;
61 result = cli_lsa_open_policy(cli, mem_ctx, False,
62 SEC_RIGHTS_MAXIMUM_ALLOWED,
63 &pol);
64 if (!NT_STATUS_IS_OK(result)) {
65 goto error;
68 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
69 domain_name, &domain_sid);
70 if (!NT_STATUS_IS_OK(result)) {
71 error:
72 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
74 if (!NT_STATUS_IS_OK(result)) {
75 fprintf(stderr, "error: %s\n", nt_errstr(result));
78 exit(1);
81 cli_lsa_close(cli, mem_ctx, &pol);
82 cli_nt_session_close(cli);
84 return domain_sid;
87 /**
88 * Run a single RPC command, from start to finish.
90 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
91 * @param conn_flag a NET_FLAG_ combination. Passed to
92 * net_make_ipc_connection.
93 * @param argc Standard main() style argc
94 * @param argc Standard main() style argv. Initial components are already
95 * stripped
96 * @return A shell status integer (0 for success)
99 int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
100 rpc_command_fn fn,
101 int argc, const char **argv)
103 struct cli_state *cli = NULL;
104 TALLOC_CTX *mem_ctx;
105 NTSTATUS nt_status;
106 DOM_SID *domain_sid;
107 char *domain_name;
109 /* make use of cli_state handed over as an argument, if possible */
110 if (!cli_arg)
111 cli = net_make_ipc_connection(conn_flags);
112 else
113 cli = cli_arg;
115 if (!cli) {
116 return -1;
119 /* Create mem_ctx */
121 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
122 DEBUG(0, ("talloc_init() failed\n"));
123 cli_shutdown(cli);
124 return -1;
127 domain_sid = net_get_remote_domain_sid(cli, mem_ctx, &domain_name);
129 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
130 if (!cli_nt_session_open(cli, pipe_idx)) {
131 DEBUG(0, ("Could not initialise pipe\n"));
135 nt_status = fn(domain_sid, domain_name, cli, mem_ctx, argc, argv);
137 if (!NT_STATUS_IS_OK(nt_status)) {
138 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
139 } else {
140 DEBUG(5, ("rpc command function succedded\n"));
143 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
144 if (cli->pipes[cli->pipe_idx].fnum)
145 cli_nt_session_close(cli);
148 /* close the connection only if it was opened here */
149 if (!cli_arg)
150 cli_shutdown(cli);
152 talloc_destroy(mem_ctx);
154 return (!NT_STATUS_IS_OK(nt_status));
158 /****************************************************************************/
161 /**
162 * Force a change of the trust acccount password.
164 * All parameters are provided by the run_rpc_command function, except for
165 * argc, argv which are passes through.
167 * @param domain_sid The domain sid aquired from the remote server
168 * @param cli A cli_state connected to the server.
169 * @param mem_ctx Talloc context, destoyed on compleation of the function.
170 * @param argc Standard main() style argc
171 * @param argc Standard main() style argv. Initial components are already
172 * stripped
174 * @return Normal NTSTATUS return.
177 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, const char *domain_name,
178 struct cli_state *cli, TALLOC_CTX *mem_ctx,
179 int argc, const char **argv) {
181 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
184 /**
185 * Force a change of the trust acccount password.
187 * @param argc Standard main() style argc
188 * @param argc Standard main() style argv. Initial components are already
189 * stripped
191 * @return A shell status integer (0 for success)
194 int net_rpc_changetrustpw(int argc, const char **argv)
196 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
197 rpc_changetrustpw_internals,
198 argc, argv);
202 /****************************************************************************/
205 /**
206 * Join a domain, the old way.
208 * This uses 'machinename' as the inital password, and changes it.
210 * The password should be created with 'server manager' or equiv first.
212 * All parameters are provided by the run_rpc_command function, except for
213 * argc, argv which are passes through.
215 * @param domain_sid The domain sid aquired from the remote server
216 * @param cli A cli_state connected to the server.
217 * @param mem_ctx Talloc context, destoyed on compleation of the function.
218 * @param argc Standard main() style argc
219 * @param argc Standard main() style argv. Initial components are already
220 * stripped
222 * @return Normal NTSTATUS return.
225 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, const char *domain_name,
226 struct cli_state *cli,
227 TALLOC_CTX *mem_ctx,
228 int argc, const char **argv) {
230 fstring trust_passwd;
231 unsigned char orig_trust_passwd_hash[16];
232 NTSTATUS result;
233 uint32 sec_channel_type;
236 check what type of join - if the user want's to join as
237 a BDC, the server must agree that we are a BDC.
239 if (argc >= 0) {
240 sec_channel_type = get_sec_channel_type(argv[0]);
241 } else {
242 sec_channel_type = get_sec_channel_type(NULL);
245 fstrcpy(trust_passwd, global_myname());
246 strlower_m(trust_passwd);
249 * Machine names can be 15 characters, but the max length on
250 * a password is 14. --jerry
253 trust_passwd[14] = '\0';
255 E_md4hash(trust_passwd, orig_trust_passwd_hash);
257 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
258 orig_trust_passwd_hash,
259 sec_channel_type);
261 if (NT_STATUS_IS_OK(result))
262 printf("Joined domain %s.\n",opt_target_workgroup);
265 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
266 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
267 result = NT_STATUS_UNSUCCESSFUL;
270 return result;
273 /**
274 * Join a domain, the old way.
276 * @param argc Standard main() style argc
277 * @param argc Standard main() style argv. Initial components are already
278 * stripped
280 * @return A shell status integer (0 for success)
283 static int net_rpc_perform_oldjoin(int argc, const char **argv)
285 return run_rpc_command(NULL, PI_NETLOGON,
286 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
287 rpc_oldjoin_internals,
288 argc, argv);
291 /**
292 * Join a domain, the old way. This function exists to allow
293 * the message to be displayed when oldjoin was explicitly
294 * requested, but not when it was implied by "net rpc join"
296 * @param argc Standard main() style argc
297 * @param argc Standard main() style argv. Initial components are already
298 * stripped
300 * @return A shell status integer (0 for success)
303 static int net_rpc_oldjoin(int argc, const char **argv)
305 int rc = net_rpc_perform_oldjoin(argc, argv);
307 if (rc) {
308 d_printf("Failed to join domain\n");
311 return rc;
314 /**
315 * Basic usage function for 'net rpc join'
316 * @param argc Standard main() style argc
317 * @param argc Standard main() style argv. Initial components are already
318 * stripped
321 static int rpc_join_usage(int argc, const char **argv)
323 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
324 "\t to join a domain with admin username & password\n"\
325 "\t\t password will be prompted if needed and none is specified\n"\
326 "\t <type> can be (default MEMBER)\n"\
327 "\t\t BDC - Join as a BDC\n"\
328 "\t\t PDC - Join as a PDC\n"\
329 "\t\t MEMBER - Join as a MEMBER server\n");
331 net_common_flags_usage(argc, argv);
332 return -1;
335 /**
336 * 'net rpc join' entrypoint.
337 * @param argc Standard main() style argc
338 * @param argc Standard main() style argv. Initial components are already
339 * stripped
341 * Main 'net_rpc_join()' (where the admain username/password is used) is
342 * in net_rpc_join.c
343 * Try to just change the password, but if that doesn't work, use/prompt
344 * for a username/password.
347 int net_rpc_join(int argc, const char **argv)
349 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
350 return 0;
352 return net_rpc_join_newstyle(argc, argv);
357 /**
358 * display info about a rpc domain
360 * All parameters are provided by the run_rpc_command function, except for
361 * argc, argv which are passed through.
363 * @param domain_sid The domain sid acquired from the remote server
364 * @param cli A cli_state connected to the server.
365 * @param mem_ctx Talloc context, destoyed on completion of the function.
366 * @param argc Standard main() style argc
367 * @param argv Standard main() style argv. Initial components are already
368 * stripped
370 * @return Normal NTSTATUS return.
373 static NTSTATUS
374 rpc_info_internals(const DOM_SID *domain_sid, const char *domain_name,
375 struct cli_state *cli,
376 TALLOC_CTX *mem_ctx, int argc, const char **argv)
378 POLICY_HND connect_pol, domain_pol;
379 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
380 SAM_UNK_CTR ctr;
381 fstring sid_str;
383 sid_to_string(sid_str, domain_sid);
385 /* Get sam policy handle */
386 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
387 &connect_pol);
388 if (!NT_STATUS_IS_OK(result)) {
389 goto done;
392 /* Get domain policy handle */
393 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
394 MAXIMUM_ALLOWED_ACCESS,
395 domain_sid, &domain_pol);
396 if (!NT_STATUS_IS_OK(result)) {
397 goto done;
400 ZERO_STRUCT(ctr);
401 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
402 2, &ctr);
403 if (NT_STATUS_IS_OK(result)) {
404 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
405 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
406 d_printf("Domain SID: %s\n", sid_str);
407 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
408 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
409 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
410 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
411 talloc_destroy(ctx);
414 done:
415 return result;
419 /**
420 * 'net rpc info' entrypoint.
421 * @param argc Standard main() style argc
422 * @param argc Standard main() style argv. Initial components are already
423 * stripped
425 int net_rpc_info(int argc, const char **argv)
427 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
428 rpc_info_internals,
429 argc, argv);
433 /**
434 * Fetch domain SID into the local secrets.tdb
436 * All parameters are provided by the run_rpc_command function, except for
437 * argc, argv which are passes through.
439 * @param domain_sid The domain sid acquired from the remote server
440 * @param cli A cli_state connected to the server.
441 * @param mem_ctx Talloc context, destoyed on completion of the function.
442 * @param argc Standard main() style argc
443 * @param argv Standard main() style argv. Initial components are already
444 * stripped
446 * @return Normal NTSTATUS return.
449 static NTSTATUS
450 rpc_getsid_internals(const DOM_SID *domain_sid, const char *domain_name,
451 struct cli_state *cli,
452 TALLOC_CTX *mem_ctx, int argc, const char **argv)
454 fstring sid_str;
456 sid_to_string(sid_str, domain_sid);
457 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
458 sid_str, domain_name);
460 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
461 DEBUG(0,("Can't store domain SID\n"));
462 return NT_STATUS_UNSUCCESSFUL;
465 return NT_STATUS_OK;
469 /**
470 * 'net rpc getsid' entrypoint.
471 * @param argc Standard main() style argc
472 * @param argc Standard main() style argv. Initial components are already
473 * stripped
475 int net_rpc_getsid(int argc, const char **argv)
477 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
478 rpc_getsid_internals,
479 argc, argv);
483 /****************************************************************************/
486 * Basic usage function for 'net rpc user'
487 * @param argc Standard main() style argc.
488 * @param argv Standard main() style argv. Initial components are already
489 * stripped.
492 static int rpc_user_usage(int argc, const char **argv)
494 return net_help_user(argc, argv);
497 /**
498 * Add a new user to a remote RPC server
500 * All parameters are provided by the run_rpc_command function, except for
501 * argc, argv which are passes through.
503 * @param domain_sid The domain sid acquired from the remote server
504 * @param cli A cli_state connected to the server.
505 * @param mem_ctx Talloc context, destoyed on completion of the function.
506 * @param argc Standard main() style argc
507 * @param argv Standard main() style argv. Initial components are already
508 * stripped
510 * @return Normal NTSTATUS return.
513 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, const char *domain_name,
514 struct cli_state *cli, TALLOC_CTX *mem_ctx,
515 int argc, const char **argv) {
517 POLICY_HND connect_pol, domain_pol, user_pol;
518 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
519 const char *acct_name;
520 uint16 acb_info;
521 uint32 unknown, user_rid;
523 if (argc != 1) {
524 d_printf("User must be specified\n");
525 rpc_user_usage(argc, argv);
526 return NT_STATUS_OK;
529 acct_name = argv[0];
531 /* Get sam policy handle */
533 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
534 &connect_pol);
535 if (!NT_STATUS_IS_OK(result)) {
536 goto done;
539 /* Get domain policy handle */
541 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
542 MAXIMUM_ALLOWED_ACCESS,
543 domain_sid, &domain_pol);
544 if (!NT_STATUS_IS_OK(result)) {
545 goto done;
548 /* Create domain user */
550 acb_info = ACB_NORMAL;
551 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
553 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
554 acct_name, acb_info, unknown,
555 &user_pol, &user_rid);
556 if (!NT_STATUS_IS_OK(result)) {
557 goto done;
560 done:
561 if (!NT_STATUS_IS_OK(result)) {
562 d_printf("Failed to add user %s - %s\n", acct_name,
563 nt_errstr(result));
564 } else {
565 d_printf("Added user %s\n", acct_name);
567 return result;
570 /**
571 * Add a new user to a remote RPC server
573 * @param argc Standard main() style argc
574 * @param argv Standard main() style argv. Initial components are already
575 * stripped
577 * @return A shell status integer (0 for success)
580 static int rpc_user_add(int argc, const char **argv)
582 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
583 argc, argv);
586 /**
587 * Delete a user from a remote RPC server
589 * All parameters are provided by the run_rpc_command function, except for
590 * argc, argv which are passes through.
592 * @param domain_sid The domain sid acquired from the remote server
593 * @param cli A cli_state connected to the server.
594 * @param mem_ctx Talloc context, destoyed on completion of the function.
595 * @param argc Standard main() style argc
596 * @param argv Standard main() style argv. Initial components are already
597 * stripped
599 * @return Normal NTSTATUS return.
602 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
603 const char *domain_name,
604 struct cli_state *cli,
605 TALLOC_CTX *mem_ctx,
606 int argc, const char **argv)
608 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
609 POLICY_HND connect_pol, domain_pol, user_pol;
611 if (argc < 1) {
612 d_printf("User must be specified\n");
613 rpc_user_usage(argc, argv);
614 return NT_STATUS_OK;
616 /* Get sam policy and domain handles */
618 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
619 &connect_pol);
621 if (!NT_STATUS_IS_OK(result)) {
622 goto done;
625 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
626 MAXIMUM_ALLOWED_ACCESS,
627 domain_sid, &domain_pol);
629 if (!NT_STATUS_IS_OK(result)) {
630 goto done;
633 /* Get handle on user */
636 uint32 *user_rids, num_rids, *name_types;
637 uint32 flags = 0x000003e8; /* Unknown */
639 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
640 flags, 1, &argv[0],
641 &num_rids, &user_rids,
642 &name_types);
644 if (!NT_STATUS_IS_OK(result)) {
645 goto done;
648 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
649 MAXIMUM_ALLOWED_ACCESS,
650 user_rids[0], &user_pol);
652 if (!NT_STATUS_IS_OK(result)) {
653 goto done;
657 /* Delete user */
659 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
661 if (!NT_STATUS_IS_OK(result)) {
662 goto done;
665 /* Display results */
666 if (!NT_STATUS_IS_OK(result)) {
667 d_printf("Failed to delete user account - %s\n", nt_errstr(result));
668 } else {
669 d_printf("Deleted user account\n");
672 done:
673 return result;
677 /**
678 * Rename a user on a remote RPC server
680 * All parameters are provided by the run_rpc_command function, except for
681 * argc, argv which are passes through.
683 * @param domain_sid The domain sid acquired from the remote server
684 * @param cli A cli_state connected to the server.
685 * @param mem_ctx Talloc context, destoyed on completion of the function.
686 * @param argc Standard main() style argc
687 * @param argv Standard main() style argv. Initial components are already
688 * stripped
690 * @return Normal NTSTATUS return.
693 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid, const char *domain_name,
694 struct cli_state *cli, TALLOC_CTX *mem_ctx,
695 int argc, const char **argv) {
697 POLICY_HND connect_pol, domain_pol, user_pol;
698 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
699 uint32 info_level = 7;
700 const char *old_name, *new_name;
701 uint32 *user_rid;
702 uint32 flags = 0x000003e8; /* Unknown */
703 uint32 num_rids, *name_types;
704 uint32 num_names = 1;
705 const char **names;
706 SAM_USERINFO_CTR *user_ctr;
707 SAM_USERINFO_CTR ctr;
708 SAM_USER_INFO_7 info7;
710 if (argc != 2) {
711 d_printf("New and old username must be specified\n");
712 rpc_user_usage(argc, argv);
713 return NT_STATUS_OK;
716 old_name = argv[0];
717 new_name = argv[1];
719 ZERO_STRUCT(ctr);
720 ZERO_STRUCT(user_ctr);
722 /* Get sam policy handle */
724 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
725 &connect_pol);
726 if (!NT_STATUS_IS_OK(result)) {
727 goto done;
730 /* Get domain policy handle */
732 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
733 MAXIMUM_ALLOWED_ACCESS,
734 domain_sid, &domain_pol);
735 if (!NT_STATUS_IS_OK(result)) {
736 goto done;
739 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
740 names[0] = old_name;
741 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
742 flags, num_names, names,
743 &num_rids, &user_rid, &name_types);
744 if (!NT_STATUS_IS_OK(result)) {
745 goto done;
748 /* Open domain user */
749 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
750 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
752 if (!NT_STATUS_IS_OK(result)) {
753 goto done;
756 /* Query user info */
757 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
758 info_level, &user_ctr);
760 if (!NT_STATUS_IS_OK(result)) {
761 goto done;
764 ctr.switch_value = info_level;
765 ctr.info.id7 = &info7;
767 init_sam_user_info7(&info7, new_name);
769 /* Set new name */
770 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol,
771 info_level, &cli->user_session_key, &ctr);
773 if (!NT_STATUS_IS_OK(result)) {
774 goto done;
777 done:
778 if (!NT_STATUS_IS_OK(result)) {
779 d_printf("Failed to rename user from %s to %s - %s\n", old_name, new_name,
780 nt_errstr(result));
781 } else {
782 d_printf("Renamed user from %s to %s\n", old_name, new_name);
784 return result;
788 /**
789 * Rename a user on a remote RPC server
791 * @param argc Standard main() style argc
792 * @param argv Standard main() style argv. Initial components are already
793 * stripped
795 * @return A shell status integer (0 for success)
798 static int rpc_user_rename(int argc, const char **argv)
800 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
801 argc, argv);
804 /**
805 * Delete a user from a remote RPC server
807 * @param argc Standard main() style argc
808 * @param argv Standard main() style argv. Initial components are already
809 * stripped
811 * @return A shell status integer (0 for success)
814 static int rpc_user_delete(int argc, const char **argv)
816 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
817 argc, argv);
820 /**
821 * Set a password for a user on a remote RPC server
823 * All parameters are provided by the run_rpc_command function, except for
824 * argc, argv which are passes through.
826 * @param domain_sid The domain sid acquired from the remote server
827 * @param cli A cli_state connected to the server.
828 * @param mem_ctx Talloc context, destoyed on completion of the function.
829 * @param argc Standard main() style argc
830 * @param argv Standard main() style argv. Initial components are already
831 * stripped
833 * @return Normal NTSTATUS return.
836 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
837 const char *domain_name,
838 struct cli_state *cli,
839 TALLOC_CTX *mem_ctx,
840 int argc, const char **argv)
842 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
843 POLICY_HND connect_pol, domain_pol, user_pol;
844 SAM_USERINFO_CTR ctr;
845 SAM_USER_INFO_24 p24;
846 uchar pwbuf[516];
847 const char *user;
848 const char *new_password;
849 char *prompt = NULL;
851 if (argc < 1) {
852 d_printf("User must be specified\n");
853 rpc_user_usage(argc, argv);
854 return NT_STATUS_OK;
857 user = argv[0];
859 if (argv[1]) {
860 new_password = argv[1];
861 } else {
862 asprintf(&prompt, "Enter new password for %s:", user);
863 new_password = getpass(prompt);
864 SAFE_FREE(prompt);
867 /* Get sam policy and domain handles */
869 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
870 &connect_pol);
872 if (!NT_STATUS_IS_OK(result)) {
873 goto done;
876 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
877 MAXIMUM_ALLOWED_ACCESS,
878 domain_sid, &domain_pol);
880 if (!NT_STATUS_IS_OK(result)) {
881 goto done;
884 /* Get handle on user */
887 uint32 *user_rids, num_rids, *name_types;
888 uint32 flags = 0x000003e8; /* Unknown */
890 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
891 flags, 1, &user,
892 &num_rids, &user_rids,
893 &name_types);
895 if (!NT_STATUS_IS_OK(result)) {
896 goto done;
899 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
900 MAXIMUM_ALLOWED_ACCESS,
901 user_rids[0], &user_pol);
903 if (!NT_STATUS_IS_OK(result)) {
904 goto done;
908 /* Set password on account */
910 ZERO_STRUCT(ctr);
911 ZERO_STRUCT(p24);
913 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
915 init_sam_user_info24(&p24, (char *)pwbuf,24);
917 ctr.switch_value = 24;
918 ctr.info.id24 = &p24;
920 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
921 &cli->user_session_key, &ctr);
923 if (!NT_STATUS_IS_OK(result)) {
924 goto done;
927 /* Display results */
929 done:
930 return result;
934 /**
935 * Set a user's password on a remote RPC server
937 * @param argc Standard main() style argc
938 * @param argv Standard main() style argv. Initial components are already
939 * stripped
941 * @return A shell status integer (0 for success)
944 static int rpc_user_password(int argc, const char **argv)
946 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
947 argc, argv);
950 /**
951 * List user's groups on a remote RPC server
953 * All parameters are provided by the run_rpc_command function, except for
954 * argc, argv which are passes through.
956 * @param domain_sid The domain sid acquired from the remote server
957 * @param cli A cli_state connected to the server.
958 * @param mem_ctx Talloc context, destoyed on completion of the function.
959 * @param argc Standard main() style argc
960 * @param argv Standard main() style argv. Initial components are already
961 * stripped
963 * @return Normal NTSTATUS return.
966 static NTSTATUS
967 rpc_user_info_internals(const DOM_SID *domain_sid, const char *domain_name,
968 struct cli_state *cli,
969 TALLOC_CTX *mem_ctx, int argc, const char **argv)
971 POLICY_HND connect_pol, domain_pol, user_pol;
972 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
973 uint32 *rids, num_rids, *name_types, num_names;
974 uint32 flags = 0x000003e8; /* Unknown */
975 int i;
976 char **names;
977 DOM_GID *user_gids;
979 if (argc < 1) {
980 d_printf("User must be specified\n");
981 rpc_user_usage(argc, argv);
982 return NT_STATUS_OK;
984 /* Get sam policy handle */
986 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
987 &connect_pol);
988 if (!NT_STATUS_IS_OK(result)) goto done;
990 /* Get domain policy handle */
992 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
993 MAXIMUM_ALLOWED_ACCESS,
994 domain_sid, &domain_pol);
995 if (!NT_STATUS_IS_OK(result)) goto done;
997 /* Get handle on user */
999 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1000 flags, 1, &argv[0],
1001 &num_rids, &rids, &name_types);
1003 if (!NT_STATUS_IS_OK(result)) goto done;
1005 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1006 MAXIMUM_ALLOWED_ACCESS,
1007 rids[0], &user_pol);
1008 if (!NT_STATUS_IS_OK(result)) goto done;
1010 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
1011 &num_rids, &user_gids);
1013 if (!NT_STATUS_IS_OK(result)) goto done;
1015 /* Look up rids */
1017 if (num_rids) {
1018 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1020 for (i = 0; i < num_rids; i++)
1021 rids[i] = user_gids[i].g_rid;
1023 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
1024 num_rids, rids,
1025 &num_names, &names, &name_types);
1027 if (!NT_STATUS_IS_OK(result)) {
1028 goto done;
1031 /* Display results */
1033 for (i = 0; i < num_names; i++)
1034 printf("%s\n", names[i]);
1036 done:
1037 return result;
1040 /**
1041 * List a user's groups from a remote RPC server
1043 * @param argc Standard main() style argc
1044 * @param argv Standard main() style argv. Initial components are already
1045 * stripped
1047 * @return A shell status integer (0 for success)
1050 static int rpc_user_info(int argc, const char **argv)
1052 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1053 argc, argv);
1056 /**
1057 * List users on a remote RPC server
1059 * All parameters are provided by the run_rpc_command function, except for
1060 * argc, argv which are passes through.
1062 * @param domain_sid The domain sid acquired from the remote server
1063 * @param cli A cli_state connected to the server.
1064 * @param mem_ctx Talloc context, destoyed on completion of the function.
1065 * @param argc Standard main() style argc
1066 * @param argv Standard main() style argv. Initial components are already
1067 * stripped
1069 * @return Normal NTSTATUS return.
1072 static NTSTATUS
1073 rpc_user_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1074 struct cli_state *cli,
1075 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1077 POLICY_HND connect_pol, domain_pol;
1078 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1079 uint32 start_idx=0, num_entries, i, loop_count = 0;
1080 SAM_DISPINFO_CTR ctr;
1081 SAM_DISPINFO_1 info1;
1083 /* Get sam policy handle */
1085 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1086 &connect_pol);
1087 if (!NT_STATUS_IS_OK(result)) {
1088 goto done;
1091 /* Get domain policy handle */
1093 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1094 MAXIMUM_ALLOWED_ACCESS,
1095 domain_sid, &domain_pol);
1096 if (!NT_STATUS_IS_OK(result)) {
1097 goto done;
1100 /* Query domain users */
1101 ZERO_STRUCT(ctr);
1102 ZERO_STRUCT(info1);
1103 ctr.sam.info1 = &info1;
1104 if (opt_long_list_entries)
1105 d_printf("\nUser name Comment"\
1106 "\n-----------------------------\n");
1107 do {
1108 fstring user, desc;
1109 uint32 max_entries, max_size;
1111 get_query_dispinfo_params(
1112 loop_count, &max_entries, &max_size);
1114 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1115 &start_idx, 1, &num_entries,
1116 max_entries, max_size, &ctr);
1117 loop_count++;
1119 for (i = 0; i < num_entries; i++) {
1120 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1121 if (opt_long_list_entries)
1122 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1124 if (opt_long_list_entries)
1125 printf("%-21.21s %s\n", user, desc);
1126 else
1127 printf("%s\n", user);
1129 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1131 done:
1132 return result;
1135 /**
1136 * 'net rpc user' entrypoint.
1137 * @param argc Standard main() style argc
1138 * @param argc Standard main() style argv. Initial components are already
1139 * stripped
1142 int net_rpc_user(int argc, const char **argv)
1144 struct functable func[] = {
1145 {"add", rpc_user_add},
1146 {"info", rpc_user_info},
1147 {"delete", rpc_user_delete},
1148 {"password", rpc_user_password},
1149 {"rename", rpc_user_rename},
1150 {NULL, NULL}
1153 if (argc == 0) {
1154 if (opt_long_list_entries) {
1155 } else {
1157 return run_rpc_command(NULL,PI_SAMR, 0,
1158 rpc_user_list_internals,
1159 argc, argv);
1162 return net_run_function(argc, argv, func, rpc_user_usage);
1166 /****************************************************************************/
1169 * Basic usage function for 'net rpc group'
1170 * @param argc Standard main() style argc.
1171 * @param argv Standard main() style argv. Initial components are already
1172 * stripped.
1175 static int rpc_group_usage(int argc, const char **argv)
1177 return net_help_group(argc, argv);
1181 * Delete group on a remote RPC server
1183 * All parameters are provided by the run_rpc_command function, except for
1184 * argc, argv which are passes through.
1186 * @param domain_sid The domain sid acquired from the remote server
1187 * @param cli A cli_state connected to the server.
1188 * @param mem_ctx Talloc context, destoyed on completion of the function.
1189 * @param argc Standard main() style argc
1190 * @param argv Standard main() style argv. Initial components are already
1191 * stripped
1193 * @return Normal NTSTATUS return.
1196 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1197 const char *domain_name,
1198 struct cli_state *cli,
1199 TALLOC_CTX *mem_ctx,
1200 int argc, const char **argv)
1202 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1203 BOOL group_is_primary = False;
1204 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1206 uint32 *group_rids, num_rids, *name_types, num_members,
1207 *group_attrs, group_rid;
1208 uint32 flags = 0x000003e8; /* Unknown */
1209 /* char **names; */
1210 int i;
1211 /* DOM_GID *user_gids; */
1212 SAM_USERINFO_CTR *user_ctr;
1213 fstring temp;
1215 if (argc < 1) {
1216 d_printf("specify group\n");
1217 rpc_group_usage(argc,argv);
1218 return NT_STATUS_OK; /* ok? */
1221 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1222 &connect_pol);
1224 if (!NT_STATUS_IS_OK(result)) {
1225 d_printf("Request samr_connect failed\n");
1226 goto done;
1229 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1230 MAXIMUM_ALLOWED_ACCESS,
1231 domain_sid, &domain_pol);
1233 if (!NT_STATUS_IS_OK(result)) {
1234 d_printf("Request open_domain failed\n");
1235 goto done;
1238 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1239 flags, 1, &argv[0],
1240 &num_rids, &group_rids,
1241 &name_types);
1243 if (!NT_STATUS_IS_OK(result)) {
1244 d_printf("Lookup of '%s' failed\n",argv[0]);
1245 goto done;
1248 switch (name_types[0])
1250 case SID_NAME_DOM_GRP:
1251 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1252 MAXIMUM_ALLOWED_ACCESS,
1253 group_rids[0], &group_pol);
1254 if (!NT_STATUS_IS_OK(result)) {
1255 d_printf("Request open_group failed");
1256 goto done;
1259 group_rid = group_rids[0];
1261 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1262 &num_members, &group_rids,
1263 &group_attrs);
1265 if (!NT_STATUS_IS_OK(result)) {
1266 d_printf("Unable to query group members of %s",argv[0]);
1267 goto done;
1270 if (opt_verbose) {
1271 d_printf("Domain Group %s (rid: %d) has %d members\n",
1272 argv[0],group_rid,num_members);
1275 /* Check if group is anyone's primary group */
1276 for (i = 0; i < num_members; i++)
1278 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1279 MAXIMUM_ALLOWED_ACCESS,
1280 group_rids[i], &user_pol);
1282 if (!NT_STATUS_IS_OK(result)) {
1283 d_printf("Unable to open group member %d\n",group_rids[i]);
1284 goto done;
1287 ZERO_STRUCT(user_ctr);
1289 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
1290 21, &user_ctr);
1292 if (!NT_STATUS_IS_OK(result)) {
1293 d_printf("Unable to lookup userinfo for group member %d\n",group_rids[i]);
1294 goto done;
1297 if (user_ctr->info.id21->group_rid == group_rid) {
1298 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1299 sizeof(temp)-1);
1300 if (opt_verbose)
1301 d_printf("Group is primary group of %s\n",temp);
1302 group_is_primary = True;
1305 cli_samr_close(cli, mem_ctx, &user_pol);
1308 if (group_is_primary) {
1309 d_printf("Unable to delete group because some of it's "
1310 "members have it as primary group\n");
1311 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1312 goto done;
1315 /* remove all group members */
1316 for (i = 0; i < num_members; i++)
1318 if (opt_verbose)
1319 d_printf("Remove group member %d...",group_rids[i]);
1320 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, group_rids[i]);
1322 if (NT_STATUS_IS_OK(result)) {
1323 if (opt_verbose)
1324 d_printf("ok\n");
1325 } else {
1326 if (opt_verbose)
1327 d_printf("failed\n");
1328 goto done;
1332 result = cli_samr_delete_dom_group(cli, mem_ctx, &group_pol);
1334 break;
1335 /* removing a local group is easier... */
1336 case SID_NAME_ALIAS:
1337 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1338 MAXIMUM_ALLOWED_ACCESS,
1339 group_rids[0], &group_pol);
1341 if (!NT_STATUS_IS_OK(result)) {
1342 d_printf("Request open_alias failed\n");
1343 goto done;
1346 result = cli_samr_delete_dom_alias(cli, mem_ctx, &group_pol);
1347 break;
1348 default:
1349 d_printf("%s is of type %s. This command is only for deleting local or global groups\n",
1350 argv[0],sid_type_lookup(name_types[0]));
1351 result = NT_STATUS_UNSUCCESSFUL;
1352 goto done;
1356 if (NT_STATUS_IS_OK(result)) {
1357 if (opt_verbose)
1358 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1359 } else {
1360 d_printf("Deleting of %s failed: %s\n",argv[0],
1361 get_friendly_nt_error_msg(result));
1364 done:
1365 return result;
1369 static int rpc_group_delete(int argc, const char **argv)
1371 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1372 argc,argv);
1375 static NTSTATUS
1376 rpc_group_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1377 struct cli_state *cli,
1378 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1380 POLICY_HND connect_pol, domain_pol, group_pol;
1381 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1382 GROUP_INFO_CTR group_info;
1384 if (argc != 1) {
1385 d_printf("Group name must be specified\n");
1386 rpc_group_usage(argc, argv);
1387 return NT_STATUS_OK;
1390 /* Get sam policy handle */
1392 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1393 &connect_pol);
1394 if (!NT_STATUS_IS_OK(result)) goto done;
1396 /* Get domain policy handle */
1398 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1399 MAXIMUM_ALLOWED_ACCESS,
1400 domain_sid, &domain_pol);
1401 if (!NT_STATUS_IS_OK(result)) goto done;
1403 /* Create the group */
1405 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1406 argv[0], MAXIMUM_ALLOWED_ACCESS,
1407 &group_pol);
1408 if (!NT_STATUS_IS_OK(result)) goto done;
1410 if (strlen(opt_comment) == 0) goto done;
1412 /* We've got a comment to set */
1414 group_info.switch_value1 = 4;
1415 init_samr_group_info4(&group_info.group.info4, opt_comment);
1417 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &group_info);
1418 if (!NT_STATUS_IS_OK(result)) goto done;
1420 done:
1421 if (NT_STATUS_IS_OK(result))
1422 DEBUG(5, ("add group succeeded\n"));
1423 else
1424 d_printf("add group failed: %s\n", nt_errstr(result));
1426 return result;
1429 static NTSTATUS
1430 rpc_alias_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1431 struct cli_state *cli,
1432 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1434 POLICY_HND connect_pol, domain_pol, alias_pol;
1435 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1436 ALIAS_INFO_CTR alias_info;
1438 if (argc != 1) {
1439 d_printf("Alias name must be specified\n");
1440 rpc_group_usage(argc, argv);
1441 return NT_STATUS_OK;
1444 /* Get sam policy handle */
1446 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1447 &connect_pol);
1448 if (!NT_STATUS_IS_OK(result)) goto done;
1450 /* Get domain policy handle */
1452 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1453 MAXIMUM_ALLOWED_ACCESS,
1454 domain_sid, &domain_pol);
1455 if (!NT_STATUS_IS_OK(result)) goto done;
1457 /* Create the group */
1459 result = cli_samr_create_dom_alias(cli, mem_ctx, &domain_pol,
1460 argv[0], &alias_pol);
1461 if (!NT_STATUS_IS_OK(result)) goto done;
1463 if (strlen(opt_comment) == 0) goto done;
1465 /* We've got a comment to set */
1467 alias_info.level = 3;
1468 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1470 result = cli_samr_set_aliasinfo(cli, mem_ctx, &alias_pol, &alias_info);
1471 if (!NT_STATUS_IS_OK(result)) goto done;
1473 done:
1474 if (NT_STATUS_IS_OK(result))
1475 DEBUG(5, ("add alias succeeded\n"));
1476 else
1477 d_printf("add alias failed: %s\n", nt_errstr(result));
1479 return result;
1482 static int rpc_group_add(int argc, const char **argv)
1484 if (opt_localgroup)
1485 return run_rpc_command(NULL, PI_SAMR, 0,
1486 rpc_alias_add_internals,
1487 argc, argv);
1489 return run_rpc_command(NULL, PI_SAMR, 0,
1490 rpc_group_add_internals,
1491 argc, argv);
1494 static NTSTATUS
1495 get_sid_from_name(struct cli_state *cli, TALLOC_CTX *mem_ctx, const char *name,
1496 DOM_SID *sid, enum SID_NAME_USE *type)
1498 int current_pipe = cli->pipe_idx;
1500 DOM_SID *sids = NULL;
1501 uint32 *types = NULL;
1502 POLICY_HND lsa_pol;
1503 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1505 if (current_pipe != PI_LSARPC) {
1507 if (current_pipe != -1)
1508 cli_nt_session_close(cli);
1510 if (!cli_nt_session_open(cli, PI_LSARPC))
1511 goto done;
1514 result = cli_lsa_open_policy(cli, mem_ctx, False,
1515 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1517 if (!NT_STATUS_IS_OK(result))
1518 goto done;
1520 result = cli_lsa_lookup_names(cli, mem_ctx, &lsa_pol, 1,
1521 &name, &sids, &types);
1523 if (NT_STATUS_IS_OK(result)) {
1524 sid_copy(sid, &sids[0]);
1525 *type = types[0];
1528 cli_lsa_close(cli, mem_ctx, &lsa_pol);
1530 done:
1531 if (current_pipe != PI_LSARPC) {
1532 cli_nt_session_close(cli);
1533 if (current_pipe != -1)
1534 cli_nt_session_open(cli, current_pipe);
1537 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1539 /* Try as S-1-5-whatever */
1541 DOM_SID tmp_sid;
1543 if (string_to_sid(&tmp_sid, name)) {
1544 sid_copy(sid, &tmp_sid);
1545 *type = SID_NAME_UNKNOWN;
1546 result = NT_STATUS_OK;
1550 return result;
1553 static NTSTATUS
1554 rpc_add_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1555 const DOM_SID *group_sid, const char *member)
1557 POLICY_HND connect_pol, domain_pol;
1558 NTSTATUS result;
1559 uint32 group_rid;
1560 POLICY_HND group_pol;
1562 uint32 num_rids;
1563 uint32 *rids = NULL;
1564 uint32 *rid_types = NULL;
1566 DOM_SID sid;
1568 sid_copy(&sid, group_sid);
1570 if (!sid_split_rid(&sid, &group_rid))
1571 return NT_STATUS_UNSUCCESSFUL;
1573 /* Get sam policy handle */
1574 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1575 &connect_pol);
1576 if (!NT_STATUS_IS_OK(result))
1577 return result;
1579 /* Get domain policy handle */
1580 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1581 MAXIMUM_ALLOWED_ACCESS,
1582 &sid, &domain_pol);
1583 if (!NT_STATUS_IS_OK(result))
1584 return result;
1586 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1587 1, &member,
1588 &num_rids, &rids, &rid_types);
1590 if (!NT_STATUS_IS_OK(result)) {
1591 d_printf("Could not lookup up group member %s\n", member);
1592 goto done;
1595 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1596 MAXIMUM_ALLOWED_ACCESS,
1597 group_rid, &group_pol);
1599 if (!NT_STATUS_IS_OK(result))
1600 goto done;
1602 result = cli_samr_add_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1604 done:
1605 cli_samr_close(cli, mem_ctx, &connect_pol);
1606 return result;
1609 static NTSTATUS
1610 rpc_add_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1611 const DOM_SID *alias_sid, const char *member)
1613 POLICY_HND connect_pol, domain_pol;
1614 NTSTATUS result;
1615 uint32 alias_rid;
1616 POLICY_HND alias_pol;
1618 DOM_SID member_sid;
1619 enum SID_NAME_USE member_type;
1621 DOM_SID sid;
1623 sid_copy(&sid, alias_sid);
1625 if (!sid_split_rid(&sid, &alias_rid))
1626 return NT_STATUS_UNSUCCESSFUL;
1628 result = get_sid_from_name(cli, mem_ctx, member,
1629 &member_sid, &member_type);
1631 if (!NT_STATUS_IS_OK(result)) {
1632 d_printf("Could not lookup up group member %s\n", member);
1633 return result;
1636 /* Get sam policy handle */
1637 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1638 &connect_pol);
1639 if (!NT_STATUS_IS_OK(result)) {
1640 goto done;
1643 /* Get domain policy handle */
1644 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1645 MAXIMUM_ALLOWED_ACCESS,
1646 &sid, &domain_pol);
1647 if (!NT_STATUS_IS_OK(result)) {
1648 goto done;
1651 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1652 MAXIMUM_ALLOWED_ACCESS,
1653 alias_rid, &alias_pol);
1655 if (!NT_STATUS_IS_OK(result))
1656 return result;
1658 result = cli_samr_add_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1660 if (!NT_STATUS_IS_OK(result))
1661 return result;
1663 done:
1664 cli_samr_close(cli, mem_ctx, &connect_pol);
1665 return result;
1668 static NTSTATUS
1669 rpc_group_addmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1670 struct cli_state *cli,
1671 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1673 DOM_SID group_sid;
1674 enum SID_NAME_USE group_type;
1676 if (argc != 2) {
1677 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
1678 return NT_STATUS_UNSUCCESSFUL;
1681 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1682 &group_sid, &group_type))) {
1683 d_printf("Could not lookup group name %s\n", argv[0]);
1684 return NT_STATUS_UNSUCCESSFUL;
1687 if (group_type == SID_NAME_DOM_GRP) {
1688 NTSTATUS result = rpc_add_groupmem(cli, mem_ctx,
1689 &group_sid, argv[1]);
1691 if (!NT_STATUS_IS_OK(result)) {
1692 d_printf("Could not add %s to %s: %s\n",
1693 argv[1], argv[0], nt_errstr(result));
1695 return result;
1698 if (group_type == SID_NAME_ALIAS) {
1699 NTSTATUS result = rpc_add_aliasmem(cli, mem_ctx,
1700 &group_sid, argv[1]);
1702 if (!NT_STATUS_IS_OK(result)) {
1703 d_printf("Could not add %s to %s: %s\n",
1704 argv[1], argv[0], nt_errstr(result));
1706 return result;
1709 d_printf("Can only add members to global or local groups which "
1710 "%s is not\n", argv[0]);
1712 return NT_STATUS_UNSUCCESSFUL;
1715 static int rpc_group_addmem(int argc, const char **argv)
1717 return run_rpc_command(NULL, PI_SAMR, 0,
1718 rpc_group_addmem_internals,
1719 argc, argv);
1722 static NTSTATUS
1723 rpc_del_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1724 const DOM_SID *group_sid, const char *member)
1726 POLICY_HND connect_pol, domain_pol;
1727 NTSTATUS result;
1728 uint32 group_rid;
1729 POLICY_HND group_pol;
1731 uint32 num_rids;
1732 uint32 *rids = NULL;
1733 uint32 *rid_types = NULL;
1735 DOM_SID sid;
1737 sid_copy(&sid, group_sid);
1739 if (!sid_split_rid(&sid, &group_rid))
1740 return NT_STATUS_UNSUCCESSFUL;
1742 /* Get sam policy handle */
1743 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1744 &connect_pol);
1745 if (!NT_STATUS_IS_OK(result))
1746 return result;
1748 /* Get domain policy handle */
1749 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1750 MAXIMUM_ALLOWED_ACCESS,
1751 &sid, &domain_pol);
1752 if (!NT_STATUS_IS_OK(result))
1753 return result;
1755 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1756 1, &member,
1757 &num_rids, &rids, &rid_types);
1759 if (!NT_STATUS_IS_OK(result)) {
1760 d_printf("Could not lookup up group member %s\n", member);
1761 goto done;
1764 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1765 MAXIMUM_ALLOWED_ACCESS,
1766 group_rid, &group_pol);
1768 if (!NT_STATUS_IS_OK(result))
1769 goto done;
1771 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1773 done:
1774 cli_samr_close(cli, mem_ctx, &connect_pol);
1775 return result;
1778 static NTSTATUS
1779 rpc_del_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1780 const DOM_SID *alias_sid, const char *member)
1782 POLICY_HND connect_pol, domain_pol;
1783 NTSTATUS result;
1784 uint32 alias_rid;
1785 POLICY_HND alias_pol;
1787 DOM_SID member_sid;
1788 enum SID_NAME_USE member_type;
1790 DOM_SID sid;
1792 sid_copy(&sid, alias_sid);
1794 if (!sid_split_rid(&sid, &alias_rid))
1795 return NT_STATUS_UNSUCCESSFUL;
1797 result = get_sid_from_name(cli, mem_ctx, member,
1798 &member_sid, &member_type);
1800 if (!NT_STATUS_IS_OK(result)) {
1801 d_printf("Could not lookup up group member %s\n", member);
1802 return result;
1805 /* Get sam policy handle */
1806 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1807 &connect_pol);
1808 if (!NT_STATUS_IS_OK(result)) {
1809 goto done;
1812 /* Get domain policy handle */
1813 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1814 MAXIMUM_ALLOWED_ACCESS,
1815 &sid, &domain_pol);
1816 if (!NT_STATUS_IS_OK(result)) {
1817 goto done;
1820 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1821 MAXIMUM_ALLOWED_ACCESS,
1822 alias_rid, &alias_pol);
1824 if (!NT_STATUS_IS_OK(result))
1825 return result;
1827 result = cli_samr_del_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1829 if (!NT_STATUS_IS_OK(result))
1830 return result;
1832 done:
1833 cli_samr_close(cli, mem_ctx, &connect_pol);
1834 return result;
1837 static NTSTATUS
1838 rpc_group_delmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1839 struct cli_state *cli,
1840 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1842 DOM_SID group_sid;
1843 enum SID_NAME_USE group_type;
1845 if (argc != 2) {
1846 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1847 return NT_STATUS_UNSUCCESSFUL;
1850 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1851 &group_sid, &group_type))) {
1852 d_printf("Could not lookup group name %s\n", argv[0]);
1853 return NT_STATUS_UNSUCCESSFUL;
1856 if (group_type == SID_NAME_DOM_GRP) {
1857 NTSTATUS result = rpc_del_groupmem(cli, mem_ctx,
1858 &group_sid, argv[1]);
1860 if (!NT_STATUS_IS_OK(result)) {
1861 d_printf("Could not del %s from %s: %s\n",
1862 argv[1], argv[0], nt_errstr(result));
1864 return result;
1867 if (group_type == SID_NAME_ALIAS) {
1868 NTSTATUS result = rpc_del_aliasmem(cli, mem_ctx,
1869 &group_sid, argv[1]);
1871 if (!NT_STATUS_IS_OK(result)) {
1872 d_printf("Could not del %s from %s: %s\n",
1873 argv[1], argv[0], nt_errstr(result));
1875 return result;
1878 d_printf("Can only delete members from global or local groups which "
1879 "%s is not\n", argv[0]);
1881 return NT_STATUS_UNSUCCESSFUL;
1884 static int rpc_group_delmem(int argc, const char **argv)
1886 return run_rpc_command(NULL, PI_SAMR, 0,
1887 rpc_group_delmem_internals,
1888 argc, argv);
1891 /**
1892 * List groups on a remote RPC server
1894 * All parameters are provided by the run_rpc_command function, except for
1895 * argc, argv which are passes through.
1897 * @param domain_sid The domain sid acquired from the remote server
1898 * @param cli A cli_state connected to the server.
1899 * @param mem_ctx Talloc context, destoyed on completion of the function.
1900 * @param argc Standard main() style argc
1901 * @param argv Standard main() style argv. Initial components are already
1902 * stripped
1904 * @return Normal NTSTATUS return.
1907 static NTSTATUS
1908 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1909 struct cli_state *cli,
1910 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1912 POLICY_HND connect_pol, domain_pol;
1913 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1914 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1915 struct acct_info *groups;
1916 BOOL global = False;
1917 BOOL local = False;
1918 BOOL builtin = False;
1920 if (argc == 0) {
1921 global = True;
1922 local = True;
1923 builtin = True;
1926 for (i=0; i<argc; i++) {
1927 if (strequal(argv[i], "global"))
1928 global = True;
1930 if (strequal(argv[i], "local"))
1931 local = True;
1933 if (strequal(argv[i], "builtin"))
1934 builtin = True;
1937 /* Get sam policy handle */
1939 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1940 &connect_pol);
1941 if (!NT_STATUS_IS_OK(result)) {
1942 goto done;
1945 /* Get domain policy handle */
1947 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1948 MAXIMUM_ALLOWED_ACCESS,
1949 domain_sid, &domain_pol);
1950 if (!NT_STATUS_IS_OK(result)) {
1951 goto done;
1954 /* Query domain groups */
1955 if (opt_long_list_entries)
1956 d_printf("\nGroup name Comment"\
1957 "\n-----------------------------\n");
1958 do {
1959 SAM_DISPINFO_CTR ctr;
1960 SAM_DISPINFO_3 info3;
1961 uint32 max_size;
1963 ZERO_STRUCT(ctr);
1964 ZERO_STRUCT(info3);
1965 ctr.sam.info3 = &info3;
1967 if (!global) break;
1969 get_query_dispinfo_params(
1970 loop_count, &max_entries, &max_size);
1972 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1973 &start_idx, 3, &num_entries,
1974 max_entries, max_size, &ctr);
1976 if (!NT_STATUS_IS_OK(result) &&
1977 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1978 break;
1980 for (i = 0; i < num_entries; i++) {
1982 fstring group, desc;
1984 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1985 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1987 if (opt_long_list_entries)
1988 printf("%-21.21s %-50.50s\n",
1989 group, desc);
1990 else
1991 printf("%s\n", group);
1993 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1994 /* query domain aliases */
1995 start_idx = 0;
1996 do {
1997 if (!local) break;
1999 /* The max_size field in cli_samr_enum_als_groups is more like
2000 * an account_control field with indiviual bits what to
2001 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2002 * everything. I'm too lazy (sorry) to get this through to
2003 * rpc_parse/ etc. Volker */
2005 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2006 &start_idx, 0xffff,
2007 &groups, &num_entries);
2009 if (!NT_STATUS_IS_OK(result) &&
2010 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2011 break;
2013 for (i = 0; i < num_entries; i++) {
2015 char *description = NULL;
2017 if (opt_long_list_entries) {
2019 POLICY_HND alias_pol;
2020 ALIAS_INFO_CTR ctr;
2022 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2023 &domain_pol,
2024 0x8,
2025 groups[i].rid,
2026 &alias_pol))) &&
2027 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2028 &alias_pol, 3,
2029 &ctr))) &&
2030 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2031 &alias_pol)))) {
2032 description = unistr2_tdup(mem_ctx,
2033 ctr.alias.info3.description.string);
2037 if (description != NULL) {
2038 printf("%-21.21s %-50.50s\n",
2039 groups[i].acct_name,
2040 description);
2041 } else {
2042 printf("%s\n", groups[i].acct_name);
2045 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2046 cli_samr_close(cli, mem_ctx, &domain_pol);
2047 /* Get builtin policy handle */
2049 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2050 MAXIMUM_ALLOWED_ACCESS,
2051 &global_sid_Builtin, &domain_pol);
2052 if (!NT_STATUS_IS_OK(result)) {
2053 goto done;
2055 /* query builtin aliases */
2056 start_idx = 0;
2057 do {
2058 if (!builtin) break;
2060 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2061 &start_idx, max_entries,
2062 &groups, &num_entries);
2064 if (!NT_STATUS_IS_OK(result) &&
2065 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2066 break;
2068 for (i = 0; i < num_entries; i++) {
2070 char *description = NULL;
2072 if (opt_long_list_entries) {
2074 POLICY_HND alias_pol;
2075 ALIAS_INFO_CTR ctr;
2077 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2078 &domain_pol,
2079 0x8,
2080 groups[i].rid,
2081 &alias_pol))) &&
2082 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2083 &alias_pol, 3,
2084 &ctr))) &&
2085 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2086 &alias_pol)))) {
2087 description = unistr2_tdup(mem_ctx,
2088 ctr.alias.info3.description.string);
2092 if (description != NULL) {
2093 printf("%-21.21s %-50.50s\n",
2094 groups[i].acct_name,
2095 description);
2096 } else {
2097 printf("%s\n", groups[i].acct_name);
2100 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2102 done:
2103 return result;
2106 static int rpc_group_list(int argc, const char **argv)
2108 return run_rpc_command(NULL, PI_SAMR, 0,
2109 rpc_group_list_internals,
2110 argc, argv);
2113 static NTSTATUS
2114 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2115 const char *domain_name, const DOM_SID *domain_sid,
2116 POLICY_HND *domain_pol, uint32 rid)
2118 NTSTATUS result;
2119 POLICY_HND group_pol;
2120 uint32 num_members, *group_rids, *group_attrs;
2121 uint32 num_names;
2122 char **names;
2123 uint32 *name_types;
2124 int i;
2126 fstring sid_str;
2127 sid_to_string(sid_str, domain_sid);
2129 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
2130 MAXIMUM_ALLOWED_ACCESS,
2131 rid, &group_pol);
2133 if (!NT_STATUS_IS_OK(result))
2134 return result;
2136 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
2137 &num_members, &group_rids,
2138 &group_attrs);
2140 if (!NT_STATUS_IS_OK(result))
2141 return result;
2143 while (num_members > 0) {
2144 int this_time = 512;
2146 if (num_members < this_time)
2147 this_time = num_members;
2149 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol,
2150 this_time, group_rids,
2151 &num_names, &names, &name_types);
2153 if (!NT_STATUS_IS_OK(result))
2154 return result;
2156 /* We only have users as members, but make the output
2157 the same as the output of alias members */
2159 for (i = 0; i < this_time; i++) {
2161 if (opt_long_list_entries) {
2162 printf("%s-%d %s\\%s %d\n", sid_str,
2163 group_rids[i], domain_name, names[i],
2164 SID_NAME_USER);
2165 } else {
2166 printf("%s\\%s\n", domain_name, names[i]);
2170 num_members -= this_time;
2171 group_rids += 512;
2174 return NT_STATUS_OK;
2177 static NTSTATUS
2178 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2179 POLICY_HND *domain_pol, uint32 rid)
2181 NTSTATUS result;
2182 POLICY_HND alias_pol, lsa_pol;
2183 uint32 num_members;
2184 DOM_SID *alias_sids;
2185 char **domains;
2186 char **names;
2187 uint32 *types;
2188 int i;
2190 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
2191 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2193 if (!NT_STATUS_IS_OK(result))
2194 return result;
2196 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
2197 &num_members, &alias_sids);
2199 if (!NT_STATUS_IS_OK(result)) {
2200 d_printf("Couldn't list alias members\n");
2201 return result;
2204 if (num_members == 0) {
2205 return NT_STATUS_OK;
2208 cli_nt_session_close(cli);
2210 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2211 d_printf("Couldn't open LSA pipe\n");
2212 return result;
2215 result = cli_lsa_open_policy(cli, mem_ctx, True,
2216 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2218 if (!NT_STATUS_IS_OK(result)) {
2219 d_printf("Couldn't open LSA policy handle\n");
2220 return result;
2223 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
2224 alias_sids,
2225 &domains, &names, &types);
2227 if (!NT_STATUS_IS_OK(result) &&
2228 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2229 d_printf("Couldn't lookup SIDs\n");
2230 return result;
2233 for (i = 0; i < num_members; i++) {
2234 fstring sid_str;
2235 sid_to_string(sid_str, &alias_sids[i]);
2237 if (opt_long_list_entries) {
2238 printf("%s %s\\%s %d\n", sid_str,
2239 domains[i] ? domains[i] : "*unknown*",
2240 names[i] ? names[i] : "*unknown*", types[i]);
2241 } else {
2242 if (domains[i])
2243 printf("%s\\%s\n", domains[i], names[i]);
2244 else
2245 printf("%s\n", sid_str);
2249 return NT_STATUS_OK;
2252 static NTSTATUS
2253 rpc_group_members_internals(const DOM_SID *domain_sid,
2254 const char *domain_name,
2255 struct cli_state *cli,
2256 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2258 NTSTATUS result;
2259 POLICY_HND connect_pol, domain_pol;
2260 uint32 num_rids, *rids, *rid_types;
2262 /* Get sam policy handle */
2264 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2265 &connect_pol);
2267 if (!NT_STATUS_IS_OK(result))
2268 return result;
2270 /* Get domain policy handle */
2272 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2273 MAXIMUM_ALLOWED_ACCESS,
2274 domain_sid, &domain_pol);
2276 if (!NT_STATUS_IS_OK(result))
2277 return result;
2279 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2280 1, argv, &num_rids, &rids, &rid_types);
2282 if (!NT_STATUS_IS_OK(result)) {
2284 /* Ok, did not find it in the global sam, try with builtin */
2286 DOM_SID sid_Builtin;
2288 cli_samr_close(cli, mem_ctx, &domain_pol);
2290 string_to_sid(&sid_Builtin, "S-1-5-32");
2292 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2293 MAXIMUM_ALLOWED_ACCESS,
2294 &sid_Builtin, &domain_pol);
2296 if (!NT_STATUS_IS_OK(result)) {
2297 d_printf("Couldn't find group %s\n", argv[0]);
2298 return result;
2301 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2302 1, argv, &num_rids,
2303 &rids, &rid_types);
2305 if (!NT_STATUS_IS_OK(result)) {
2306 d_printf("Couldn't find group %s\n", argv[0]);
2307 return result;
2311 if (num_rids != 1) {
2312 d_printf("Couldn't find group %s\n", argv[0]);
2313 return result;
2316 if (rid_types[0] == SID_NAME_DOM_GRP) {
2317 return rpc_list_group_members(cli, mem_ctx, domain_name,
2318 domain_sid, &domain_pol,
2319 rids[0]);
2322 if (rid_types[0] == SID_NAME_ALIAS) {
2323 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
2324 rids[0]);
2327 return NT_STATUS_NO_SUCH_GROUP;
2330 static int rpc_group_members(int argc, const char **argv)
2332 if (argc != 1) {
2333 return rpc_group_usage(argc, argv);
2336 return run_rpc_command(NULL, PI_SAMR, 0,
2337 rpc_group_members_internals,
2338 argc, argv);
2341 static NTSTATUS
2342 rpc_group_rename_internals(const DOM_SID *domain_sid,
2343 const char *domain_name,
2344 struct cli_state *cli,
2345 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2347 NTSTATUS result;
2348 POLICY_HND connect_pol, domain_pol, group_pol;
2349 uint32 num_rids, *rids, *rid_types;
2350 GROUP_INFO_CTR ctr;
2352 if (argc != 2) {
2353 d_printf("Usage: 'net rpc group rename group newname'\n");
2354 return NT_STATUS_UNSUCCESSFUL;
2357 /* Get sam policy handle */
2359 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2360 &connect_pol);
2362 if (!NT_STATUS_IS_OK(result))
2363 return result;
2365 /* Get domain policy handle */
2367 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2368 MAXIMUM_ALLOWED_ACCESS,
2369 domain_sid, &domain_pol);
2371 if (!NT_STATUS_IS_OK(result))
2372 return result;
2374 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2375 1, argv, &num_rids, &rids, &rid_types);
2377 if (num_rids != 1) {
2378 d_printf("Couldn't find group %s\n", argv[0]);
2379 return result;
2382 if (rid_types[0] != SID_NAME_DOM_GRP) {
2383 d_printf("Can only rename domain groups\n");
2384 return NT_STATUS_UNSUCCESSFUL;
2387 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
2388 MAXIMUM_ALLOWED_ACCESS,
2389 rids[0], &group_pol);
2391 if (!NT_STATUS_IS_OK(result))
2392 return result;
2394 ZERO_STRUCT(ctr);
2396 ctr.switch_value1 = 2;
2397 init_samr_group_info2(&ctr.group.info2, argv[1]);
2399 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &ctr);
2401 if (!NT_STATUS_IS_OK(result))
2402 return result;
2404 return NT_STATUS_NO_SUCH_GROUP;
2407 static int rpc_group_rename(int argc, const char **argv)
2409 if (argc != 2) {
2410 return rpc_group_usage(argc, argv);
2413 return run_rpc_command(NULL, PI_SAMR, 0,
2414 rpc_group_rename_internals,
2415 argc, argv);
2418 /**
2419 * 'net rpc group' entrypoint.
2420 * @param argc Standard main() style argc
2421 * @param argc Standard main() style argv. Initial components are already
2422 * stripped
2425 int net_rpc_group(int argc, const char **argv)
2427 struct functable func[] = {
2428 {"add", rpc_group_add},
2429 {"delete", rpc_group_delete},
2430 {"addmem", rpc_group_addmem},
2431 {"delmem", rpc_group_delmem},
2432 {"list", rpc_group_list},
2433 {"members", rpc_group_members},
2434 {"rename", rpc_group_rename},
2435 {NULL, NULL}
2438 if (argc == 0) {
2439 if (opt_long_list_entries) {
2440 } else {
2442 return run_rpc_command(NULL, PI_SAMR, 0,
2443 rpc_group_list_internals,
2444 argc, argv);
2447 return net_run_function(argc, argv, func, rpc_group_usage);
2450 /****************************************************************************/
2452 static int rpc_share_usage(int argc, const char **argv)
2454 return net_help_share(argc, argv);
2457 /**
2458 * Add a share on a remote RPC server
2460 * All parameters are provided by the run_rpc_command function, except for
2461 * argc, argv which are passes through.
2463 * @param domain_sid The domain sid acquired from the remote server
2464 * @param cli A cli_state connected to the server.
2465 * @param mem_ctx Talloc context, destoyed on completion of the function.
2466 * @param argc Standard main() style argc
2467 * @param argv Standard main() style argv. Initial components are already
2468 * stripped
2470 * @return Normal NTSTATUS return.
2472 static NTSTATUS
2473 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
2474 struct cli_state *cli,
2475 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2477 WERROR result;
2478 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2479 char *path;
2480 uint32 type=0; /* only allow disk shares to be added */
2481 uint32 num_users=0, perms=0;
2482 char *password=NULL; /* don't allow a share password */
2483 uint32 level = 2;
2485 path = strchr(sharename, '=');
2486 if (!path)
2487 return NT_STATUS_UNSUCCESSFUL;
2488 *path++ = '\0';
2490 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
2491 opt_comment, perms, opt_maxusers,
2492 num_users, path, password,
2493 level, NULL);
2494 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2497 static int rpc_share_add(int argc, const char **argv)
2499 if ((argc < 1) || !strchr(argv[0], '=')) {
2500 DEBUG(1,("Sharename or path not specified on add\n"));
2501 return rpc_share_usage(argc, argv);
2503 return run_rpc_command(NULL, PI_SRVSVC, 0,
2504 rpc_share_add_internals,
2505 argc, argv);
2508 /**
2509 * Delete a share on a remote RPC server
2511 * All parameters are provided by the run_rpc_command function, except for
2512 * argc, argv which are passes through.
2514 * @param domain_sid The domain sid acquired from the remote server
2515 * @param cli A cli_state connected to the server.
2516 * @param mem_ctx Talloc context, destoyed on completion of the function.
2517 * @param argc Standard main() style argc
2518 * @param argv Standard main() style argv. Initial components are already
2519 * stripped
2521 * @return Normal NTSTATUS return.
2523 static NTSTATUS
2524 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
2525 struct cli_state *cli,
2526 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2528 WERROR result;
2530 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
2531 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2534 /**
2535 * Delete a share on a remote RPC server
2537 * @param domain_sid The domain sid acquired from the remote server
2538 * @param argc Standard main() style argc
2539 * @param argv Standard main() style argv. Initial components are already
2540 * stripped
2542 * @return A shell status integer (0 for success)
2544 static int rpc_share_delete(int argc, const char **argv)
2546 if (argc < 1) {
2547 DEBUG(1,("Sharename not specified on delete\n"));
2548 return rpc_share_usage(argc, argv);
2550 return run_rpc_command(NULL, PI_SRVSVC, 0,
2551 rpc_share_del_internals,
2552 argc, argv);
2556 * Formatted print of share info
2558 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2561 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
2563 fstring netname = "", remark = "";
2565 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
2566 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
2568 if (opt_long_list_entries) {
2569 d_printf("%-12s %-8.8s %-50s\n",
2570 netname, share_type[info1->info_1.type], remark);
2571 } else {
2572 d_printf("%s\n", netname);
2577 /**
2578 * List shares on a remote RPC server
2580 * All parameters are provided by the run_rpc_command function, except for
2581 * argc, argv which are passes through.
2583 * @param domain_sid The domain sid acquired from the remote server
2584 * @param cli A cli_state connected to the server.
2585 * @param mem_ctx Talloc context, destoyed on completion of the function.
2586 * @param argc Standard main() style argc
2587 * @param argv Standard main() style argv. Initial components are already
2588 * stripped
2590 * @return Normal NTSTATUS return.
2593 static NTSTATUS
2594 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
2595 struct cli_state *cli,
2596 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2598 SRV_SHARE_INFO_CTR ctr;
2599 WERROR result;
2600 ENUM_HND hnd;
2601 uint32 preferred_len = 0xffffffff, i;
2603 init_enum_hnd(&hnd, 0);
2605 result = cli_srvsvc_net_share_enum(
2606 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
2608 if (!W_ERROR_IS_OK(result))
2609 goto done;
2611 /* Display results */
2613 if (opt_long_list_entries) {
2614 d_printf(
2615 "\nEnumerating shared resources (exports) on remote server:\n\n"\
2616 "\nShare name Type Description\n"\
2617 "---------- ---- -----------\n");
2619 for (i = 0; i < ctr.num_entries; i++)
2620 display_share_info_1(&ctr.share.info1[i]);
2621 done:
2622 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2625 /**
2626 * Migrate shares from a remote RPC server to the local RPC srever
2628 * All parameters are provided by the run_rpc_command function, except for
2629 * argc, argv which are passes through.
2631 * @param domain_sid The domain sid acquired from the remote server
2632 * @param cli A cli_state connected to the server.
2633 * @param mem_ctx Talloc context, destoyed on completion of the function.
2634 * @param argc Standard main() style argc
2635 * @param argv Standard main() style argv. Initial components are already
2636 * stripped
2638 * @return Normal NTSTATUS return.
2640 static NTSTATUS
2641 rpc_share_migrate_shares_internals(const DOM_SID *domain_sid, const char *domain_name,
2642 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2643 int argc, const char **argv)
2645 WERROR result;
2646 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2647 SRV_SHARE_INFO_CTR ctr_src;
2648 ENUM_HND hnd;
2649 uint32 type = 0; /* only allow disk shares to be added */
2650 uint32 num_uses = 0, perms = 0, max_uses = 0;
2651 char *password = NULL; /* don't allow a share password */
2652 uint32 preferred_len = 0xffffffff, i;
2653 BOOL got_dst_srvsvc_pipe = False;
2654 struct cli_state *cli_dst = NULL;
2655 uint32 level = 502; /* includes secdesc */
2656 SEC_DESC *share_sd = NULL;
2658 init_enum_hnd(&hnd, 0);
2660 result = cli_srvsvc_net_share_enum(
2661 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2662 if (!W_ERROR_IS_OK(result))
2663 goto done;
2665 /* connect local PI_SRVSVC */
2666 nt_status = connect_pipe(&cli_dst, PI_SRVSVC, &got_dst_srvsvc_pipe);
2667 if (!NT_STATUS_IS_OK(nt_status))
2668 return nt_status;
2671 for (i = 0; i < ctr_src.num_entries; i++) {
2673 fstring netname = "", remark = "", path = "";
2674 /* reset error-code */
2675 nt_status = NT_STATUS_UNSUCCESSFUL;
2677 rpcstr_pull_unistr2_fstring(
2678 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
2679 rpcstr_pull_unistr2_fstring(
2680 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
2681 rpcstr_pull_unistr2_fstring(
2682 path, &ctr_src.share.info502[i].info_502_str.uni_path);
2683 num_uses = ctr_src.share.info502[i].info_502.num_uses;
2684 max_uses = ctr_src.share.info502[i].info_502.max_uses;
2685 perms = ctr_src.share.info502[i].info_502.perms;
2688 if (opt_acls)
2689 share_sd = dup_sec_desc(
2690 mem_ctx, ctr_src.share.info502[i].info_502_str.sd);
2692 /* since we do not have NetShareGetInfo implemented in samba3 we
2693 only can skip inside the enum-ctr_src */
2694 if (argc == 1) {
2695 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2696 if (!strequal(netname, one_share))
2697 continue;
2700 /* skip builtin shares */
2701 /* FIXME: should print$ be added too ? */
2702 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
2703 strequal(netname,"global"))
2704 continue;
2706 if (opt_exclude && in_list(netname, opt_exclude, False)) {
2707 printf("excluding [%s]\n", netname);
2708 continue;
2711 /* only work with file-shares */
2712 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2713 d_printf("skipping [%s]: not a file share.\n", netname);
2714 continue;
2717 if (!cli_tdis(cli))
2718 goto done;
2721 /* finallly add the share on the dst server
2722 please note that samba currently does not allow to
2723 add a share without existing directory */
2725 printf("migrating: [%s], path: %s, comment: %s, %s share-ACLs\n",
2726 netname, path, remark, opt_acls ? "including" : "without" );
2728 if (opt_verbose && opt_acls)
2729 display_sec_desc(share_sd);
2731 result = cli_srvsvc_net_share_add(cli_dst, mem_ctx, netname, type,
2732 remark, perms, max_uses,
2733 num_uses, path, password,
2734 level, share_sd);
2736 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
2737 printf(" [%s] does already exist\n", netname);
2738 continue;
2741 if (!W_ERROR_IS_OK(result)) {
2742 printf("cannot add share: %s\n", dos_errstr(result));
2743 goto done;
2748 nt_status = NT_STATUS_OK;
2750 done:
2751 if (got_dst_srvsvc_pipe) {
2752 cli_nt_session_close(cli_dst);
2753 cli_shutdown(cli_dst);
2756 return nt_status;
2760 /**
2761 * Migrate shares from a rpc-server to another
2763 * @param argc Standard main() style argc
2764 * @param argv Standard main() style argv. Initial components are already
2765 * stripped
2767 * @return A shell status integer (0 for success)
2769 static int rpc_share_migrate_shares(int argc, const char **argv)
2772 if (!opt_host) {
2773 printf("no server to migrate\n");
2774 return -1;
2777 return run_rpc_command(NULL, PI_SRVSVC, 0,
2778 rpc_share_migrate_shares_internals,
2779 argc, argv);
2782 typedef struct copy_clistate {
2783 TALLOC_CTX *mem_ctx;
2784 struct cli_state *cli_share_src;
2785 struct cli_state *cli_share_dst;
2786 const char *cwd;
2787 } copy_clistate;
2791 * Copy a file/dir
2793 * @param f file_info
2794 * @param mask current search mask
2795 * @param state arg-pointer
2798 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
2800 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2801 struct copy_clistate *local_state = (struct copy_clistate *)state;
2802 fstring filename, new_mask, dir;
2804 if (strequal(f->name, ".") || strequal(f->name, ".."))
2805 return;
2807 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
2809 /* DIRECTORY */
2810 if (f->mode & aDIR) {
2812 DEBUG(3,("got dir: %s\n", f->name));
2814 fstrcpy(dir, local_state->cwd);
2815 fstrcat(dir, "\\");
2816 fstrcat(dir, f->name);
2818 /* create that directory */
2819 nt_status = net_copy_file(local_state->mem_ctx,
2820 local_state->cli_share_src,
2821 local_state->cli_share_dst,
2822 dir, dir,
2823 opt_acls? True : False,
2824 opt_attrs? True : False,
2825 opt_timestamps? True : False,
2826 False);
2828 if (!NT_STATUS_IS_OK(nt_status))
2829 printf("could not copy dir %s: %s\n",
2830 dir, nt_errstr(nt_status));
2832 /* search below that directory */
2833 fstrcpy(new_mask, dir);
2834 fstrcat(new_mask, "\\*");
2836 if (!sync_files(local_state->mem_ctx,
2837 local_state->cli_share_src,
2838 local_state->cli_share_dst,
2839 new_mask, dir))
2841 printf("could not sync files\n");
2843 return;
2847 /* FILE */
2848 fstrcpy(filename, local_state->cwd);
2849 fstrcat(filename, "\\");
2850 fstrcat(filename, f->name);
2852 DEBUG(3,("got file: %s\n", filename));
2854 nt_status = net_copy_file(local_state->mem_ctx,
2855 local_state->cli_share_src,
2856 local_state->cli_share_dst,
2857 filename, filename,
2858 opt_acls? True : False,
2859 opt_attrs? True : False,
2860 opt_timestamps? True: False,
2861 True);
2863 if (!NT_STATUS_IS_OK(nt_status))
2864 printf("could not copy file %s: %s\n",
2865 filename, nt_errstr(nt_status));
2870 * sync files, can be called recursivly to list files
2871 * and then call copy_fn for each file
2873 * @param mem_ctx TALLOC_CTX
2874 * @param cli_share_src a connected share on the originating server
2875 * @param cli_share_dst a connected share on the destination server
2876 * @param mask the current search mask
2877 * @param cwd the current path
2879 * @return Boolean result
2881 BOOL sync_files(TALLOC_CTX *mem_ctx,
2882 struct cli_state *cli_share_src,
2883 struct cli_state *cli_share_dst,
2884 pstring mask, fstring cwd)
2888 uint16 attribute = aSYSTEM | aHIDDEN | aDIR;
2889 struct copy_clistate clistate;
2891 clistate.mem_ctx = mem_ctx;
2892 clistate.cli_share_src = cli_share_src;
2893 clistate.cli_share_dst = cli_share_dst;
2894 clistate.cwd = cwd;
2896 DEBUG(3,("calling cli_list with mask: %s\n", mask));
2898 if (cli_list(cli_share_src, mask, attribute, copy_fn, &clistate) == -1) {
2899 d_printf("listing %s failed with error: %s\n",
2900 mask, cli_errstr(cli_share_src));
2901 return False;
2904 return True;
2908 /**
2909 * Sync all files inside a remote share to another share (over smb)
2911 * All parameters are provided by the run_rpc_command function, except for
2912 * argc, argv which are passes through.
2914 * @param domain_sid The domain sid acquired from the remote server
2915 * @param cli A cli_state connected to the server.
2916 * @param mem_ctx Talloc context, destoyed on completion of the function.
2917 * @param argc Standard main() style argc
2918 * @param argv Standard main() style argv. Initial components are already
2919 * stripped
2921 * @return Normal NTSTATUS return.
2923 static NTSTATUS
2924 rpc_share_migrate_files_internals(const DOM_SID *domain_sid, const char *domain_name,
2925 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2926 int argc, const char **argv)
2928 WERROR result;
2929 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2930 SRV_SHARE_INFO_CTR ctr_src;
2931 ENUM_HND hnd;
2932 uint32 preferred_len = 0xffffffff, i;
2933 uint32 level = 2;
2934 struct cli_state *cli_share_src = NULL;
2935 struct cli_state *cli_share_dst = NULL;
2936 BOOL got_src_share = False;
2937 BOOL got_dst_share = False;
2938 pstring mask;
2939 char *dst = NULL;
2941 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
2943 init_enum_hnd(&hnd, 0);
2945 result = cli_srvsvc_net_share_enum(
2946 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2948 if (!W_ERROR_IS_OK(result))
2949 goto done;
2951 for (i = 0; i < ctr_src.num_entries; i++) {
2953 fstring netname = "", remark = "", path = "";
2955 rpcstr_pull_unistr2_fstring(
2956 netname, &ctr_src.share.info2[i].info_2_str.uni_netname);
2957 rpcstr_pull_unistr2_fstring(
2958 remark, &ctr_src.share.info2[i].info_2_str.uni_remark);
2959 rpcstr_pull_unistr2_fstring(
2960 path, &ctr_src.share.info2[i].info_2_str.uni_path);
2962 /* since we do not have NetShareGetInfo implemented in samba3 we
2963 only can skip inside the enum-ctr_src */
2964 if (argc == 1) {
2965 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2966 if (!strequal(netname, one_share))
2967 continue;
2970 /* skip builtin and hidden shares
2971 In particular, one might not want to mirror whole discs :) */
2972 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$"))
2973 continue;
2975 if (strequal(netname, "print$") || netname[1] == '$') {
2976 d_printf("skipping [%s]: builtin/hidden share\n", netname);
2977 continue;
2980 if (opt_exclude && in_list(netname, opt_exclude, False)) {
2981 printf("excluding [%s]\n", netname);
2982 continue;
2985 /* only work with file-shares */
2986 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2987 d_printf("skipping [%s]: not a file share.\n", netname);
2988 continue;
2991 if (!cli_tdis(cli))
2992 return NT_STATUS_UNSUCCESSFUL;
2994 printf("syncing [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
2995 netname,
2996 opt_acls ? "including" : "without",
2997 opt_attrs ? "including" : "without",
2998 opt_timestamps ? "(preserving timestamps)" : "");
3001 /* open share source */
3002 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
3003 cli->desthost, netname, "A:");
3004 if (!NT_STATUS_IS_OK(nt_status))
3005 goto done;
3007 got_src_share = True;
3010 /* open share destination */
3011 nt_status = connect_to_service(&cli_share_dst, NULL,
3012 dst, netname, "A:");
3013 if (!NT_STATUS_IS_OK(nt_status))
3014 goto done;
3016 got_dst_share = True;
3019 /* now call the filesync */
3020 pstrcpy(mask, "\\*");
3022 if (!sync_files(mem_ctx, cli_share_src, cli_share_dst, mask, NULL)) {
3023 d_printf("could not sync files for share: %s\n", netname);
3024 nt_status = NT_STATUS_UNSUCCESSFUL;
3025 goto done;
3030 nt_status = NT_STATUS_OK;
3032 done:
3034 if (got_src_share)
3035 cli_shutdown(cli_share_src);
3037 if (got_dst_share)
3038 cli_shutdown(cli_share_dst);
3040 return nt_status;
3044 static int rpc_share_migrate_files(int argc, const char **argv)
3047 if (!opt_host) {
3048 printf("no server to migrate\n");
3049 return -1;
3052 return run_rpc_command(NULL, PI_SRVSVC, 0,
3053 rpc_share_migrate_files_internals,
3054 argc, argv);
3057 /**
3058 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3059 * from one server to another
3061 * @param argc Standard main() style argc
3062 * @param argv Standard main() style argv. Initial components are already
3063 * stripped
3065 * @return A shell status integer (0 for success)
3068 static int rpc_share_migrate_all(int argc, const char **argv)
3070 int ret;
3072 if (!opt_host) {
3073 printf("no server to migrate\n");
3074 return -1;
3077 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3078 if (ret)
3079 return ret;
3080 #if 0
3081 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_security_internals, argc, argv);
3082 if (ret)
3083 return ret;
3084 #endif
3085 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3089 /**
3090 * 'net rpc share migrate' entrypoint.
3091 * @param argc Standard main() style argc
3092 * @param argv Standard main() style argv. Initial components are already
3093 * stripped
3095 static int rpc_share_migrate(int argc, const char **argv)
3098 struct functable func[] = {
3099 {"all", rpc_share_migrate_all},
3100 {"files", rpc_share_migrate_files},
3101 {"help", rpc_share_usage},
3102 /* {"security", rpc_share_migrate_security},*/
3103 {"shares", rpc_share_migrate_shares},
3104 {NULL, NULL}
3107 return net_run_function(argc, argv, func, rpc_share_usage);
3110 struct full_alias {
3111 DOM_SID sid;
3112 int num_members;
3113 DOM_SID *members;
3116 static int num_server_aliases;
3117 static struct full_alias *server_aliases;
3120 * Add an alias to the static list.
3122 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3124 if (server_aliases == NULL)
3125 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3127 server_aliases[num_server_aliases] = *alias;
3128 num_server_aliases += 1;
3132 * For a specific domain on the server, fetch all the aliases
3133 * and their members. Add all of them to the server_aliases.
3135 static NTSTATUS
3136 rpc_fetch_domain_aliases(struct cli_state *cli, TALLOC_CTX *mem_ctx,
3137 POLICY_HND *connect_pol,
3138 const DOM_SID *domain_sid)
3140 uint32 start_idx, max_entries, num_entries, i;
3141 struct acct_info *groups;
3142 NTSTATUS result;
3143 POLICY_HND domain_pol;
3145 /* Get domain policy handle */
3147 result = cli_samr_open_domain(cli, mem_ctx, connect_pol,
3148 MAXIMUM_ALLOWED_ACCESS,
3149 domain_sid, &domain_pol);
3150 if (!NT_STATUS_IS_OK(result))
3151 return result;
3153 start_idx = 0;
3154 max_entries = 250;
3156 do {
3157 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
3158 &start_idx, max_entries,
3159 &groups, &num_entries);
3161 for (i = 0; i < num_entries; i++) {
3163 POLICY_HND alias_pol;
3164 struct full_alias alias;
3165 DOM_SID *members;
3166 int j;
3168 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
3169 MAXIMUM_ALLOWED_ACCESS,
3170 groups[i].rid,
3171 &alias_pol);
3172 if (!NT_STATUS_IS_OK(result))
3173 goto done;
3175 result = cli_samr_query_aliasmem(cli, mem_ctx,
3176 &alias_pol,
3177 &alias.num_members,
3178 &members);
3179 if (!NT_STATUS_IS_OK(result))
3180 goto done;
3182 result = cli_samr_close(cli, mem_ctx, &alias_pol);
3183 if (!NT_STATUS_IS_OK(result))
3184 goto done;
3186 alias.members = NULL;
3188 if (alias.num_members > 0) {
3189 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3191 for (j = 0; j < alias.num_members; j++)
3192 sid_copy(&alias.members[j],
3193 &members[j]);
3196 sid_copy(&alias.sid, domain_sid);
3197 sid_append_rid(&alias.sid, groups[i].rid);
3199 push_alias(mem_ctx, &alias);
3201 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3203 result = NT_STATUS_OK;
3205 done:
3206 cli_samr_close(cli, mem_ctx, &domain_pol);
3208 return result;
3212 * Dump server_aliases as names for debugging purposes.
3214 static NTSTATUS
3215 rpc_aliaslist_dump(const DOM_SID *domain_sid, const char *domain_name,
3216 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3217 int argc, const char **argv)
3219 int i;
3220 NTSTATUS result;
3221 POLICY_HND lsa_pol;
3223 result = cli_lsa_open_policy(cli, mem_ctx, True,
3224 SEC_RIGHTS_MAXIMUM_ALLOWED,
3225 &lsa_pol);
3226 if (!NT_STATUS_IS_OK(result))
3227 return result;
3229 for (i=0; i<num_server_aliases; i++) {
3230 char **names;
3231 char **domains;
3232 uint32 *types;
3233 int j;
3235 struct full_alias *alias = &server_aliases[i];
3237 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, 1,
3238 &alias->sid,
3239 &domains, &names, &types);
3240 if (!NT_STATUS_IS_OK(result))
3241 continue;
3243 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3245 if (alias->num_members == 0) {
3246 DEBUG(1, ("\n"));
3247 continue;
3250 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol,
3251 alias->num_members,
3252 alias->members,
3253 &domains, &names, &types);
3255 if (!NT_STATUS_IS_OK(result) &&
3256 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3257 continue;
3259 for (j=0; j<alias->num_members; j++)
3260 DEBUG(1, ("%s\\%s (%d); ",
3261 domains[j] ? domains[j] : "*unknown*",
3262 names[j] ? names[j] : "*unknown*",types[j]));
3263 DEBUG(1, ("\n"));
3266 cli_lsa_close(cli, mem_ctx, &lsa_pol);
3268 return NT_STATUS_OK;
3272 * Fetch a list of all server aliases and their members into
3273 * server_aliases.
3275 static NTSTATUS
3276 rpc_aliaslist_internals(const DOM_SID *domain_sid, const char *domain_name,
3277 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3278 int argc, const char **argv)
3280 NTSTATUS result;
3281 POLICY_HND connect_pol;
3283 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
3284 &connect_pol);
3286 if (!NT_STATUS_IS_OK(result))
3287 goto done;
3289 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3290 &global_sid_Builtin);
3292 if (!NT_STATUS_IS_OK(result))
3293 goto done;
3295 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3296 domain_sid);
3298 cli_samr_close(cli, mem_ctx, &connect_pol);
3299 done:
3300 return result;
3303 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3305 token->num_sids = 4;
3307 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
3309 token->user_sids[0] = *user_sid;
3310 sid_copy(&token->user_sids[1], &global_sid_World);
3311 sid_copy(&token->user_sids[2], &global_sid_Network);
3312 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3315 static void free_user_token(NT_USER_TOKEN *token)
3317 SAFE_FREE(token->user_sids);
3320 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
3322 int i;
3324 for (i=0; i<token->num_sids; i++) {
3325 if (sid_compare(sid, &token->user_sids[i]) == 0)
3326 return True;
3328 return False;
3331 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3333 if (is_sid_in_token(token, sid))
3334 return;
3336 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3338 sid_copy(&token->user_sids[token->num_sids], sid);
3340 token->num_sids += 1;
3343 struct user_token {
3344 fstring name;
3345 NT_USER_TOKEN token;
3348 static void dump_user_token(struct user_token *token)
3350 int i;
3352 d_printf("%s\n", token->name);
3354 for (i=0; i<token->token.num_sids; i++) {
3355 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
3359 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
3361 int i;
3363 for (i=0; i<alias->num_members; i++) {
3364 if (sid_compare(sid, &alias->members[i]) == 0)
3365 return True;
3368 return False;
3371 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
3373 int i;
3375 for (i=0; i<num_server_aliases; i++) {
3376 if (is_alias_member(&sid, &server_aliases[i]))
3377 add_sid_to_token(token, &server_aliases[i].sid);
3382 * We got a user token with all the SIDs we can know about without asking the
3383 * server directly. These are the user and domain group sids. All of these can
3384 * be members of aliases. So scan the list of aliases for each of the SIDs and
3385 * add them to the token.
3388 static void collect_alias_memberships(NT_USER_TOKEN *token)
3390 int num_global_sids = token->num_sids;
3391 int i;
3393 for (i=0; i<num_global_sids; i++) {
3394 collect_sid_memberships(token, token->user_sids[i]);
3398 static BOOL get_user_sids(const char *domain, const char *user,
3399 NT_USER_TOKEN *token)
3401 struct winbindd_request request;
3402 struct winbindd_response response;
3403 fstring full_name;
3404 NSS_STATUS result;
3406 DOM_SID user_sid;
3408 int i;
3410 fstr_sprintf(full_name, "%s%c%s",
3411 domain, *lp_winbind_separator(), user);
3413 /* First let's find out the user sid */
3415 ZERO_STRUCT(request);
3416 ZERO_STRUCT(response);
3418 fstrcpy(request.data.name.dom_name, domain);
3419 fstrcpy(request.data.name.name, user);
3421 result = winbindd_request(WINBINDD_LOOKUPNAME, &request, &response);
3423 if (result != NSS_STATUS_SUCCESS) {
3424 DEBUG(1, ("winbind could not find %s\n", full_name));
3425 return False;
3428 if (response.data.sid.type != SID_NAME_USER) {
3429 DEBUG(1, ("%s is not a user\n", full_name));
3430 return False;
3433 string_to_sid(&user_sid, response.data.sid.sid);
3435 init_user_token(token, &user_sid);
3437 /* And now the groups winbind knows about */
3439 ZERO_STRUCT(response);
3441 fstrcpy(request.data.username, full_name);
3443 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
3445 if (result != NSS_STATUS_SUCCESS) {
3446 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
3447 return False;
3450 for (i = 0; i < response.data.num_entries; i++) {
3451 gid_t gid = ((gid_t *)response.extra_data)[i];
3452 DOM_SID sid;
3454 struct winbindd_request sidrequest;
3455 struct winbindd_response sidresponse;
3457 ZERO_STRUCT(sidrequest);
3458 ZERO_STRUCT(sidresponse);
3460 sidrequest.data.gid = gid;
3462 result = winbindd_request(WINBINDD_GID_TO_SID,
3463 &sidrequest, &sidresponse);
3465 if (result != NSS_STATUS_SUCCESS) {
3466 DEBUG(1, ("winbind could not find SID of gid %d\n",
3467 gid));
3468 return False;
3471 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
3473 string_to_sid(&sid, sidresponse.data.sid.sid);
3474 add_sid_to_token(token, &sid);
3477 SAFE_FREE(response.extra_data);
3479 return True;
3483 * Get a list of all user tokens we want to look at
3485 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
3487 struct winbindd_request request;
3488 struct winbindd_response response;
3489 const char *extra_data;
3490 fstring name;
3491 int i;
3492 struct user_token *result;
3494 if (lp_winbind_use_default_domain() &&
3495 (opt_target_workgroup == NULL)) {
3496 d_printf("winbind use default domain = yes set, please "
3497 "specify a workgroup\n");
3498 return False;
3501 /* Send request to winbind daemon */
3503 ZERO_STRUCT(request);
3504 ZERO_STRUCT(response);
3506 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
3507 NSS_STATUS_SUCCESS)
3508 return False;
3510 /* Look through extra data */
3512 if (!response.extra_data)
3513 return False;
3515 extra_data = (const char *)response.extra_data;
3516 *num_tokens = 0;
3518 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3519 *num_tokens += 1;
3522 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
3524 if (result == NULL) {
3525 DEBUG(1, ("Could not malloc sid array\n"));
3526 return False;
3529 extra_data = (const char *)response.extra_data;
3530 i=0;
3532 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3534 fstring domain, user;
3535 char *p;
3537 fstrcpy(result[i].name, name);
3539 p = strchr(name, *lp_winbind_separator());
3541 DEBUG(3, ("%s\n", name));
3543 if (p == NULL) {
3544 fstrcpy(domain, opt_target_workgroup);
3545 fstrcpy(user, name);
3546 } else {
3547 *p++ = '\0';
3548 fstrcpy(domain, name);
3549 strupper_m(domain);
3550 fstrcpy(user, p);
3553 get_user_sids(domain, user, &(result[i].token));
3554 i+=1;
3557 SAFE_FREE(response.extra_data);
3559 *user_tokens = result;
3561 return True;
3564 static BOOL get_user_tokens_from_file(FILE *f,
3565 int *num_tokens,
3566 struct user_token **tokens)
3568 struct user_token *token = NULL;
3570 while (!feof(f)) {
3571 fstring line;
3573 if (fgets(line, sizeof(line)-1, f) == NULL) {
3574 return True;
3577 if (line[strlen(line)-1] == '\n')
3578 line[strlen(line)-1] = '\0';
3580 if (line[0] == ' ') {
3581 /* We have a SID */
3583 DOM_SID sid;
3584 string_to_sid(&sid, &line[1]);
3586 if (token == NULL) {
3587 DEBUG(0, ("File does not begin with username"));
3588 return False;
3591 add_sid_to_token(&token->token, &sid);
3592 continue;
3595 /* And a new user... */
3597 *num_tokens += 1;
3598 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
3599 if (*tokens == NULL) {
3600 DEBUG(0, ("Could not realloc tokens\n"));
3601 return False;
3604 token = &((*tokens)[*num_tokens-1]);
3606 fstrcpy(token->name, line);
3607 token->token.num_sids = 0;
3608 token->token.user_sids = NULL;
3609 continue;
3612 return False;
3617 * Show the list of all users that have access to a share
3620 static void show_userlist(struct cli_state *cli,
3621 TALLOC_CTX *mem_ctx, const char *netname,
3622 int num_tokens, struct user_token *tokens)
3624 int fnum;
3625 SEC_DESC *share_sd = NULL;
3626 SEC_DESC *root_sd = NULL;
3627 int i;
3628 SRV_SHARE_INFO info;
3629 WERROR result;
3630 uint16 cnum;
3632 result = cli_srvsvc_net_share_get_info(cli, mem_ctx, netname,
3633 502, &info);
3635 if (!W_ERROR_IS_OK(result)) {
3636 DEBUG(1, ("Coult not query secdesc for share %s\n",
3637 netname));
3638 return;
3641 share_sd = info.share.info502.info_502_str.sd;
3642 if (share_sd == NULL) {
3643 DEBUG(1, ("Got no secdesc for share %s\n",
3644 netname));
3647 cnum = cli->cnum;
3649 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3650 return;
3653 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
3655 if (fnum != -1) {
3656 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
3659 for (i=0; i<num_tokens; i++) {
3660 uint32 acc_granted;
3661 NTSTATUS status;
3663 if (share_sd != NULL) {
3664 if (!se_access_check(share_sd, &tokens[i].token,
3665 1, &acc_granted, &status)) {
3666 DEBUG(1, ("Could not check share_sd for "
3667 "user %s\n",
3668 tokens[i].name));
3669 continue;
3672 if (!NT_STATUS_IS_OK(status))
3673 continue;
3676 if (root_sd == NULL) {
3677 d_printf(" %s\n", tokens[i].name);
3678 continue;
3681 if (!se_access_check(root_sd, &tokens[i].token,
3682 1, &acc_granted, &status)) {
3683 DEBUG(1, ("Could not check root_sd for user %s\n",
3684 tokens[i].name));
3685 continue;
3688 if (!NT_STATUS_IS_OK(status))
3689 continue;
3691 d_printf(" %s\n", tokens[i].name);
3694 if (fnum != -1)
3695 cli_close(cli, fnum);
3696 cli_tdis(cli);
3697 cli->cnum = cnum;
3699 return;
3702 struct share_list {
3703 int num_shares;
3704 char **shares;
3707 static void collect_share(const char *name, uint32 m,
3708 const char *comment, void *state)
3710 struct share_list *share_list = (struct share_list *)state;
3712 if (m != STYPE_DISKTREE)
3713 return;
3715 share_list->num_shares += 1;
3716 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
3717 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
3720 static void rpc_share_userlist_usage(void)
3722 return;
3725 /**
3726 * List shares on a remote RPC server, including the security descriptors
3728 * All parameters are provided by the run_rpc_command function, except for
3729 * argc, argv which are passes through.
3731 * @param domain_sid The domain sid acquired from the remote server
3732 * @param cli A cli_state connected to the server.
3733 * @param mem_ctx Talloc context, destoyed on completion of the function.
3734 * @param argc Standard main() style argc
3735 * @param argv Standard main() style argv. Initial components are already
3736 * stripped
3738 * @return Normal NTSTATUS return.
3741 static NTSTATUS
3742 rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
3743 const char *domain_name,
3744 struct cli_state *cli,
3745 TALLOC_CTX *mem_ctx,
3746 int argc, const char **argv)
3748 int ret;
3749 BOOL r;
3750 ENUM_HND hnd;
3751 uint32 i;
3752 FILE *f;
3754 struct user_token *tokens = NULL;
3755 int num_tokens = 0;
3757 struct share_list share_list;
3759 if (argc > 1) {
3760 rpc_share_userlist_usage();
3761 return NT_STATUS_UNSUCCESSFUL;
3764 if (argc == 0) {
3765 f = stdin;
3766 } else {
3767 f = fopen(argv[0], "r");
3770 if (f == NULL) {
3771 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
3772 return NT_STATUS_UNSUCCESSFUL;
3775 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
3777 if (f != stdin)
3778 fclose(f);
3780 if (!r) {
3781 DEBUG(0, ("Could not read users from file\n"));
3782 return NT_STATUS_UNSUCCESSFUL;
3785 for (i=0; i<num_tokens; i++)
3786 collect_alias_memberships(&tokens[i].token);
3788 init_enum_hnd(&hnd, 0);
3790 share_list.num_shares = 0;
3791 share_list.shares = NULL;
3793 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
3795 if (ret == -1) {
3796 DEBUG(0, ("Error returning browse list: %s\n",
3797 cli_errstr(cli)));
3798 goto done;
3801 for (i = 0; i < share_list.num_shares; i++) {
3802 char *netname = share_list.shares[i];
3804 if (netname[strlen(netname)-1] == '$')
3805 continue;
3807 d_printf("%s\n", netname);
3809 show_userlist(cli, mem_ctx, netname,
3810 num_tokens, tokens);
3812 done:
3813 for (i=0; i<num_tokens; i++) {
3814 free_user_token(&tokens[i].token);
3816 SAFE_FREE(tokens);
3817 SAFE_FREE(share_list.shares);
3819 return NT_STATUS_OK;
3822 static int
3823 rpc_share_allowedusers(int argc, const char **argv)
3825 int result;
3827 result = run_rpc_command(NULL, PI_SAMR, 0,
3828 rpc_aliaslist_internals,
3829 argc, argv);
3830 if (result != 0)
3831 return result;
3833 result = run_rpc_command(NULL, PI_LSARPC, 0,
3834 rpc_aliaslist_dump,
3835 argc, argv);
3836 if (result != 0)
3837 return result;
3839 return run_rpc_command(NULL, PI_SRVSVC, 0,
3840 rpc_share_allowedusers_internals,
3841 argc, argv);
3844 int net_usersidlist(int argc, const char **argv)
3846 int num_tokens = 0;
3847 struct user_token *tokens = NULL;
3848 int i;
3850 if (argc != 0) {
3851 net_usersidlist_usage(argc, argv);
3852 return 0;
3855 if (!get_user_tokens(&num_tokens, &tokens)) {
3856 DEBUG(0, ("Could not get the user/sid list\n"));
3857 return 0;
3860 for (i=0; i<num_tokens; i++) {
3861 dump_user_token(&tokens[i]);
3862 free_user_token(&tokens[i].token);
3865 SAFE_FREE(tokens);
3866 return 1;
3869 int net_usersidlist_usage(int argc, const char **argv)
3871 d_printf("net usersidlist\n"
3872 "\tprints out a list of all users the running winbind knows\n"
3873 "\tabout, together with all their SIDs. This is used as\n"
3874 "\tinput to the 'net rpc share allowedusers' command.\n\n");
3876 net_common_flags_usage(argc, argv);
3877 return -1;
3880 /**
3881 * 'net rpc share' entrypoint.
3882 * @param argc Standard main() style argc
3883 * @param argv Standard main() style argv. Initial components are already
3884 * stripped
3887 int net_rpc_share(int argc, const char **argv)
3889 struct functable func[] = {
3890 {"add", rpc_share_add},
3891 {"delete", rpc_share_delete},
3892 {"allowedusers", rpc_share_allowedusers},
3893 {"migrate", rpc_share_migrate},
3894 {NULL, NULL}
3897 if (argc == 0)
3898 return run_rpc_command(NULL, PI_SRVSVC, 0,
3899 rpc_share_list_internals,
3900 argc, argv);
3902 return net_run_function(argc, argv, func, rpc_share_usage);
3905 /****************************************************************************/
3907 static int rpc_file_usage(int argc, const char **argv)
3909 return net_help_file(argc, argv);
3912 /**
3913 * Close a file on a remote RPC server
3915 * All parameters are provided by the run_rpc_command function, except for
3916 * argc, argv which are passes through.
3918 * @param domain_sid The domain sid acquired from the remote server
3919 * @param cli A cli_state connected to the server.
3920 * @param mem_ctx Talloc context, destoyed on completion of the function.
3921 * @param argc Standard main() style argc
3922 * @param argv Standard main() style argv. Initial components are already
3923 * stripped
3925 * @return Normal NTSTATUS return.
3927 static NTSTATUS
3928 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
3929 struct cli_state *cli,
3930 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3932 WERROR result;
3933 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
3934 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3937 /**
3938 * Close a file on a remote RPC server
3940 * @param argc Standard main() style argc
3941 * @param argv Standard main() style argv. Initial components are already
3942 * stripped
3944 * @return A shell status integer (0 for success)
3946 static int rpc_file_close(int argc, const char **argv)
3948 if (argc < 1) {
3949 DEBUG(1, ("No fileid given on close\n"));
3950 return(rpc_file_usage(argc, argv));
3953 return run_rpc_command(NULL, PI_SRVSVC, 0,
3954 rpc_file_close_internals,
3955 argc, argv);
3958 /**
3959 * Formatted print of open file info
3961 * @param info3 FILE_INFO_3 contents
3962 * @param str3 strings for FILE_INFO_3
3965 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
3967 fstring user = "", path = "";
3969 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
3970 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
3972 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
3973 info3->id, user, info3->perms, info3->num_locks, path);
3976 /**
3977 * List open files on a remote RPC server
3979 * All parameters are provided by the run_rpc_command function, except for
3980 * argc, argv which are passes through.
3982 * @param domain_sid The domain sid acquired from the remote server
3983 * @param cli A cli_state connected to the server.
3984 * @param mem_ctx Talloc context, destoyed on completion of the function.
3985 * @param argc Standard main() style argc
3986 * @param argv Standard main() style argv. Initial components are already
3987 * stripped
3989 * @return Normal NTSTATUS return.
3992 static NTSTATUS
3993 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
3994 struct cli_state *cli,
3995 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3997 SRV_FILE_INFO_CTR ctr;
3998 WERROR result;
3999 ENUM_HND hnd;
4000 uint32 preferred_len = 0xffffffff, i;
4001 const char *username=NULL;
4003 init_enum_hnd(&hnd, 0);
4005 /* if argc > 0, must be user command */
4006 if (argc > 0)
4007 username = smb_xstrdup(argv[0]);
4009 result = cli_srvsvc_net_file_enum(
4010 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4012 if (!W_ERROR_IS_OK(result))
4013 goto done;
4015 /* Display results */
4017 d_printf(
4018 "\nEnumerating open files on remote server:\n\n"\
4019 "\nFileId Opened by Perms Locks Path"\
4020 "\n------ --------- ----- ----- ---- \n");
4021 for (i = 0; i < ctr.num_entries; i++)
4022 display_file_info_3(&ctr.file.info3[i].info_3,
4023 &ctr.file.info3[i].info_3_str);
4024 done:
4025 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4029 /**
4030 * List files for a user on a remote RPC server
4032 * @param argc Standard main() style argc
4033 * @param argv Standard main() style argv. Initial components are already
4034 * stripped
4036 * @return A shell status integer (0 for success)
4038 static int rpc_file_user(int argc, const char **argv)
4040 if (argc < 1) {
4041 DEBUG(1, ("No username given\n"));
4042 return(rpc_file_usage(argc, argv));
4045 return run_rpc_command(NULL, PI_SRVSVC, 0,
4046 rpc_file_list_internals,
4047 argc, argv);
4051 /**
4052 * 'net rpc file' entrypoint.
4053 * @param argc Standard main() style argc
4054 * @param argv Standard main() style argv. Initial components are already
4055 * stripped
4058 int net_rpc_file(int argc, const char **argv)
4060 struct functable func[] = {
4061 {"close", rpc_file_close},
4062 {"user", rpc_file_user},
4063 #if 0
4064 {"info", rpc_file_info},
4065 #endif
4066 {NULL, NULL}
4069 if (argc == 0)
4070 return run_rpc_command(NULL, PI_SRVSVC, 0,
4071 rpc_file_list_internals,
4072 argc, argv);
4074 return net_run_function(argc, argv, func, rpc_file_usage);
4077 /****************************************************************************/
4081 /**
4082 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4084 * All parameters are provided by the run_rpc_command function, except for
4085 * argc, argv which are passed through.
4087 * @param domain_sid The domain sid aquired from the remote server
4088 * @param cli A cli_state connected to the server.
4089 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4090 * @param argc Standard main() style argc
4091 * @param argv Standard main() style argv. Initial components are already
4092 * stripped
4094 * @return Normal NTSTATUS return.
4097 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4098 const char *domain_name,
4099 struct cli_state *cli,
4100 TALLOC_CTX *mem_ctx,
4101 int argc, const char **argv)
4103 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4105 result = cli_shutdown_abort(cli, mem_ctx);
4107 if (NT_STATUS_IS_OK(result)) {
4108 d_printf("\nShutdown successfully aborted\n");
4109 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4110 } else
4111 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4113 return result;
4117 /**
4118 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4120 * All parameters are provided by the run_rpc_command function, except for
4121 * argc, argv which are passed through.
4123 * @param domain_sid The domain sid aquired from the remote server
4124 * @param cli A cli_state connected to the server.
4125 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4126 * @param argc Standard main() style argc
4127 * @param argv Standard main() style argv. Initial components are already
4128 * stripped
4130 * @return Normal NTSTATUS return.
4133 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
4134 const char *domain_name,
4135 struct cli_state *cli,
4136 TALLOC_CTX *mem_ctx,
4137 int argc, const char **argv)
4139 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4141 result = werror_to_ntstatus(cli_reg_abort_shutdown(cli, mem_ctx));
4143 if (NT_STATUS_IS_OK(result)) {
4144 d_printf("\nShutdown successfully aborted\n");
4145 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4146 } else
4147 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4149 return result;
4152 /**
4153 * ABORT the Shut down of a remote RPC server
4155 * @param argc Standard main() style argc
4156 * @param argv Standard main() style argv. Initial components are already
4157 * stripped
4159 * @return A shell status integer (0 for success)
4162 static int rpc_shutdown_abort(int argc, const char **argv)
4164 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4165 rpc_shutdown_abort_internals,
4166 argc, argv);
4168 if (rc == 0)
4169 return rc;
4171 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4173 return run_rpc_command(NULL, PI_WINREG, 0,
4174 rpc_reg_shutdown_abort_internals,
4175 argc, argv);
4178 /**
4179 * Shut down a remote RPC Server via initshutdown pipe
4181 * All parameters are provided by the run_rpc_command function, except for
4182 * argc, argv which are passes through.
4184 * @param domain_sid The domain sid aquired from the remote server
4185 * @param cli A cli_state connected to the server.
4186 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4187 * @param argc Standard main() style argc
4188 * @param argc Standard main() style argv. Initial components are already
4189 * stripped
4191 * @return Normal NTSTATUS return.
4194 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
4195 const char *domain_name,
4196 struct cli_state *cli,
4197 TALLOC_CTX *mem_ctx,
4198 int argc, const char **argv)
4200 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4201 const char *msg = "This machine will be shutdown shortly";
4202 uint32 timeout = 20;
4204 if (opt_comment) {
4205 msg = opt_comment;
4207 if (opt_timeout) {
4208 timeout = opt_timeout;
4211 /* create an entry */
4212 result = cli_shutdown_init(cli, mem_ctx, msg, timeout, opt_reboot,
4213 opt_force);
4215 if (NT_STATUS_IS_OK(result)) {
4216 d_printf("\nShutdown of remote machine succeeded\n");
4217 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4218 } else
4219 DEBUG(0,("Shutdown of remote machine failed!\n"));
4221 return result;
4224 /**
4225 * Shut down a remote RPC Server via winreg pipe
4227 * All parameters are provided by the run_rpc_command function, except for
4228 * argc, argv which are passes through.
4230 * @param domain_sid The domain sid aquired from the remote server
4231 * @param cli A cli_state connected to the server.
4232 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4233 * @param argc Standard main() style argc
4234 * @param argc Standard main() style argv. Initial components are already
4235 * stripped
4237 * @return Normal NTSTATUS return.
4240 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
4241 const char *domain_name,
4242 struct cli_state *cli,
4243 TALLOC_CTX *mem_ctx,
4244 int argc, const char **argv)
4246 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4247 const char *msg = "This machine will be shutdown shortly";
4248 uint32 timeout = 20;
4249 #if 0
4250 poptContext pc;
4251 int rc;
4253 struct poptOption long_options[] = {
4254 {"message", 'm', POPT_ARG_STRING, &msg},
4255 {"timeout", 't', POPT_ARG_INT, &timeout},
4256 {"reboot", 'r', POPT_ARG_NONE, &reboot},
4257 {"force", 'f', POPT_ARG_NONE, &force},
4258 { 0, 0, 0, 0}
4261 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
4262 POPT_CONTEXT_KEEP_FIRST);
4264 rc = poptGetNextOpt(pc);
4266 if (rc < -1) {
4267 /* an error occurred during option processing */
4268 DEBUG(0, ("%s: %s\n",
4269 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
4270 poptStrerror(rc)));
4271 return NT_STATUS_INVALID_PARAMETER;
4273 #endif
4274 if (opt_comment) {
4275 msg = opt_comment;
4277 if (opt_timeout) {
4278 timeout = opt_timeout;
4281 /* create an entry */
4282 result = werror_to_ntstatus(cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force));
4284 if (NT_STATUS_IS_OK(result)) {
4285 d_printf("\nShutdown of remote machine succeeded\n");
4286 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4288 else
4289 DEBUG(0,("Shutdown of remote machine failed!\n"));
4291 return result;
4294 /**
4295 * Shut down a remote RPC server
4297 * @param argc Standard main() style argc
4298 * @param argc Standard main() style argv. Initial components are already
4299 * stripped
4301 * @return A shell status integer (0 for success)
4304 static int rpc_shutdown(int argc, const char **argv)
4306 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4307 rpc_init_shutdown_internals,
4308 argc, argv);
4309 if (rc == 0)
4310 return rc;
4312 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4314 return run_rpc_command(NULL, PI_WINREG, 0, rpc_reg_shutdown_internals,
4315 argc, argv);
4318 /***************************************************************************
4319 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
4321 ***************************************************************************/
4324 * Add interdomain trust account to the RPC server.
4325 * All parameters (except for argc and argv) are passed by run_rpc_command
4326 * function.
4328 * @param domain_sid The domain sid acquired from the server
4329 * @param cli A cli_state connected to the server.
4330 * @param mem_ctx Talloc context, destoyed on completion of the function.
4331 * @param argc Standard main() style argc
4332 * @param argc Standard main() style argv. Initial components are already
4333 * stripped
4335 * @return normal NTSTATUS return code
4338 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
4339 const char *domain_name,
4340 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4341 int argc, const char **argv) {
4343 POLICY_HND connect_pol, domain_pol, user_pol;
4344 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4345 char *acct_name;
4346 uint16 acb_info;
4347 uint32 unknown, user_rid;
4349 if (argc != 2) {
4350 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
4351 return NT_STATUS_INVALID_PARAMETER;
4355 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4358 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
4359 return NT_STATUS_NO_MEMORY;
4362 strupper_m(acct_name);
4364 /* Get samr policy handle */
4365 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4366 &connect_pol);
4367 if (!NT_STATUS_IS_OK(result)) {
4368 goto done;
4371 /* Get domain policy handle */
4372 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4373 MAXIMUM_ALLOWED_ACCESS,
4374 domain_sid, &domain_pol);
4375 if (!NT_STATUS_IS_OK(result)) {
4376 goto done;
4379 /* Create trusting domain's account */
4380 acb_info = ACB_NORMAL;
4381 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
4382 mimir: yes, most probably it is */
4384 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
4385 acct_name, acb_info, unknown,
4386 &user_pol, &user_rid);
4387 if (!NT_STATUS_IS_OK(result)) {
4388 goto done;
4392 SAM_USERINFO_CTR ctr;
4393 SAM_USER_INFO_23 p23;
4394 NTTIME notime;
4395 char nostr[] = "";
4396 LOGON_HRS hrs;
4397 uchar pwbuf[516];
4399 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
4401 ZERO_STRUCT(ctr);
4402 ZERO_STRUCT(p23);
4403 ZERO_STRUCT(notime);
4404 hrs.max_len = 1260;
4405 hrs.offset = 0;
4406 hrs.len = 21;
4407 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
4408 acb_info = ACB_DOMTRUST;
4410 init_sam_user_info23A(&p23, &notime, &notime, &notime,
4411 &notime, &notime, &notime,
4412 nostr, nostr, nostr, nostr, nostr,
4413 nostr, nostr, nostr, nostr, nostr,
4414 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
4415 0, 0, (char *)pwbuf);
4416 ctr.switch_value = 23;
4417 ctr.info.id23 = &p23;
4418 p23.passmustchange = 0;
4420 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 23,
4421 &cli->user_session_key, &ctr);
4423 if (!NT_STATUS_IS_OK(result)) {
4424 DEBUG(0,("Could not set trust account password: %s\n",
4425 nt_errstr(result)));
4426 goto done;
4430 done:
4431 SAFE_FREE(acct_name);
4432 return result;
4436 * Create interdomain trust account for a remote domain.
4438 * @param argc standard argc
4439 * @param argv standard argv without initial components
4441 * @return Integer status (0 means success)
4444 static int rpc_trustdom_add(int argc, const char **argv)
4446 if (argc > 0) {
4447 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
4448 argc, argv);
4449 } else {
4450 d_printf("Usage: net rpc trustdom add <domain>\n");
4451 return -1;
4457 * Remove interdomain trust account from the RPC server.
4458 * All parameters (except for argc and argv) are passed by run_rpc_command
4459 * function.
4461 * @param domain_sid The domain sid acquired from the server
4462 * @param cli A cli_state connected to the server.
4463 * @param mem_ctx Talloc context, destoyed on completion of the function.
4464 * @param argc Standard main() style argc
4465 * @param argc Standard main() style argv. Initial components are already
4466 * stripped
4468 * @return normal NTSTATUS return code
4471 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
4472 const char *domain_name,
4473 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4474 int argc, const char **argv) {
4476 POLICY_HND connect_pol, domain_pol, user_pol;
4477 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4478 char *acct_name;
4479 const char **names;
4480 DOM_SID trust_acct_sid;
4481 uint32 *user_rids, num_rids, *name_types;
4482 uint32 flags = 0x000003e8; /* Unknown */
4484 if (argc != 1) {
4485 d_printf("Usage: net rpc trustdom del <domain_name>\n");
4486 return NT_STATUS_INVALID_PARAMETER;
4490 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4492 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
4494 if (acct_name == NULL)
4495 return NT_STATUS_NO_MEMORY;
4497 strupper_m(acct_name);
4499 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
4500 names[0] = acct_name;
4503 /* Get samr policy handle */
4504 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4505 &connect_pol);
4506 if (!NT_STATUS_IS_OK(result)) {
4507 goto done;
4510 /* Get domain policy handle */
4511 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4512 MAXIMUM_ALLOWED_ACCESS,
4513 domain_sid, &domain_pol);
4514 if (!NT_STATUS_IS_OK(result)) {
4515 goto done;
4518 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, flags, 1,
4519 names, &num_rids,
4520 &user_rids, &name_types);
4522 if (!NT_STATUS_IS_OK(result)) {
4523 goto done;
4526 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
4527 MAXIMUM_ALLOWED_ACCESS,
4528 user_rids[0], &user_pol);
4530 if (!NT_STATUS_IS_OK(result)) {
4531 goto done;
4534 /* append the rid to the domain sid */
4535 sid_copy(&trust_acct_sid, domain_sid);
4536 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
4537 goto done;
4540 /* remove the sid */
4542 result = cli_samr_remove_sid_foreign_domain(cli, mem_ctx, &user_pol,
4543 &trust_acct_sid);
4545 if (!NT_STATUS_IS_OK(result)) {
4546 goto done;
4549 /* Delete user */
4551 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
4553 if (!NT_STATUS_IS_OK(result)) {
4554 goto done;
4557 if (!NT_STATUS_IS_OK(result)) {
4558 DEBUG(0,("Could not set trust account password: %s\n",
4559 nt_errstr(result)));
4560 goto done;
4563 done:
4564 return result;
4568 * Delete interdomain trust account for a remote domain.
4570 * @param argc standard argc
4571 * @param argv standard argv without initial components
4573 * @return Integer status (0 means success)
4576 static int rpc_trustdom_del(int argc, const char **argv)
4578 if (argc > 0) {
4579 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
4580 argc, argv);
4581 } else {
4582 d_printf("Usage: net rpc trustdom del <domain>\n");
4583 return -1;
4589 * Establish trust relationship to a trusting domain.
4590 * Interdomain account must already be created on remote PDC.
4592 * @param argc standard argc
4593 * @param argv standard argv without initial components
4595 * @return Integer status (0 means success)
4598 static int rpc_trustdom_establish(int argc, const char **argv)
4600 struct cli_state *cli;
4601 struct in_addr server_ip;
4602 POLICY_HND connect_hnd;
4603 TALLOC_CTX *mem_ctx;
4604 NTSTATUS nt_status;
4605 DOM_SID *domain_sid;
4606 smb_ucs2_t *uni_domain_name;
4608 char* domain_name;
4609 char* domain_name_pol;
4610 char* acct_name;
4611 fstring pdc_name;
4614 * Connect to \\server\ipc$ as 'our domain' account with password
4617 if (argc != 1) {
4618 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
4619 return -1;
4622 domain_name = smb_xstrdup(argv[0]);
4623 strupper_m(domain_name);
4625 /* account name used at first is our domain's name with '$' */
4626 asprintf(&acct_name, "%s$", lp_workgroup());
4627 strupper_m(acct_name);
4630 * opt_workgroup will be used by connection functions further,
4631 * hence it should be set to remote domain name instead of ours
4633 if (opt_workgroup) {
4634 opt_workgroup = smb_xstrdup(domain_name);
4637 opt_user_name = acct_name;
4639 /* find the domain controller */
4640 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
4641 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
4642 return -1;
4645 /* connect to ipc$ as username/password */
4646 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
4647 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
4649 /* Is it trusting domain account for sure ? */
4650 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
4651 nt_errstr(nt_status)));
4652 return -1;
4656 * Connect to \\server\ipc$ again (this time anonymously)
4659 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
4661 if (NT_STATUS_IS_ERR(nt_status)) {
4662 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
4663 domain_name, nt_errstr(nt_status)));
4667 * Use NetServerEnum2 to make sure we're talking to a proper server
4670 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
4671 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
4672 for domain %s\n", domain_name));
4675 if (!(mem_ctx = talloc_init("establishing trust relationship to "
4676 "domain %s", domain_name))) {
4677 DEBUG(0, ("talloc_init() failed\n"));
4678 cli_shutdown(cli);
4679 return -1;
4683 * Call LsaOpenPolicy and LsaQueryInfo
4686 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4687 DEBUG(0, ("Could not initialise lsa pipe\n"));
4688 cli_shutdown(cli);
4689 return -1;
4692 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
4693 &connect_hnd);
4694 if (NT_STATUS_IS_ERR(nt_status)) {
4695 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4696 nt_errstr(nt_status)));
4697 return -1;
4700 /* Querying info level 5 */
4702 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
4703 5 /* info level */,
4704 &domain_name_pol, &domain_sid);
4705 if (NT_STATUS_IS_ERR(nt_status)) {
4706 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4707 nt_errstr(nt_status)));
4708 return -1;
4711 if (push_ucs2_talloc(mem_ctx, &uni_domain_name, domain_name_pol) < 0) {
4712 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4713 domain_name_pol));
4714 return -1;
4717 /* There should be actually query info level 3 (following nt serv behaviour),
4718 but I still don't know if it's _really_ necessary */
4721 * Store the password in secrets db
4724 if (!secrets_store_trusted_domain_password(domain_name,
4725 uni_domain_name,
4726 strlen_w(uni_domain_name)+1,
4727 opt_password,
4728 *domain_sid)) {
4729 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4730 return -1;
4734 * Close the pipes and clean up
4737 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
4738 if (NT_STATUS_IS_ERR(nt_status)) {
4739 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
4740 nt_errstr(nt_status)));
4741 return -1;
4744 if (cli->pipes[cli->pipe_idx].fnum)
4745 cli_nt_session_close(cli);
4747 cli_shutdown(cli);
4749 talloc_destroy(mem_ctx);
4751 d_printf("Trust to domain %s established\n", domain_name);
4752 return 0;
4756 * Revoke trust relationship to the remote domain
4758 * @param argc standard argc
4759 * @param argv standard argv without initial components
4761 * @return Integer status (0 means success)
4764 static int rpc_trustdom_revoke(int argc, const char **argv)
4766 char* domain_name;
4768 if (argc < 1) return -1;
4770 /* generate upper cased domain name */
4771 domain_name = smb_xstrdup(argv[0]);
4772 strupper_m(domain_name);
4774 /* delete password of the trust */
4775 if (!trusted_domain_password_delete(domain_name)) {
4776 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
4777 domain_name));
4778 return -1;
4781 return 0;
4785 * Usage for 'net rpc trustdom' command
4787 * @param argc standard argc
4788 * @param argv standard argv without inital components
4790 * @return Integer status returned to shell
4793 static int rpc_trustdom_usage(int argc, const char **argv)
4795 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
4796 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
4797 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
4798 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
4799 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
4800 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
4801 return -1;
4805 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
4806 const char *domain_name,
4807 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4808 int argc, const char **argv)
4810 fstring str_sid;
4811 sid_to_string(str_sid, domain_sid);
4812 d_printf("%s\n", str_sid);
4813 return NT_STATUS_OK;
4816 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
4818 fstring ascii_sid, padding;
4819 int pad_len, col_len = 20;
4821 /* convert sid into ascii string */
4822 sid_to_string(ascii_sid, dom_sid);
4824 /* calculate padding space for d_printf to look nicer */
4825 pad_len = col_len - strlen(trusted_dom_name);
4826 padding[pad_len] = 0;
4827 do padding[--pad_len] = ' '; while (pad_len);
4829 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
4832 static NTSTATUS vampire_trusted_domain(struct cli_state *cli,
4833 TALLOC_CTX *mem_ctx,
4834 POLICY_HND *pol,
4835 DOM_SID dom_sid,
4836 const char *trusted_dom_name)
4838 NTSTATUS nt_status;
4839 LSA_TRUSTED_DOMAIN_INFO *info;
4840 char *cleartextpwd;
4841 DATA_BLOB data;
4842 smb_ucs2_t *uni_dom_name;
4844 nt_status = cli_lsa_query_trusted_domain_info_by_sid(cli, mem_ctx, pol, 4, &dom_sid, &info);
4846 if (NT_STATUS_IS_ERR(nt_status)) {
4847 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
4848 nt_errstr(nt_status)));
4849 goto done;
4852 data = data_blob(NULL, info->password.password.length);
4854 memcpy(data.data, info->password.password.data, info->password.password.length);
4855 data.length = info->password.password.length;
4857 cleartextpwd = decrypt_trustdom_secret(cli->pwd.password, &data);
4859 if (cleartextpwd == NULL) {
4860 DEBUG(0,("retrieved NULL password\n"));
4861 nt_status = NT_STATUS_UNSUCCESSFUL;
4862 goto done;
4865 if (push_ucs2_talloc(mem_ctx, &uni_dom_name, trusted_dom_name) < 0) {
4866 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4867 trusted_dom_name));
4868 nt_status = NT_STATUS_UNSUCCESSFUL;
4869 goto done;
4872 if (!secrets_store_trusted_domain_password(trusted_dom_name,
4873 uni_dom_name,
4874 strlen_w(uni_dom_name)+1,
4875 cleartextpwd,
4876 dom_sid)) {
4877 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4878 nt_status = NT_STATUS_UNSUCCESSFUL;
4879 goto done;
4882 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
4883 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
4885 done:
4886 SAFE_FREE(cleartextpwd);
4887 data_blob_free(&data);
4889 return nt_status;
4892 static int rpc_trustdom_vampire(int argc, const char **argv)
4894 /* common variables */
4895 TALLOC_CTX* mem_ctx;
4896 struct cli_state *cli;
4897 NTSTATUS nt_status;
4898 const char *domain_name = NULL;
4899 DOM_SID *queried_dom_sid;
4900 POLICY_HND connect_hnd;
4902 /* trusted domains listing variables */
4903 unsigned int num_domains, enum_ctx = 0;
4904 int i;
4905 DOM_SID *domain_sids;
4906 char **trusted_dom_names;
4907 fstring pdc_name;
4908 char *dummy;
4911 * Listing trusted domains (stored in secrets.tdb, if local)
4914 mem_ctx = talloc_init("trust relationships vampire");
4917 * set domain and pdc name to local samba server (default)
4918 * or to remote one given in command line
4921 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
4922 domain_name = opt_workgroup;
4923 opt_target_workgroup = opt_workgroup;
4924 } else {
4925 fstrcpy(pdc_name, global_myname());
4926 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
4927 opt_target_workgroup = domain_name;
4930 /* open \PIPE\lsarpc and open policy handle */
4931 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
4932 DEBUG(0, ("Couldn't connect to domain controller\n"));
4933 return -1;
4936 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4937 DEBUG(0, ("Could not initialise lsa pipe\n"));
4938 return -1;
4941 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
4942 &connect_hnd);
4943 if (NT_STATUS_IS_ERR(nt_status)) {
4944 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4945 nt_errstr(nt_status)));
4946 return -1;
4949 /* query info level 5 to obtain sid of a domain being queried */
4950 nt_status = cli_lsa_query_info_policy(
4951 cli, mem_ctx, &connect_hnd, 5 /* info level */,
4952 &dummy, &queried_dom_sid);
4954 if (NT_STATUS_IS_ERR(nt_status)) {
4955 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4956 nt_errstr(nt_status)));
4957 return -1;
4961 * Keep calling LsaEnumTrustdom over opened pipe until
4962 * the end of enumeration is reached
4965 d_printf("Vampire trusted domains:\n\n");
4967 do {
4968 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
4969 &num_domains,
4970 &trusted_dom_names, &domain_sids);
4972 if (NT_STATUS_IS_ERR(nt_status)) {
4973 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
4974 nt_errstr(nt_status)));
4975 return -1;
4978 for (i = 0; i < num_domains; i++) {
4980 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
4982 nt_status = vampire_trusted_domain(cli, mem_ctx, &connect_hnd,
4983 domain_sids[i], trusted_dom_names[i]);
4984 if (!NT_STATUS_IS_OK(nt_status))
4985 return -1;
4989 * in case of no trusted domains say something rather
4990 * than just display blank line
4992 if (!num_domains) d_printf("none\n");
4994 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
4996 /* close this connection before doing next one */
4997 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
4998 if (NT_STATUS_IS_ERR(nt_status)) {
4999 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5000 nt_errstr(nt_status)));
5001 return -1;
5004 /* close lsarpc pipe and connection to IPC$ */
5005 cli_nt_session_close(cli);
5006 cli_shutdown(cli);
5008 talloc_destroy(mem_ctx);
5009 return 0;
5012 static int rpc_trustdom_list(int argc, const char **argv)
5014 /* common variables */
5015 TALLOC_CTX* mem_ctx;
5016 struct cli_state *cli, *remote_cli;
5017 NTSTATUS nt_status;
5018 const char *domain_name = NULL;
5019 DOM_SID *queried_dom_sid;
5020 fstring padding;
5021 int ascii_dom_name_len;
5022 POLICY_HND connect_hnd;
5024 /* trusted domains listing variables */
5025 unsigned int num_domains, enum_ctx = 0;
5026 int i, pad_len, col_len = 20;
5027 DOM_SID *domain_sids;
5028 char **trusted_dom_names;
5029 fstring pdc_name;
5030 char *dummy;
5032 /* trusting domains listing variables */
5033 POLICY_HND domain_hnd;
5034 char **trusting_dom_names;
5035 uint32 *trusting_dom_rids;
5038 * Listing trusted domains (stored in secrets.tdb, if local)
5041 mem_ctx = talloc_init("trust relationships listing");
5044 * set domain and pdc name to local samba server (default)
5045 * or to remote one given in command line
5048 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5049 domain_name = opt_workgroup;
5050 opt_target_workgroup = opt_workgroup;
5051 } else {
5052 fstrcpy(pdc_name, global_myname());
5053 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5054 opt_target_workgroup = domain_name;
5057 /* open \PIPE\lsarpc and open policy handle */
5058 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5059 DEBUG(0, ("Couldn't connect to domain controller\n"));
5060 return -1;
5063 if (!cli_nt_session_open(cli, PI_LSARPC)) {
5064 DEBUG(0, ("Could not initialise lsa pipe\n"));
5065 return -1;
5068 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5069 &connect_hnd);
5070 if (NT_STATUS_IS_ERR(nt_status)) {
5071 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5072 nt_errstr(nt_status)));
5073 return -1;
5076 /* query info level 5 to obtain sid of a domain being queried */
5077 nt_status = cli_lsa_query_info_policy(
5078 cli, mem_ctx, &connect_hnd, 5 /* info level */,
5079 &dummy, &queried_dom_sid);
5081 if (NT_STATUS_IS_ERR(nt_status)) {
5082 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5083 nt_errstr(nt_status)));
5084 return -1;
5088 * Keep calling LsaEnumTrustdom over opened pipe until
5089 * the end of enumeration is reached
5092 d_printf("Trusted domains list:\n\n");
5094 do {
5095 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
5096 &num_domains,
5097 &trusted_dom_names, &domain_sids);
5099 if (NT_STATUS_IS_ERR(nt_status)) {
5100 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5101 nt_errstr(nt_status)));
5102 return -1;
5105 for (i = 0; i < num_domains; i++) {
5106 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5110 * in case of no trusted domains say something rather
5111 * than just display blank line
5113 if (!num_domains) d_printf("none\n");
5115 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5117 /* close this connection before doing next one */
5118 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
5119 if (NT_STATUS_IS_ERR(nt_status)) {
5120 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5121 nt_errstr(nt_status)));
5122 return -1;
5125 cli_nt_session_close(cli);
5128 * Listing trusting domains (stored in passdb backend, if local)
5131 d_printf("\nTrusting domains list:\n\n");
5134 * Open \PIPE\samr and get needed policy handles
5136 if (!cli_nt_session_open(cli, PI_SAMR)) {
5137 DEBUG(0, ("Could not initialise samr pipe\n"));
5138 return -1;
5141 /* SamrConnect */
5142 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
5143 &connect_hnd);
5144 if (!NT_STATUS_IS_OK(nt_status)) {
5145 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
5146 nt_errstr(nt_status)));
5147 return -1;
5150 /* SamrOpenDomain - we have to open domain policy handle in order to be
5151 able to enumerate accounts*/
5152 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
5153 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
5154 queried_dom_sid, &domain_hnd);
5155 if (!NT_STATUS_IS_OK(nt_status)) {
5156 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
5157 nt_errstr(nt_status)));
5158 return -1;
5162 * perform actual enumeration
5165 enum_ctx = 0; /* reset enumeration context from last enumeration */
5166 do {
5168 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
5169 &enum_ctx, ACB_DOMTRUST, 0xffff,
5170 &trusting_dom_names, &trusting_dom_rids,
5171 &num_domains);
5172 if (NT_STATUS_IS_ERR(nt_status)) {
5173 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
5174 nt_errstr(nt_status)));
5175 return -1;
5178 for (i = 0; i < num_domains; i++) {
5181 * get each single domain's sid (do we _really_ need this ?):
5182 * 1) connect to domain's pdc
5183 * 2) query the pdc for domain's sid
5186 /* get rid of '$' tail */
5187 ascii_dom_name_len = strlen(trusting_dom_names[i]);
5188 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
5189 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
5191 /* calculate padding space for d_printf to look nicer */
5192 pad_len = col_len - strlen(trusting_dom_names[i]);
5193 padding[pad_len] = 0;
5194 do padding[--pad_len] = ' '; while (pad_len);
5196 /* set opt_* variables to remote domain */
5197 strupper_m(trusting_dom_names[i]);
5198 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
5199 opt_target_workgroup = opt_workgroup;
5201 d_printf("%s%s", trusting_dom_names[i], padding);
5203 /* connect to remote domain controller */
5204 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
5205 if (remote_cli) {
5206 /* query for domain's sid */
5207 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
5208 d_printf("couldn't get domain's sid\n");
5210 cli_shutdown(remote_cli);
5212 } else {
5213 d_printf("domain controller is not responding\n");
5217 if (!num_domains) d_printf("none\n");
5219 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5221 /* close opened samr and domain policy handles */
5222 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
5223 if (!NT_STATUS_IS_OK(nt_status)) {
5224 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
5227 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
5228 if (!NT_STATUS_IS_OK(nt_status)) {
5229 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
5232 /* close samr pipe and connection to IPC$ */
5233 cli_nt_session_close(cli);
5234 cli_shutdown(cli);
5236 talloc_destroy(mem_ctx);
5237 return 0;
5241 * Entrypoint for 'net rpc trustdom' code
5243 * @param argc standard argc
5244 * @param argv standard argv without initial components
5246 * @return Integer status (0 means success)
5249 static int rpc_trustdom(int argc, const char **argv)
5251 struct functable func[] = {
5252 {"add", rpc_trustdom_add},
5253 {"del", rpc_trustdom_del},
5254 {"establish", rpc_trustdom_establish},
5255 {"revoke", rpc_trustdom_revoke},
5256 {"help", rpc_trustdom_usage},
5257 {"list", rpc_trustdom_list},
5258 {"vampire", rpc_trustdom_vampire},
5259 {NULL, NULL}
5262 if (argc == 0) {
5263 rpc_trustdom_usage(argc, argv);
5264 return -1;
5267 return (net_run_function(argc, argv, func, rpc_user_usage));
5271 * Check if a server will take rpc commands
5272 * @param flags Type of server to connect to (PDC, DMB, localhost)
5273 * if the host is not explicitly specified
5274 * @return BOOL (true means rpc supported)
5276 BOOL net_rpc_check(unsigned flags)
5278 struct cli_state cli;
5279 BOOL ret = False;
5280 struct in_addr server_ip;
5281 char *server_name = NULL;
5283 /* flags (i.e. server type) may depend on command */
5284 if (!net_find_server(flags, &server_ip, &server_name))
5285 return False;
5287 ZERO_STRUCT(cli);
5288 if (cli_initialise(&cli) == False)
5289 return False;
5291 if (!cli_connect(&cli, server_name, &server_ip))
5292 goto done;
5293 if (!attempt_netbios_session_request(&cli, global_myname(),
5294 server_name, &server_ip))
5295 goto done;
5296 if (!cli_negprot(&cli))
5297 goto done;
5298 if (cli.protocol < PROTOCOL_NT1)
5299 goto done;
5301 ret = True;
5302 done:
5303 cli_shutdown(&cli);
5304 return ret;
5307 /* dump sam database via samsync rpc calls */
5308 static int rpc_samdump(int argc, const char **argv) {
5309 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
5310 argc, argv);
5313 /* syncronise sam database via samsync rpc calls */
5314 static int rpc_vampire(int argc, const char **argv) {
5315 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
5316 argc, argv);
5319 /**
5320 * Migrate everything from a print-server
5322 * @param argc Standard main() style argc
5323 * @param argv Standard main() style argv. Initial components are already
5324 * stripped
5326 * @return A shell status integer (0 for success)
5328 * The order is important !
5329 * To successfully add drivers the print-queues have to exist !
5330 * Applying ACLs should be the last step, because you're easily locked out
5333 static int rpc_printer_migrate_all(int argc, const char **argv)
5335 int ret;
5337 if (!opt_host) {
5338 printf("no server to migrate\n");
5339 return -1;
5342 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
5343 if (ret)
5344 return ret;
5346 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
5347 if (ret)
5348 return ret;
5350 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
5351 if (ret)
5352 return ret;
5354 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
5355 if (ret)
5356 return ret;
5358 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
5362 /**
5363 * Migrate print-drivers from a print-server
5365 * @param argc Standard main() style argc
5366 * @param argv Standard main() style argv. Initial components are already
5367 * stripped
5369 * @return A shell status integer (0 for success)
5371 static int rpc_printer_migrate_drivers(int argc, const char **argv)
5373 if (!opt_host) {
5374 printf("no server to migrate\n");
5375 return -1;
5378 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5379 rpc_printer_migrate_drivers_internals,
5380 argc, argv);
5383 /**
5384 * Migrate print-forms from a print-server
5386 * @param argc Standard main() style argc
5387 * @param argv Standard main() style argv. Initial components are already
5388 * stripped
5390 * @return A shell status integer (0 for success)
5392 static int rpc_printer_migrate_forms(int argc, const char **argv)
5394 if (!opt_host) {
5395 printf("no server to migrate\n");
5396 return -1;
5399 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5400 rpc_printer_migrate_forms_internals,
5401 argc, argv);
5404 /**
5405 * Migrate printers from a print-server
5407 * @param argc Standard main() style argc
5408 * @param argv Standard main() style argv. Initial components are already
5409 * stripped
5411 * @return A shell status integer (0 for success)
5413 static int rpc_printer_migrate_printers(int argc, const char **argv)
5415 if (!opt_host) {
5416 printf("no server to migrate\n");
5417 return -1;
5420 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5421 rpc_printer_migrate_printers_internals,
5422 argc, argv);
5425 /**
5426 * Migrate printer-ACLs from a print-server
5428 * @param argc Standard main() style argc
5429 * @param argv Standard main() style argv. Initial components are already
5430 * stripped
5432 * @return A shell status integer (0 for success)
5434 static int rpc_printer_migrate_security(int argc, const char **argv)
5436 if (!opt_host) {
5437 printf("no server to migrate\n");
5438 return -1;
5441 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5442 rpc_printer_migrate_security_internals,
5443 argc, argv);
5446 /**
5447 * Migrate printer-settings from a print-server
5449 * @param argc Standard main() style argc
5450 * @param argv Standard main() style argv. Initial components are already
5451 * stripped
5453 * @return A shell status integer (0 for success)
5455 static int rpc_printer_migrate_settings(int argc, const char **argv)
5457 if (!opt_host) {
5458 printf("no server to migrate\n");
5459 return -1;
5462 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5463 rpc_printer_migrate_settings_internals,
5464 argc, argv);
5467 /**
5468 * 'net rpc printer' entrypoint.
5469 * @param argc Standard main() style argc
5470 * @param argv Standard main() style argv. Initial components are already
5471 * stripped
5474 int rpc_printer_migrate(int argc, const char **argv)
5477 /* ouch: when addriver and setdriver are called from within
5478 rpc_printer_migrate_drivers_internals, the printer-queue already
5479 *has* to exist */
5481 struct functable func[] = {
5482 {"all", rpc_printer_migrate_all},
5483 {"drivers", rpc_printer_migrate_drivers},
5484 {"forms", rpc_printer_migrate_forms},
5485 {"help", rpc_printer_usage},
5486 {"printers", rpc_printer_migrate_printers},
5487 {"security", rpc_printer_migrate_security},
5488 {"settings", rpc_printer_migrate_settings},
5489 {NULL, NULL}
5492 return net_run_function(argc, argv, func, rpc_printer_usage);
5496 /**
5497 * List printers on a remote RPC server
5499 * @param argc Standard main() style argc
5500 * @param argv Standard main() style argv. Initial components are already
5501 * stripped
5503 * @return A shell status integer (0 for success)
5505 static int rpc_printer_list(int argc, const char **argv)
5508 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5509 rpc_printer_list_internals,
5510 argc, argv);
5513 /**
5514 * List printer-drivers on a remote RPC server
5516 * @param argc Standard main() style argc
5517 * @param argv Standard main() style argv. Initial components are already
5518 * stripped
5520 * @return A shell status integer (0 for success)
5522 static int rpc_printer_driver_list(int argc, const char **argv)
5525 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5526 rpc_printer_driver_list_internals,
5527 argc, argv);
5530 /**
5531 * Publish printer in ADS via MSRPC
5533 * @param argc Standard main() style argc
5534 * @param argv Standard main() style argv. Initial components are already
5535 * stripped
5537 * @return A shell status integer (0 for success)
5539 static int rpc_printer_publish_publish(int argc, const char **argv)
5542 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5543 rpc_printer_publish_publish_internals,
5544 argc, argv);
5547 /**
5548 * Update printer in ADS via MSRPC
5550 * @param argc Standard main() style argc
5551 * @param argv Standard main() style argv. Initial components are already
5552 * stripped
5554 * @return A shell status integer (0 for success)
5556 static int rpc_printer_publish_update(int argc, const char **argv)
5559 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5560 rpc_printer_publish_update_internals,
5561 argc, argv);
5564 /**
5565 * UnPublish printer in ADS via MSRPC
5567 * @param argc Standard main() style argc
5568 * @param argv Standard main() style argv. Initial components are already
5569 * stripped
5571 * @return A shell status integer (0 for success)
5573 static int rpc_printer_publish_unpublish(int argc, const char **argv)
5576 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5577 rpc_printer_publish_unpublish_internals,
5578 argc, argv);
5581 /**
5582 * List published printers via MSRPC
5584 * @param argc Standard main() style argc
5585 * @param argv Standard main() style argv. Initial components are already
5586 * stripped
5588 * @return A shell status integer (0 for success)
5590 static int rpc_printer_publish_list(int argc, const char **argv)
5593 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5594 rpc_printer_publish_list_internals,
5595 argc, argv);
5599 /**
5600 * Publish printer in ADS
5602 * @param argc Standard main() style argc
5603 * @param argv Standard main() style argv. Initial components are already
5604 * stripped
5606 * @return A shell status integer (0 for success)
5608 static int rpc_printer_publish(int argc, const char **argv)
5611 struct functable func[] = {
5612 {"publish", rpc_printer_publish_publish},
5613 {"update", rpc_printer_publish_update},
5614 {"unpublish", rpc_printer_publish_unpublish},
5615 {"list", rpc_printer_publish_list},
5616 {"help", rpc_printer_usage},
5617 {NULL, NULL}
5620 if (argc == 0)
5621 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5622 rpc_printer_publish_list_internals,
5623 argc, argv);
5625 return net_run_function(argc, argv, func, rpc_printer_usage);
5630 /**
5631 * Display rpc printer help page.
5632 * @param argc Standard main() style argc
5633 * @param argv Standard main() style argv. Initial components are already
5634 * stripped
5636 int rpc_printer_usage(int argc, const char **argv)
5638 return net_help_printer(argc, argv);
5641 /**
5642 * 'net rpc printer' entrypoint.
5643 * @param argc Standard main() style argc
5644 * @param argv Standard main() style argv. Initial components are already
5645 * stripped
5647 int net_rpc_printer(int argc, const char **argv)
5649 struct functable func[] = {
5650 {"list", rpc_printer_list},
5651 {"migrate", rpc_printer_migrate},
5652 {"driver", rpc_printer_driver_list},
5653 {"publish", rpc_printer_publish},
5654 {NULL, NULL}
5657 if (argc == 0)
5658 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5659 rpc_printer_list_internals,
5660 argc, argv);
5662 return net_run_function(argc, argv, func, rpc_printer_usage);
5665 /****************************************************************************/
5668 /**
5669 * Basic usage function for 'net rpc'
5670 * @param argc Standard main() style argc
5671 * @param argv Standard main() style argv. Initial components are already
5672 * stripped
5675 int net_rpc_usage(int argc, const char **argv)
5677 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
5678 d_printf(" net rpc join \t\t\tto join a domain \n");
5679 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
5680 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
5681 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
5682 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
5683 d_printf(" net rpc group \t\tto list groups\n");
5684 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
5685 d_printf(" net rpc printer \t\tto list and migrate printers\n");
5686 d_printf(" net rpc file \t\t\tto list open files\n");
5687 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
5688 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
5689 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
5690 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
5691 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
5692 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
5693 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
5694 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
5695 d_printf(" net rpc registry\t\tto manage registry hives\n");
5696 d_printf("\n");
5697 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
5698 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
5699 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
5700 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
5701 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
5702 return -1;
5707 * Help function for 'net rpc'. Calls command specific help if requested
5708 * or displays usage of net rpc
5709 * @param argc Standard main() style argc
5710 * @param argv Standard main() style argv. Initial components are already
5711 * stripped
5714 int net_rpc_help(int argc, const char **argv)
5716 struct functable func[] = {
5717 {"join", rpc_join_usage},
5718 {"user", rpc_user_usage},
5719 {"group", rpc_group_usage},
5720 {"share", rpc_share_usage},
5721 /*{"changetrustpw", rpc_changetrustpw_usage}, */
5722 {"trustdom", rpc_trustdom_usage},
5723 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
5724 /*{"shutdown", rpc_shutdown_usage}, */
5725 {NULL, NULL}
5728 if (argc == 0) {
5729 net_rpc_usage(argc, argv);
5730 return -1;
5733 return (net_run_function(argc, argv, func, rpc_user_usage));
5737 /**
5738 * 'net rpc' entrypoint.
5739 * @param argc Standard main() style argc
5740 * @param argv Standard main() style argv. Initial components are already
5741 * stripped
5744 int net_rpc(int argc, const char **argv)
5746 struct functable func[] = {
5747 {"info", net_rpc_info},
5748 {"join", net_rpc_join},
5749 {"oldjoin", net_rpc_oldjoin},
5750 {"testjoin", net_rpc_testjoin},
5751 {"user", net_rpc_user},
5752 {"password", rpc_user_password},
5753 {"group", net_rpc_group},
5754 {"share", net_rpc_share},
5755 {"file", net_rpc_file},
5756 {"printer", net_rpc_printer},
5757 {"changetrustpw", net_rpc_changetrustpw},
5758 {"trustdom", rpc_trustdom},
5759 {"abortshutdown", rpc_shutdown_abort},
5760 {"shutdown", rpc_shutdown},
5761 {"samdump", rpc_samdump},
5762 {"vampire", rpc_vampire},
5763 {"getsid", net_rpc_getsid},
5764 {"rights", net_rpc_rights},
5765 {"service", net_rpc_service},
5766 {"registry", net_rpc_registry},
5767 {"help", net_rpc_help},
5768 {NULL, NULL}
5770 return net_run_function(argc, argv, func, net_rpc_usage);