r7609: Move top level dir handling in from of the loop. This makes the code
[Samba/bb.git] / source / utils / net_rpc.c
blob614f19c803db522ddbae64ff915e0281b801a74f
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);
2783 * Copy a file/dir
2785 * @param f file_info
2786 * @param mask current search mask
2787 * @param state arg-pointer
2790 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
2792 static NTSTATUS nt_status;
2793 static struct copy_clistate *local_state;
2794 static fstring filename, new_mask;
2795 fstring dir;
2796 char *old_dir;
2798 local_state = (struct copy_clistate *)state;
2799 nt_status = NT_STATUS_UNSUCCESSFUL;
2801 if (strequal(f->name, ".") || strequal(f->name, ".."))
2802 return;
2804 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
2806 /* DIRECTORY */
2807 if (f->mode & aDIR) {
2809 DEBUG(3,("got dir: %s\n", f->name));
2811 fstrcpy(dir, local_state->cwd);
2812 fstrcat(dir, "\\");
2813 fstrcat(dir, f->name);
2815 switch (local_state->mode)
2817 case NET_MODE_SHARE_MIGRATE:
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);
2827 break;
2828 default:
2829 d_printf("Unsupported mode %d\n", local_state->mode);
2830 return;
2833 if (!NT_STATUS_IS_OK(nt_status))
2834 printf("could not handle dir %s: %s\n",
2835 dir, nt_errstr(nt_status));
2837 /* search below that directory */
2838 fstrcpy(new_mask, dir);
2839 fstrcat(new_mask, "\\*");
2841 old_dir = local_state->cwd;
2842 local_state->cwd = dir;
2843 if (!sync_files(local_state, new_mask))
2844 printf("could not handle files\n");
2845 local_state->cwd = old_dir;
2847 return;
2851 /* FILE */
2852 fstrcpy(filename, local_state->cwd);
2853 fstrcat(filename, "\\");
2854 fstrcat(filename, f->name);
2856 DEBUG(3,("got file: %s\n", filename));
2858 switch (local_state->mode)
2860 case NET_MODE_SHARE_MIGRATE:
2861 nt_status = net_copy_file(local_state->mem_ctx,
2862 local_state->cli_share_src,
2863 local_state->cli_share_dst,
2864 filename, filename,
2865 opt_acls? True : False,
2866 opt_attrs? True : False,
2867 opt_timestamps? True: False,
2868 True);
2869 break;
2870 default:
2871 d_printf("Unsupported file mode %d\n", local_state->mode);
2872 return;
2875 if (!NT_STATUS_IS_OK(nt_status))
2876 printf("could not handle file %s: %s\n",
2877 filename, nt_errstr(nt_status));
2882 * sync files, can be called recursivly to list files
2883 * and then call copy_fn for each file
2885 * @param cp_clistate pointer to the copy_clistate we work with
2886 * @param mask the current search mask
2888 * @return Boolean result
2890 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
2893 DEBUG(3,("calling cli_list with mask: %s\n", mask));
2895 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
2896 d_printf("listing %s failed with error: %s\n",
2897 mask, cli_errstr(cp_clistate->cli_share_src));
2898 return False;
2901 return True;
2906 * Set the top level directory permissions before we do any further copies.
2907 * Should set up ACL inheritance.
2910 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
2911 const char *sharename)
2913 NTSTATUS nt_status;
2915 switch (cp_clistate->mode) {
2916 case NET_MODE_SHARE_MIGRATE:
2917 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
2918 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
2919 cp_clistate->cli_share_src,
2920 cp_clistate->cli_share_dst,
2921 "\\", "\\",
2922 opt_acls? True : False,
2923 opt_attrs? True : False,
2924 opt_timestamps? True: False,
2925 False);
2926 break;
2927 default:
2928 d_printf("Unsupported mode %d\n", cp_clistate->mode);
2929 break;
2932 if (!NT_STATUS_IS_OK(nt_status)) {
2933 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
2934 sharename, nt_errstr(nt_status));
2935 return False;
2938 return True;
2942 /**
2943 * Sync all files inside a remote share to another share (over smb)
2945 * All parameters are provided by the run_rpc_command function, except for
2946 * argc, argv which are passes through.
2948 * @param domain_sid The domain sid acquired from the remote server
2949 * @param cli A cli_state connected to the server.
2950 * @param mem_ctx Talloc context, destoyed on completion of the function.
2951 * @param argc Standard main() style argc
2952 * @param argv Standard main() style argv. Initial components are already
2953 * stripped
2955 * @return Normal NTSTATUS return.
2957 static NTSTATUS
2958 rpc_share_migrate_files_internals(const DOM_SID *domain_sid, const char *domain_name,
2959 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2960 int argc, const char **argv)
2962 WERROR result;
2963 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2964 SRV_SHARE_INFO_CTR ctr_src;
2965 ENUM_HND hnd;
2966 uint32 preferred_len = 0xffffffff, i;
2967 uint32 level = 2;
2968 struct copy_clistate cp_clistate;
2969 BOOL got_src_share = False;
2970 BOOL got_dst_share = False;
2971 pstring mask;
2972 char *dst = NULL;
2974 /* decrese argc and safe mode */
2975 cp_clistate.mode = argv[--argc][0];
2977 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
2979 init_enum_hnd(&hnd, 0);
2981 result = cli_srvsvc_net_share_enum(
2982 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2984 if (!W_ERROR_IS_OK(result))
2985 goto done;
2987 for (i = 0; i < ctr_src.num_entries; i++) {
2989 fstring netname = "", remark = "", path = "";
2991 rpcstr_pull_unistr2_fstring(
2992 netname, &ctr_src.share.info2[i].info_2_str.uni_netname);
2993 rpcstr_pull_unistr2_fstring(
2994 remark, &ctr_src.share.info2[i].info_2_str.uni_remark);
2995 rpcstr_pull_unistr2_fstring(
2996 path, &ctr_src.share.info2[i].info_2_str.uni_path);
2998 /* since we do not have NetShareGetInfo implemented in samba3 we
2999 only can skip inside the enum-ctr_src */
3000 if (argc == 1) {
3001 char *one_share = talloc_strdup(mem_ctx, argv[0]);
3002 if (!strequal(netname, one_share))
3003 continue;
3006 /* skip builtin and hidden shares
3007 In particular, one might not want to mirror whole discs :) */
3008 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$"))
3009 continue;
3011 if (strequal(netname, "print$") || netname[1] == '$') {
3012 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3013 continue;
3016 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3017 printf("excluding [%s]\n", netname);
3018 continue;
3021 /* only work with file-shares */
3022 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3023 d_printf("skipping [%s]: not a file share.\n", netname);
3024 continue;
3027 if (!cli_tdis(cli))
3028 return NT_STATUS_UNSUCCESSFUL;
3030 switch (cp_clistate.mode)
3032 case NET_MODE_SHARE_MIGRATE:
3033 printf("syncing");
3034 break;
3035 default:
3036 d_printf("Unsupported mode %d\n", cp_clistate.mode);
3037 break;
3039 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3040 netname,
3041 opt_acls ? "including" : "without",
3042 opt_attrs ? "including" : "without",
3043 opt_timestamps ? "(preserving timestamps)" : "");
3045 cp_clistate.mem_ctx = mem_ctx;
3046 cp_clistate.cli_share_src = NULL;
3047 cp_clistate.cli_share_dst = NULL;
3048 cp_clistate.cwd = NULL;
3049 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3051 /* open share source */
3052 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3053 &cli->dest_ip, cli->desthost,
3054 netname, "A:");
3055 if (!NT_STATUS_IS_OK(nt_status))
3056 goto done;
3058 got_src_share = True;
3060 if (cp_clistate.mode == NET_MODE_SHARE_MIGRATE) {
3061 /* open share destination */
3062 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3063 NULL, dst, netname, "A:");
3064 if (!NT_STATUS_IS_OK(nt_status))
3065 goto done;
3067 got_dst_share = True;
3070 if (!copy_top_level_perms(&cp_clistate, netname)) {
3071 d_printf("Could not handle the top level directory permissions for the share: %s\n", netname);
3072 nt_status = NT_STATUS_UNSUCCESSFUL;
3073 goto done;
3076 /* now call the filesync */
3077 pstrcpy(mask, "\\*");
3080 if (!sync_files(&cp_clistate, mask)) {
3081 d_printf("could not handle files for share: %s\n", netname);
3082 nt_status = NT_STATUS_UNSUCCESSFUL;
3083 goto done;
3087 nt_status = NT_STATUS_OK;
3089 done:
3091 if (got_src_share)
3092 cli_shutdown(cp_clistate.cli_share_src);
3094 if (got_dst_share)
3095 cli_shutdown(cp_clistate.cli_share_dst);
3097 return nt_status;
3101 static int rpc_share_migrate_files(int argc, const char **argv)
3104 if (!opt_host) {
3105 printf("no server to migrate\n");
3106 return -1;
3109 return run_rpc_command(NULL, PI_SRVSVC, 0,
3110 rpc_share_migrate_files_internals,
3111 argc, argv);
3114 /**
3115 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3116 * from one server to another
3118 * @param argc Standard main() style argc
3119 * @param argv Standard main() style argv. Initial components are already
3120 * stripped
3122 * @return A shell status integer (0 for success)
3125 static int rpc_share_migrate_all(int argc, const char **argv)
3127 int ret;
3129 if (!opt_host) {
3130 printf("no server to migrate\n");
3131 return -1;
3134 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3135 if (ret)
3136 return ret;
3137 #if 0
3138 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_security_internals, argc, argv);
3139 if (ret)
3140 return ret;
3141 #endif
3142 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3146 /**
3147 * 'net rpc share migrate' entrypoint.
3148 * @param argc Standard main() style argc
3149 * @param argv Standard main() style argv. Initial components are already
3150 * stripped
3152 static int rpc_share_migrate(int argc, const char **argv)
3155 struct functable func[] = {
3156 {"all", rpc_share_migrate_all},
3157 {"files", rpc_share_migrate_files},
3158 {"help", rpc_share_usage},
3159 /* {"security", rpc_share_migrate_security},*/
3160 {"shares", rpc_share_migrate_shares},
3161 {NULL, NULL}
3164 char mode = NET_MODE_SHARE_MIGRATE;
3165 argv[argc++] = &mode;
3167 return net_run_function(argc, argv, func, rpc_share_usage);
3170 struct full_alias {
3171 DOM_SID sid;
3172 int num_members;
3173 DOM_SID *members;
3176 static int num_server_aliases;
3177 static struct full_alias *server_aliases;
3180 * Add an alias to the static list.
3182 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3184 if (server_aliases == NULL)
3185 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3187 server_aliases[num_server_aliases] = *alias;
3188 num_server_aliases += 1;
3192 * For a specific domain on the server, fetch all the aliases
3193 * and their members. Add all of them to the server_aliases.
3195 static NTSTATUS
3196 rpc_fetch_domain_aliases(struct cli_state *cli, TALLOC_CTX *mem_ctx,
3197 POLICY_HND *connect_pol,
3198 const DOM_SID *domain_sid)
3200 uint32 start_idx, max_entries, num_entries, i;
3201 struct acct_info *groups;
3202 NTSTATUS result;
3203 POLICY_HND domain_pol;
3205 /* Get domain policy handle */
3207 result = cli_samr_open_domain(cli, mem_ctx, connect_pol,
3208 MAXIMUM_ALLOWED_ACCESS,
3209 domain_sid, &domain_pol);
3210 if (!NT_STATUS_IS_OK(result))
3211 return result;
3213 start_idx = 0;
3214 max_entries = 250;
3216 do {
3217 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
3218 &start_idx, max_entries,
3219 &groups, &num_entries);
3221 for (i = 0; i < num_entries; i++) {
3223 POLICY_HND alias_pol;
3224 struct full_alias alias;
3225 DOM_SID *members;
3226 int j;
3228 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
3229 MAXIMUM_ALLOWED_ACCESS,
3230 groups[i].rid,
3231 &alias_pol);
3232 if (!NT_STATUS_IS_OK(result))
3233 goto done;
3235 result = cli_samr_query_aliasmem(cli, mem_ctx,
3236 &alias_pol,
3237 &alias.num_members,
3238 &members);
3239 if (!NT_STATUS_IS_OK(result))
3240 goto done;
3242 result = cli_samr_close(cli, mem_ctx, &alias_pol);
3243 if (!NT_STATUS_IS_OK(result))
3244 goto done;
3246 alias.members = NULL;
3248 if (alias.num_members > 0) {
3249 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3251 for (j = 0; j < alias.num_members; j++)
3252 sid_copy(&alias.members[j],
3253 &members[j]);
3256 sid_copy(&alias.sid, domain_sid);
3257 sid_append_rid(&alias.sid, groups[i].rid);
3259 push_alias(mem_ctx, &alias);
3261 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3263 result = NT_STATUS_OK;
3265 done:
3266 cli_samr_close(cli, mem_ctx, &domain_pol);
3268 return result;
3272 * Dump server_aliases as names for debugging purposes.
3274 static NTSTATUS
3275 rpc_aliaslist_dump(const DOM_SID *domain_sid, const char *domain_name,
3276 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3277 int argc, const char **argv)
3279 int i;
3280 NTSTATUS result;
3281 POLICY_HND lsa_pol;
3283 result = cli_lsa_open_policy(cli, mem_ctx, True,
3284 SEC_RIGHTS_MAXIMUM_ALLOWED,
3285 &lsa_pol);
3286 if (!NT_STATUS_IS_OK(result))
3287 return result;
3289 for (i=0; i<num_server_aliases; i++) {
3290 char **names;
3291 char **domains;
3292 uint32 *types;
3293 int j;
3295 struct full_alias *alias = &server_aliases[i];
3297 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, 1,
3298 &alias->sid,
3299 &domains, &names, &types);
3300 if (!NT_STATUS_IS_OK(result))
3301 continue;
3303 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3305 if (alias->num_members == 0) {
3306 DEBUG(1, ("\n"));
3307 continue;
3310 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol,
3311 alias->num_members,
3312 alias->members,
3313 &domains, &names, &types);
3315 if (!NT_STATUS_IS_OK(result) &&
3316 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3317 continue;
3319 for (j=0; j<alias->num_members; j++)
3320 DEBUG(1, ("%s\\%s (%d); ",
3321 domains[j] ? domains[j] : "*unknown*",
3322 names[j] ? names[j] : "*unknown*",types[j]));
3323 DEBUG(1, ("\n"));
3326 cli_lsa_close(cli, mem_ctx, &lsa_pol);
3328 return NT_STATUS_OK;
3332 * Fetch a list of all server aliases and their members into
3333 * server_aliases.
3335 static NTSTATUS
3336 rpc_aliaslist_internals(const DOM_SID *domain_sid, const char *domain_name,
3337 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3338 int argc, const char **argv)
3340 NTSTATUS result;
3341 POLICY_HND connect_pol;
3343 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
3344 &connect_pol);
3346 if (!NT_STATUS_IS_OK(result))
3347 goto done;
3349 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3350 &global_sid_Builtin);
3352 if (!NT_STATUS_IS_OK(result))
3353 goto done;
3355 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3356 domain_sid);
3358 cli_samr_close(cli, mem_ctx, &connect_pol);
3359 done:
3360 return result;
3363 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3365 token->num_sids = 4;
3367 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
3369 token->user_sids[0] = *user_sid;
3370 sid_copy(&token->user_sids[1], &global_sid_World);
3371 sid_copy(&token->user_sids[2], &global_sid_Network);
3372 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3375 static void free_user_token(NT_USER_TOKEN *token)
3377 SAFE_FREE(token->user_sids);
3380 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
3382 int i;
3384 for (i=0; i<token->num_sids; i++) {
3385 if (sid_compare(sid, &token->user_sids[i]) == 0)
3386 return True;
3388 return False;
3391 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3393 if (is_sid_in_token(token, sid))
3394 return;
3396 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3398 sid_copy(&token->user_sids[token->num_sids], sid);
3400 token->num_sids += 1;
3403 struct user_token {
3404 fstring name;
3405 NT_USER_TOKEN token;
3408 static void dump_user_token(struct user_token *token)
3410 int i;
3412 d_printf("%s\n", token->name);
3414 for (i=0; i<token->token.num_sids; i++) {
3415 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
3419 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
3421 int i;
3423 for (i=0; i<alias->num_members; i++) {
3424 if (sid_compare(sid, &alias->members[i]) == 0)
3425 return True;
3428 return False;
3431 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
3433 int i;
3435 for (i=0; i<num_server_aliases; i++) {
3436 if (is_alias_member(&sid, &server_aliases[i]))
3437 add_sid_to_token(token, &server_aliases[i].sid);
3442 * We got a user token with all the SIDs we can know about without asking the
3443 * server directly. These are the user and domain group sids. All of these can
3444 * be members of aliases. So scan the list of aliases for each of the SIDs and
3445 * add them to the token.
3448 static void collect_alias_memberships(NT_USER_TOKEN *token)
3450 int num_global_sids = token->num_sids;
3451 int i;
3453 for (i=0; i<num_global_sids; i++) {
3454 collect_sid_memberships(token, token->user_sids[i]);
3458 static BOOL get_user_sids(const char *domain, const char *user,
3459 NT_USER_TOKEN *token)
3461 struct winbindd_request request;
3462 struct winbindd_response response;
3463 fstring full_name;
3464 NSS_STATUS result;
3466 DOM_SID user_sid;
3468 int i;
3470 fstr_sprintf(full_name, "%s%c%s",
3471 domain, *lp_winbind_separator(), user);
3473 /* First let's find out the user sid */
3475 ZERO_STRUCT(request);
3476 ZERO_STRUCT(response);
3478 fstrcpy(request.data.name.dom_name, domain);
3479 fstrcpy(request.data.name.name, user);
3481 result = winbindd_request(WINBINDD_LOOKUPNAME, &request, &response);
3483 if (result != NSS_STATUS_SUCCESS) {
3484 DEBUG(1, ("winbind could not find %s\n", full_name));
3485 return False;
3488 if (response.data.sid.type != SID_NAME_USER) {
3489 DEBUG(1, ("%s is not a user\n", full_name));
3490 return False;
3493 string_to_sid(&user_sid, response.data.sid.sid);
3495 init_user_token(token, &user_sid);
3497 /* And now the groups winbind knows about */
3499 ZERO_STRUCT(response);
3501 fstrcpy(request.data.username, full_name);
3503 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
3505 if (result != NSS_STATUS_SUCCESS) {
3506 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
3507 return False;
3510 for (i = 0; i < response.data.num_entries; i++) {
3511 gid_t gid = ((gid_t *)response.extra_data)[i];
3512 DOM_SID sid;
3514 struct winbindd_request sidrequest;
3515 struct winbindd_response sidresponse;
3517 ZERO_STRUCT(sidrequest);
3518 ZERO_STRUCT(sidresponse);
3520 sidrequest.data.gid = gid;
3522 result = winbindd_request(WINBINDD_GID_TO_SID,
3523 &sidrequest, &sidresponse);
3525 if (result != NSS_STATUS_SUCCESS) {
3526 DEBUG(1, ("winbind could not find SID of gid %d\n",
3527 gid));
3528 return False;
3531 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
3533 string_to_sid(&sid, sidresponse.data.sid.sid);
3534 add_sid_to_token(token, &sid);
3537 SAFE_FREE(response.extra_data);
3539 return True;
3543 * Get a list of all user tokens we want to look at
3545 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
3547 struct winbindd_request request;
3548 struct winbindd_response response;
3549 const char *extra_data;
3550 fstring name;
3551 int i;
3552 struct user_token *result;
3554 if (lp_winbind_use_default_domain() &&
3555 (opt_target_workgroup == NULL)) {
3556 d_printf("winbind use default domain = yes set, please "
3557 "specify a workgroup\n");
3558 return False;
3561 /* Send request to winbind daemon */
3563 ZERO_STRUCT(request);
3564 ZERO_STRUCT(response);
3566 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
3567 NSS_STATUS_SUCCESS)
3568 return False;
3570 /* Look through extra data */
3572 if (!response.extra_data)
3573 return False;
3575 extra_data = (const char *)response.extra_data;
3576 *num_tokens = 0;
3578 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3579 *num_tokens += 1;
3582 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
3584 if (result == NULL) {
3585 DEBUG(1, ("Could not malloc sid array\n"));
3586 return False;
3589 extra_data = (const char *)response.extra_data;
3590 i=0;
3592 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3594 fstring domain, user;
3595 char *p;
3597 fstrcpy(result[i].name, name);
3599 p = strchr(name, *lp_winbind_separator());
3601 DEBUG(3, ("%s\n", name));
3603 if (p == NULL) {
3604 fstrcpy(domain, opt_target_workgroup);
3605 fstrcpy(user, name);
3606 } else {
3607 *p++ = '\0';
3608 fstrcpy(domain, name);
3609 strupper_m(domain);
3610 fstrcpy(user, p);
3613 get_user_sids(domain, user, &(result[i].token));
3614 i+=1;
3617 SAFE_FREE(response.extra_data);
3619 *user_tokens = result;
3621 return True;
3624 static BOOL get_user_tokens_from_file(FILE *f,
3625 int *num_tokens,
3626 struct user_token **tokens)
3628 struct user_token *token = NULL;
3630 while (!feof(f)) {
3631 fstring line;
3633 if (fgets(line, sizeof(line)-1, f) == NULL) {
3634 return True;
3637 if (line[strlen(line)-1] == '\n')
3638 line[strlen(line)-1] = '\0';
3640 if (line[0] == ' ') {
3641 /* We have a SID */
3643 DOM_SID sid;
3644 string_to_sid(&sid, &line[1]);
3646 if (token == NULL) {
3647 DEBUG(0, ("File does not begin with username"));
3648 return False;
3651 add_sid_to_token(&token->token, &sid);
3652 continue;
3655 /* And a new user... */
3657 *num_tokens += 1;
3658 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
3659 if (*tokens == NULL) {
3660 DEBUG(0, ("Could not realloc tokens\n"));
3661 return False;
3664 token = &((*tokens)[*num_tokens-1]);
3666 fstrcpy(token->name, line);
3667 token->token.num_sids = 0;
3668 token->token.user_sids = NULL;
3669 continue;
3672 return False;
3677 * Show the list of all users that have access to a share
3680 static void show_userlist(struct cli_state *cli,
3681 TALLOC_CTX *mem_ctx, const char *netname,
3682 int num_tokens, struct user_token *tokens)
3684 int fnum;
3685 SEC_DESC *share_sd = NULL;
3686 SEC_DESC *root_sd = NULL;
3687 int i;
3688 SRV_SHARE_INFO info;
3689 WERROR result;
3690 uint16 cnum;
3692 result = cli_srvsvc_net_share_get_info(cli, mem_ctx, netname,
3693 502, &info);
3695 if (!W_ERROR_IS_OK(result)) {
3696 DEBUG(1, ("Coult not query secdesc for share %s\n",
3697 netname));
3698 return;
3701 share_sd = info.share.info502.info_502_str.sd;
3702 if (share_sd == NULL) {
3703 DEBUG(1, ("Got no secdesc for share %s\n",
3704 netname));
3707 cnum = cli->cnum;
3709 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3710 return;
3713 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
3715 if (fnum != -1) {
3716 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
3719 for (i=0; i<num_tokens; i++) {
3720 uint32 acc_granted;
3721 NTSTATUS status;
3723 if (share_sd != NULL) {
3724 if (!se_access_check(share_sd, &tokens[i].token,
3725 1, &acc_granted, &status)) {
3726 DEBUG(1, ("Could not check share_sd for "
3727 "user %s\n",
3728 tokens[i].name));
3729 continue;
3732 if (!NT_STATUS_IS_OK(status))
3733 continue;
3736 if (root_sd == NULL) {
3737 d_printf(" %s\n", tokens[i].name);
3738 continue;
3741 if (!se_access_check(root_sd, &tokens[i].token,
3742 1, &acc_granted, &status)) {
3743 DEBUG(1, ("Could not check root_sd for user %s\n",
3744 tokens[i].name));
3745 continue;
3748 if (!NT_STATUS_IS_OK(status))
3749 continue;
3751 d_printf(" %s\n", tokens[i].name);
3754 if (fnum != -1)
3755 cli_close(cli, fnum);
3756 cli_tdis(cli);
3757 cli->cnum = cnum;
3759 return;
3762 struct share_list {
3763 int num_shares;
3764 char **shares;
3767 static void collect_share(const char *name, uint32 m,
3768 const char *comment, void *state)
3770 struct share_list *share_list = (struct share_list *)state;
3772 if (m != STYPE_DISKTREE)
3773 return;
3775 share_list->num_shares += 1;
3776 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
3777 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
3780 static void rpc_share_userlist_usage(void)
3782 return;
3785 /**
3786 * List shares on a remote RPC server, including the security descriptors
3788 * All parameters are provided by the run_rpc_command function, except for
3789 * argc, argv which are passes through.
3791 * @param domain_sid The domain sid acquired from the remote server
3792 * @param cli A cli_state connected to the server.
3793 * @param mem_ctx Talloc context, destoyed on completion of the function.
3794 * @param argc Standard main() style argc
3795 * @param argv Standard main() style argv. Initial components are already
3796 * stripped
3798 * @return Normal NTSTATUS return.
3801 static NTSTATUS
3802 rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
3803 const char *domain_name,
3804 struct cli_state *cli,
3805 TALLOC_CTX *mem_ctx,
3806 int argc, const char **argv)
3808 int ret;
3809 BOOL r;
3810 ENUM_HND hnd;
3811 uint32 i;
3812 FILE *f;
3814 struct user_token *tokens = NULL;
3815 int num_tokens = 0;
3817 struct share_list share_list;
3819 if (argc > 1) {
3820 rpc_share_userlist_usage();
3821 return NT_STATUS_UNSUCCESSFUL;
3824 if (argc == 0) {
3825 f = stdin;
3826 } else {
3827 f = fopen(argv[0], "r");
3830 if (f == NULL) {
3831 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
3832 return NT_STATUS_UNSUCCESSFUL;
3835 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
3837 if (f != stdin)
3838 fclose(f);
3840 if (!r) {
3841 DEBUG(0, ("Could not read users from file\n"));
3842 return NT_STATUS_UNSUCCESSFUL;
3845 for (i=0; i<num_tokens; i++)
3846 collect_alias_memberships(&tokens[i].token);
3848 init_enum_hnd(&hnd, 0);
3850 share_list.num_shares = 0;
3851 share_list.shares = NULL;
3853 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
3855 if (ret == -1) {
3856 DEBUG(0, ("Error returning browse list: %s\n",
3857 cli_errstr(cli)));
3858 goto done;
3861 for (i = 0; i < share_list.num_shares; i++) {
3862 char *netname = share_list.shares[i];
3864 if (netname[strlen(netname)-1] == '$')
3865 continue;
3867 d_printf("%s\n", netname);
3869 show_userlist(cli, mem_ctx, netname,
3870 num_tokens, tokens);
3872 done:
3873 for (i=0; i<num_tokens; i++) {
3874 free_user_token(&tokens[i].token);
3876 SAFE_FREE(tokens);
3877 SAFE_FREE(share_list.shares);
3879 return NT_STATUS_OK;
3882 static int
3883 rpc_share_allowedusers(int argc, const char **argv)
3885 int result;
3887 result = run_rpc_command(NULL, PI_SAMR, 0,
3888 rpc_aliaslist_internals,
3889 argc, argv);
3890 if (result != 0)
3891 return result;
3893 result = run_rpc_command(NULL, PI_LSARPC, 0,
3894 rpc_aliaslist_dump,
3895 argc, argv);
3896 if (result != 0)
3897 return result;
3899 return run_rpc_command(NULL, PI_SRVSVC, 0,
3900 rpc_share_allowedusers_internals,
3901 argc, argv);
3904 int net_usersidlist(int argc, const char **argv)
3906 int num_tokens = 0;
3907 struct user_token *tokens = NULL;
3908 int i;
3910 if (argc != 0) {
3911 net_usersidlist_usage(argc, argv);
3912 return 0;
3915 if (!get_user_tokens(&num_tokens, &tokens)) {
3916 DEBUG(0, ("Could not get the user/sid list\n"));
3917 return 0;
3920 for (i=0; i<num_tokens; i++) {
3921 dump_user_token(&tokens[i]);
3922 free_user_token(&tokens[i].token);
3925 SAFE_FREE(tokens);
3926 return 1;
3929 int net_usersidlist_usage(int argc, const char **argv)
3931 d_printf("net usersidlist\n"
3932 "\tprints out a list of all users the running winbind knows\n"
3933 "\tabout, together with all their SIDs. This is used as\n"
3934 "\tinput to the 'net rpc share allowedusers' command.\n\n");
3936 net_common_flags_usage(argc, argv);
3937 return -1;
3940 /**
3941 * 'net rpc share' entrypoint.
3942 * @param argc Standard main() style argc
3943 * @param argv Standard main() style argv. Initial components are already
3944 * stripped
3947 int net_rpc_share(int argc, const char **argv)
3949 struct functable func[] = {
3950 {"add", rpc_share_add},
3951 {"delete", rpc_share_delete},
3952 {"allowedusers", rpc_share_allowedusers},
3953 {"migrate", rpc_share_migrate},
3954 {NULL, NULL}
3957 if (argc == 0)
3958 return run_rpc_command(NULL, PI_SRVSVC, 0,
3959 rpc_share_list_internals,
3960 argc, argv);
3962 return net_run_function(argc, argv, func, rpc_share_usage);
3965 /****************************************************************************/
3967 static int rpc_file_usage(int argc, const char **argv)
3969 return net_help_file(argc, argv);
3972 /**
3973 * Close a file on a remote RPC server
3975 * All parameters are provided by the run_rpc_command function, except for
3976 * argc, argv which are passes through.
3978 * @param domain_sid The domain sid acquired from the remote server
3979 * @param cli A cli_state connected to the server.
3980 * @param mem_ctx Talloc context, destoyed on completion of the function.
3981 * @param argc Standard main() style argc
3982 * @param argv Standard main() style argv. Initial components are already
3983 * stripped
3985 * @return Normal NTSTATUS return.
3987 static NTSTATUS
3988 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
3989 struct cli_state *cli,
3990 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3992 WERROR result;
3993 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
3994 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3997 /**
3998 * Close a file on a remote RPC server
4000 * @param argc Standard main() style argc
4001 * @param argv Standard main() style argv. Initial components are already
4002 * stripped
4004 * @return A shell status integer (0 for success)
4006 static int rpc_file_close(int argc, const char **argv)
4008 if (argc < 1) {
4009 DEBUG(1, ("No fileid given on close\n"));
4010 return(rpc_file_usage(argc, argv));
4013 return run_rpc_command(NULL, PI_SRVSVC, 0,
4014 rpc_file_close_internals,
4015 argc, argv);
4018 /**
4019 * Formatted print of open file info
4021 * @param info3 FILE_INFO_3 contents
4022 * @param str3 strings for FILE_INFO_3
4025 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
4027 fstring user = "", path = "";
4029 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
4030 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
4032 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4033 info3->id, user, info3->perms, info3->num_locks, path);
4036 /**
4037 * List open files on a remote RPC server
4039 * All parameters are provided by the run_rpc_command function, except for
4040 * argc, argv which are passes through.
4042 * @param domain_sid The domain sid acquired from the remote server
4043 * @param cli A cli_state connected to the server.
4044 * @param mem_ctx Talloc context, destoyed on completion of the function.
4045 * @param argc Standard main() style argc
4046 * @param argv Standard main() style argv. Initial components are already
4047 * stripped
4049 * @return Normal NTSTATUS return.
4052 static NTSTATUS
4053 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
4054 struct cli_state *cli,
4055 TALLOC_CTX *mem_ctx, int argc, const char **argv)
4057 SRV_FILE_INFO_CTR ctr;
4058 WERROR result;
4059 ENUM_HND hnd;
4060 uint32 preferred_len = 0xffffffff, i;
4061 const char *username=NULL;
4063 init_enum_hnd(&hnd, 0);
4065 /* if argc > 0, must be user command */
4066 if (argc > 0)
4067 username = smb_xstrdup(argv[0]);
4069 result = cli_srvsvc_net_file_enum(
4070 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4072 if (!W_ERROR_IS_OK(result))
4073 goto done;
4075 /* Display results */
4077 d_printf(
4078 "\nEnumerating open files on remote server:\n\n"\
4079 "\nFileId Opened by Perms Locks Path"\
4080 "\n------ --------- ----- ----- ---- \n");
4081 for (i = 0; i < ctr.num_entries; i++)
4082 display_file_info_3(&ctr.file.info3[i].info_3,
4083 &ctr.file.info3[i].info_3_str);
4084 done:
4085 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4089 /**
4090 * List files for a user on a remote RPC server
4092 * @param argc Standard main() style argc
4093 * @param argv Standard main() style argv. Initial components are already
4094 * stripped
4096 * @return A shell status integer (0 for success)
4098 static int rpc_file_user(int argc, const char **argv)
4100 if (argc < 1) {
4101 DEBUG(1, ("No username given\n"));
4102 return(rpc_file_usage(argc, argv));
4105 return run_rpc_command(NULL, PI_SRVSVC, 0,
4106 rpc_file_list_internals,
4107 argc, argv);
4111 /**
4112 * 'net rpc file' entrypoint.
4113 * @param argc Standard main() style argc
4114 * @param argv Standard main() style argv. Initial components are already
4115 * stripped
4118 int net_rpc_file(int argc, const char **argv)
4120 struct functable func[] = {
4121 {"close", rpc_file_close},
4122 {"user", rpc_file_user},
4123 #if 0
4124 {"info", rpc_file_info},
4125 #endif
4126 {NULL, NULL}
4129 if (argc == 0)
4130 return run_rpc_command(NULL, PI_SRVSVC, 0,
4131 rpc_file_list_internals,
4132 argc, argv);
4134 return net_run_function(argc, argv, func, rpc_file_usage);
4137 /****************************************************************************/
4141 /**
4142 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4144 * All parameters are provided by the run_rpc_command function, except for
4145 * argc, argv which are passed through.
4147 * @param domain_sid The domain sid aquired from the remote server
4148 * @param cli A cli_state connected to the server.
4149 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4150 * @param argc Standard main() style argc
4151 * @param argv Standard main() style argv. Initial components are already
4152 * stripped
4154 * @return Normal NTSTATUS return.
4157 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4158 const char *domain_name,
4159 struct cli_state *cli,
4160 TALLOC_CTX *mem_ctx,
4161 int argc, const char **argv)
4163 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4165 result = cli_shutdown_abort(cli, mem_ctx);
4167 if (NT_STATUS_IS_OK(result)) {
4168 d_printf("\nShutdown successfully aborted\n");
4169 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4170 } else
4171 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4173 return result;
4177 /**
4178 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4180 * All parameters are provided by the run_rpc_command function, except for
4181 * argc, argv which are passed through.
4183 * @param domain_sid The domain sid aquired from the remote server
4184 * @param cli A cli_state connected to the server.
4185 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4186 * @param argc Standard main() style argc
4187 * @param argv Standard main() style argv. Initial components are already
4188 * stripped
4190 * @return Normal NTSTATUS return.
4193 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
4194 const char *domain_name,
4195 struct cli_state *cli,
4196 TALLOC_CTX *mem_ctx,
4197 int argc, const char **argv)
4199 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4201 result = werror_to_ntstatus(cli_reg_abort_shutdown(cli, mem_ctx));
4203 if (NT_STATUS_IS_OK(result)) {
4204 d_printf("\nShutdown successfully aborted\n");
4205 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4206 } else
4207 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4209 return result;
4212 /**
4213 * ABORT the Shut down of a remote RPC server
4215 * @param argc Standard main() style argc
4216 * @param argv Standard main() style argv. Initial components are already
4217 * stripped
4219 * @return A shell status integer (0 for success)
4222 static int rpc_shutdown_abort(int argc, const char **argv)
4224 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4225 rpc_shutdown_abort_internals,
4226 argc, argv);
4228 if (rc == 0)
4229 return rc;
4231 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4233 return run_rpc_command(NULL, PI_WINREG, 0,
4234 rpc_reg_shutdown_abort_internals,
4235 argc, argv);
4238 /**
4239 * Shut down a remote RPC Server via initshutdown pipe
4241 * All parameters are provided by the run_rpc_command function, except for
4242 * argc, argv which are passes through.
4244 * @param domain_sid The domain sid aquired from the remote server
4245 * @param cli A cli_state connected to the server.
4246 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4247 * @param argc Standard main() style argc
4248 * @param argc Standard main() style argv. Initial components are already
4249 * stripped
4251 * @return Normal NTSTATUS return.
4254 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
4255 const char *domain_name,
4256 struct cli_state *cli,
4257 TALLOC_CTX *mem_ctx,
4258 int argc, const char **argv)
4260 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4261 const char *msg = "This machine will be shutdown shortly";
4262 uint32 timeout = 20;
4264 if (opt_comment) {
4265 msg = opt_comment;
4267 if (opt_timeout) {
4268 timeout = opt_timeout;
4271 /* create an entry */
4272 result = cli_shutdown_init(cli, mem_ctx, msg, timeout, opt_reboot,
4273 opt_force);
4275 if (NT_STATUS_IS_OK(result)) {
4276 d_printf("\nShutdown of remote machine succeeded\n");
4277 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4278 } else
4279 DEBUG(0,("Shutdown of remote machine failed!\n"));
4281 return result;
4284 /**
4285 * Shut down a remote RPC Server via winreg pipe
4287 * All parameters are provided by the run_rpc_command function, except for
4288 * argc, argv which are passes through.
4290 * @param domain_sid The domain sid aquired from the remote server
4291 * @param cli A cli_state connected to the server.
4292 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4293 * @param argc Standard main() style argc
4294 * @param argc Standard main() style argv. Initial components are already
4295 * stripped
4297 * @return Normal NTSTATUS return.
4300 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
4301 const char *domain_name,
4302 struct cli_state *cli,
4303 TALLOC_CTX *mem_ctx,
4304 int argc, const char **argv)
4306 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4307 const char *msg = "This machine will be shutdown shortly";
4308 uint32 timeout = 20;
4309 #if 0
4310 poptContext pc;
4311 int rc;
4313 struct poptOption long_options[] = {
4314 {"message", 'm', POPT_ARG_STRING, &msg},
4315 {"timeout", 't', POPT_ARG_INT, &timeout},
4316 {"reboot", 'r', POPT_ARG_NONE, &reboot},
4317 {"force", 'f', POPT_ARG_NONE, &force},
4318 { 0, 0, 0, 0}
4321 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
4322 POPT_CONTEXT_KEEP_FIRST);
4324 rc = poptGetNextOpt(pc);
4326 if (rc < -1) {
4327 /* an error occurred during option processing */
4328 DEBUG(0, ("%s: %s\n",
4329 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
4330 poptStrerror(rc)));
4331 return NT_STATUS_INVALID_PARAMETER;
4333 #endif
4334 if (opt_comment) {
4335 msg = opt_comment;
4337 if (opt_timeout) {
4338 timeout = opt_timeout;
4341 /* create an entry */
4342 result = werror_to_ntstatus(cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force));
4344 if (NT_STATUS_IS_OK(result)) {
4345 d_printf("\nShutdown of remote machine succeeded\n");
4346 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4348 else
4349 DEBUG(0,("Shutdown of remote machine failed!\n"));
4351 return result;
4354 /**
4355 * Shut down a remote RPC server
4357 * @param argc Standard main() style argc
4358 * @param argc Standard main() style argv. Initial components are already
4359 * stripped
4361 * @return A shell status integer (0 for success)
4364 static int rpc_shutdown(int argc, const char **argv)
4366 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4367 rpc_init_shutdown_internals,
4368 argc, argv);
4369 if (rc == 0)
4370 return rc;
4372 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4374 return run_rpc_command(NULL, PI_WINREG, 0, rpc_reg_shutdown_internals,
4375 argc, argv);
4378 /***************************************************************************
4379 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
4381 ***************************************************************************/
4384 * Add interdomain trust account to the RPC server.
4385 * All parameters (except for argc and argv) are passed by run_rpc_command
4386 * function.
4388 * @param domain_sid The domain sid acquired from the server
4389 * @param cli A cli_state connected to the server.
4390 * @param mem_ctx Talloc context, destoyed on completion of the function.
4391 * @param argc Standard main() style argc
4392 * @param argc Standard main() style argv. Initial components are already
4393 * stripped
4395 * @return normal NTSTATUS return code
4398 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
4399 const char *domain_name,
4400 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4401 int argc, const char **argv) {
4403 POLICY_HND connect_pol, domain_pol, user_pol;
4404 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4405 char *acct_name;
4406 uint16 acb_info;
4407 uint32 unknown, user_rid;
4409 if (argc != 2) {
4410 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
4411 return NT_STATUS_INVALID_PARAMETER;
4415 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4418 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
4419 return NT_STATUS_NO_MEMORY;
4422 strupper_m(acct_name);
4424 /* Get samr policy handle */
4425 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4426 &connect_pol);
4427 if (!NT_STATUS_IS_OK(result)) {
4428 goto done;
4431 /* Get domain policy handle */
4432 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4433 MAXIMUM_ALLOWED_ACCESS,
4434 domain_sid, &domain_pol);
4435 if (!NT_STATUS_IS_OK(result)) {
4436 goto done;
4439 /* Create trusting domain's account */
4440 acb_info = ACB_NORMAL;
4441 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
4442 mimir: yes, most probably it is */
4444 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
4445 acct_name, acb_info, unknown,
4446 &user_pol, &user_rid);
4447 if (!NT_STATUS_IS_OK(result)) {
4448 goto done;
4452 SAM_USERINFO_CTR ctr;
4453 SAM_USER_INFO_23 p23;
4454 NTTIME notime;
4455 char nostr[] = "";
4456 LOGON_HRS hrs;
4457 uchar pwbuf[516];
4459 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
4461 ZERO_STRUCT(ctr);
4462 ZERO_STRUCT(p23);
4463 ZERO_STRUCT(notime);
4464 hrs.max_len = 1260;
4465 hrs.offset = 0;
4466 hrs.len = 21;
4467 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
4468 acb_info = ACB_DOMTRUST;
4470 init_sam_user_info23A(&p23, &notime, &notime, &notime,
4471 &notime, &notime, &notime,
4472 nostr, nostr, nostr, nostr, nostr,
4473 nostr, nostr, nostr, nostr, nostr,
4474 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
4475 0, 0, (char *)pwbuf);
4476 ctr.switch_value = 23;
4477 ctr.info.id23 = &p23;
4478 p23.passmustchange = 0;
4480 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 23,
4481 &cli->user_session_key, &ctr);
4483 if (!NT_STATUS_IS_OK(result)) {
4484 DEBUG(0,("Could not set trust account password: %s\n",
4485 nt_errstr(result)));
4486 goto done;
4490 done:
4491 SAFE_FREE(acct_name);
4492 return result;
4496 * Create interdomain trust account for a remote domain.
4498 * @param argc standard argc
4499 * @param argv standard argv without initial components
4501 * @return Integer status (0 means success)
4504 static int rpc_trustdom_add(int argc, const char **argv)
4506 if (argc > 0) {
4507 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
4508 argc, argv);
4509 } else {
4510 d_printf("Usage: net rpc trustdom add <domain>\n");
4511 return -1;
4517 * Remove interdomain trust account from the RPC server.
4518 * All parameters (except for argc and argv) are passed by run_rpc_command
4519 * function.
4521 * @param domain_sid The domain sid acquired from the server
4522 * @param cli A cli_state connected to the server.
4523 * @param mem_ctx Talloc context, destoyed on completion of the function.
4524 * @param argc Standard main() style argc
4525 * @param argc Standard main() style argv. Initial components are already
4526 * stripped
4528 * @return normal NTSTATUS return code
4531 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
4532 const char *domain_name,
4533 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4534 int argc, const char **argv) {
4536 POLICY_HND connect_pol, domain_pol, user_pol;
4537 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4538 char *acct_name;
4539 const char **names;
4540 DOM_SID trust_acct_sid;
4541 uint32 *user_rids, num_rids, *name_types;
4542 uint32 flags = 0x000003e8; /* Unknown */
4544 if (argc != 1) {
4545 d_printf("Usage: net rpc trustdom del <domain_name>\n");
4546 return NT_STATUS_INVALID_PARAMETER;
4550 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4552 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
4554 if (acct_name == NULL)
4555 return NT_STATUS_NO_MEMORY;
4557 strupper_m(acct_name);
4559 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
4560 names[0] = acct_name;
4563 /* Get samr policy handle */
4564 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4565 &connect_pol);
4566 if (!NT_STATUS_IS_OK(result)) {
4567 goto done;
4570 /* Get domain policy handle */
4571 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4572 MAXIMUM_ALLOWED_ACCESS,
4573 domain_sid, &domain_pol);
4574 if (!NT_STATUS_IS_OK(result)) {
4575 goto done;
4578 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, flags, 1,
4579 names, &num_rids,
4580 &user_rids, &name_types);
4582 if (!NT_STATUS_IS_OK(result)) {
4583 goto done;
4586 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
4587 MAXIMUM_ALLOWED_ACCESS,
4588 user_rids[0], &user_pol);
4590 if (!NT_STATUS_IS_OK(result)) {
4591 goto done;
4594 /* append the rid to the domain sid */
4595 sid_copy(&trust_acct_sid, domain_sid);
4596 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
4597 goto done;
4600 /* remove the sid */
4602 result = cli_samr_remove_sid_foreign_domain(cli, mem_ctx, &user_pol,
4603 &trust_acct_sid);
4605 if (!NT_STATUS_IS_OK(result)) {
4606 goto done;
4609 /* Delete user */
4611 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
4613 if (!NT_STATUS_IS_OK(result)) {
4614 goto done;
4617 if (!NT_STATUS_IS_OK(result)) {
4618 DEBUG(0,("Could not set trust account password: %s\n",
4619 nt_errstr(result)));
4620 goto done;
4623 done:
4624 return result;
4628 * Delete interdomain trust account for a remote domain.
4630 * @param argc standard argc
4631 * @param argv standard argv without initial components
4633 * @return Integer status (0 means success)
4636 static int rpc_trustdom_del(int argc, const char **argv)
4638 if (argc > 0) {
4639 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
4640 argc, argv);
4641 } else {
4642 d_printf("Usage: net rpc trustdom del <domain>\n");
4643 return -1;
4649 * Establish trust relationship to a trusting domain.
4650 * Interdomain account must already be created on remote PDC.
4652 * @param argc standard argc
4653 * @param argv standard argv without initial components
4655 * @return Integer status (0 means success)
4658 static int rpc_trustdom_establish(int argc, const char **argv)
4660 struct cli_state *cli;
4661 struct in_addr server_ip;
4662 POLICY_HND connect_hnd;
4663 TALLOC_CTX *mem_ctx;
4664 NTSTATUS nt_status;
4665 DOM_SID *domain_sid;
4666 smb_ucs2_t *uni_domain_name;
4668 char* domain_name;
4669 char* domain_name_pol;
4670 char* acct_name;
4671 fstring pdc_name;
4674 * Connect to \\server\ipc$ as 'our domain' account with password
4677 if (argc != 1) {
4678 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
4679 return -1;
4682 domain_name = smb_xstrdup(argv[0]);
4683 strupper_m(domain_name);
4685 /* account name used at first is our domain's name with '$' */
4686 asprintf(&acct_name, "%s$", lp_workgroup());
4687 strupper_m(acct_name);
4690 * opt_workgroup will be used by connection functions further,
4691 * hence it should be set to remote domain name instead of ours
4693 if (opt_workgroup) {
4694 opt_workgroup = smb_xstrdup(domain_name);
4697 opt_user_name = acct_name;
4699 /* find the domain controller */
4700 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
4701 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
4702 return -1;
4705 /* connect to ipc$ as username/password */
4706 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
4707 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
4709 /* Is it trusting domain account for sure ? */
4710 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
4711 nt_errstr(nt_status)));
4712 return -1;
4716 * Connect to \\server\ipc$ again (this time anonymously)
4719 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
4721 if (NT_STATUS_IS_ERR(nt_status)) {
4722 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
4723 domain_name, nt_errstr(nt_status)));
4727 * Use NetServerEnum2 to make sure we're talking to a proper server
4730 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
4731 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
4732 for domain %s\n", domain_name));
4735 if (!(mem_ctx = talloc_init("establishing trust relationship to "
4736 "domain %s", domain_name))) {
4737 DEBUG(0, ("talloc_init() failed\n"));
4738 cli_shutdown(cli);
4739 return -1;
4743 * Call LsaOpenPolicy and LsaQueryInfo
4746 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4747 DEBUG(0, ("Could not initialise lsa pipe\n"));
4748 cli_shutdown(cli);
4749 return -1;
4752 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
4753 &connect_hnd);
4754 if (NT_STATUS_IS_ERR(nt_status)) {
4755 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4756 nt_errstr(nt_status)));
4757 return -1;
4760 /* Querying info level 5 */
4762 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
4763 5 /* info level */,
4764 &domain_name_pol, &domain_sid);
4765 if (NT_STATUS_IS_ERR(nt_status)) {
4766 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4767 nt_errstr(nt_status)));
4768 return -1;
4771 if (push_ucs2_talloc(mem_ctx, &uni_domain_name, domain_name_pol) < 0) {
4772 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4773 domain_name_pol));
4774 return -1;
4777 /* There should be actually query info level 3 (following nt serv behaviour),
4778 but I still don't know if it's _really_ necessary */
4781 * Store the password in secrets db
4784 if (!secrets_store_trusted_domain_password(domain_name,
4785 uni_domain_name,
4786 strlen_w(uni_domain_name)+1,
4787 opt_password,
4788 *domain_sid)) {
4789 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4790 return -1;
4794 * Close the pipes and clean up
4797 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
4798 if (NT_STATUS_IS_ERR(nt_status)) {
4799 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
4800 nt_errstr(nt_status)));
4801 return -1;
4804 if (cli->pipes[cli->pipe_idx].fnum)
4805 cli_nt_session_close(cli);
4807 cli_shutdown(cli);
4809 talloc_destroy(mem_ctx);
4811 d_printf("Trust to domain %s established\n", domain_name);
4812 return 0;
4816 * Revoke trust relationship to the remote domain
4818 * @param argc standard argc
4819 * @param argv standard argv without initial components
4821 * @return Integer status (0 means success)
4824 static int rpc_trustdom_revoke(int argc, const char **argv)
4826 char* domain_name;
4828 if (argc < 1) return -1;
4830 /* generate upper cased domain name */
4831 domain_name = smb_xstrdup(argv[0]);
4832 strupper_m(domain_name);
4834 /* delete password of the trust */
4835 if (!trusted_domain_password_delete(domain_name)) {
4836 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
4837 domain_name));
4838 return -1;
4841 return 0;
4845 * Usage for 'net rpc trustdom' command
4847 * @param argc standard argc
4848 * @param argv standard argv without inital components
4850 * @return Integer status returned to shell
4853 static int rpc_trustdom_usage(int argc, const char **argv)
4855 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
4856 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
4857 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
4858 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
4859 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
4860 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
4861 return -1;
4865 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
4866 const char *domain_name,
4867 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4868 int argc, const char **argv)
4870 fstring str_sid;
4871 sid_to_string(str_sid, domain_sid);
4872 d_printf("%s\n", str_sid);
4873 return NT_STATUS_OK;
4876 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
4878 fstring ascii_sid, padding;
4879 int pad_len, col_len = 20;
4881 /* convert sid into ascii string */
4882 sid_to_string(ascii_sid, dom_sid);
4884 /* calculate padding space for d_printf to look nicer */
4885 pad_len = col_len - strlen(trusted_dom_name);
4886 padding[pad_len] = 0;
4887 do padding[--pad_len] = ' '; while (pad_len);
4889 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
4892 static NTSTATUS vampire_trusted_domain(struct cli_state *cli,
4893 TALLOC_CTX *mem_ctx,
4894 POLICY_HND *pol,
4895 DOM_SID dom_sid,
4896 const char *trusted_dom_name)
4898 NTSTATUS nt_status;
4899 LSA_TRUSTED_DOMAIN_INFO *info;
4900 char *cleartextpwd;
4901 DATA_BLOB data;
4902 smb_ucs2_t *uni_dom_name;
4904 nt_status = cli_lsa_query_trusted_domain_info_by_sid(cli, mem_ctx, pol, 4, &dom_sid, &info);
4906 if (NT_STATUS_IS_ERR(nt_status)) {
4907 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
4908 nt_errstr(nt_status)));
4909 goto done;
4912 data = data_blob(NULL, info->password.password.length);
4914 memcpy(data.data, info->password.password.data, info->password.password.length);
4915 data.length = info->password.password.length;
4917 cleartextpwd = decrypt_trustdom_secret(cli->pwd.password, &data);
4919 if (cleartextpwd == NULL) {
4920 DEBUG(0,("retrieved NULL password\n"));
4921 nt_status = NT_STATUS_UNSUCCESSFUL;
4922 goto done;
4925 if (push_ucs2_talloc(mem_ctx, &uni_dom_name, trusted_dom_name) < 0) {
4926 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4927 trusted_dom_name));
4928 nt_status = NT_STATUS_UNSUCCESSFUL;
4929 goto done;
4932 if (!secrets_store_trusted_domain_password(trusted_dom_name,
4933 uni_dom_name,
4934 strlen_w(uni_dom_name)+1,
4935 cleartextpwd,
4936 dom_sid)) {
4937 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4938 nt_status = NT_STATUS_UNSUCCESSFUL;
4939 goto done;
4942 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
4943 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
4945 done:
4946 SAFE_FREE(cleartextpwd);
4947 data_blob_free(&data);
4949 return nt_status;
4952 static int rpc_trustdom_vampire(int argc, const char **argv)
4954 /* common variables */
4955 TALLOC_CTX* mem_ctx;
4956 struct cli_state *cli;
4957 NTSTATUS nt_status;
4958 const char *domain_name = NULL;
4959 DOM_SID *queried_dom_sid;
4960 POLICY_HND connect_hnd;
4962 /* trusted domains listing variables */
4963 unsigned int num_domains, enum_ctx = 0;
4964 int i;
4965 DOM_SID *domain_sids;
4966 char **trusted_dom_names;
4967 fstring pdc_name;
4968 char *dummy;
4971 * Listing trusted domains (stored in secrets.tdb, if local)
4974 mem_ctx = talloc_init("trust relationships vampire");
4977 * set domain and pdc name to local samba server (default)
4978 * or to remote one given in command line
4981 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
4982 domain_name = opt_workgroup;
4983 opt_target_workgroup = opt_workgroup;
4984 } else {
4985 fstrcpy(pdc_name, global_myname());
4986 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
4987 opt_target_workgroup = domain_name;
4990 /* open \PIPE\lsarpc and open policy handle */
4991 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
4992 DEBUG(0, ("Couldn't connect to domain controller\n"));
4993 return -1;
4996 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4997 DEBUG(0, ("Could not initialise lsa pipe\n"));
4998 return -1;
5001 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5002 &connect_hnd);
5003 if (NT_STATUS_IS_ERR(nt_status)) {
5004 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5005 nt_errstr(nt_status)));
5006 return -1;
5009 /* query info level 5 to obtain sid of a domain being queried */
5010 nt_status = cli_lsa_query_info_policy(
5011 cli, mem_ctx, &connect_hnd, 5 /* info level */,
5012 &dummy, &queried_dom_sid);
5014 if (NT_STATUS_IS_ERR(nt_status)) {
5015 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5016 nt_errstr(nt_status)));
5017 return -1;
5021 * Keep calling LsaEnumTrustdom over opened pipe until
5022 * the end of enumeration is reached
5025 d_printf("Vampire trusted domains:\n\n");
5027 do {
5028 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
5029 &num_domains,
5030 &trusted_dom_names, &domain_sids);
5032 if (NT_STATUS_IS_ERR(nt_status)) {
5033 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5034 nt_errstr(nt_status)));
5035 return -1;
5038 for (i = 0; i < num_domains; i++) {
5040 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5042 nt_status = vampire_trusted_domain(cli, mem_ctx, &connect_hnd,
5043 domain_sids[i], trusted_dom_names[i]);
5044 if (!NT_STATUS_IS_OK(nt_status))
5045 return -1;
5049 * in case of no trusted domains say something rather
5050 * than just display blank line
5052 if (!num_domains) d_printf("none\n");
5054 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5056 /* close this connection before doing next one */
5057 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
5058 if (NT_STATUS_IS_ERR(nt_status)) {
5059 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5060 nt_errstr(nt_status)));
5061 return -1;
5064 /* close lsarpc pipe and connection to IPC$ */
5065 cli_nt_session_close(cli);
5066 cli_shutdown(cli);
5068 talloc_destroy(mem_ctx);
5069 return 0;
5072 static int rpc_trustdom_list(int argc, const char **argv)
5074 /* common variables */
5075 TALLOC_CTX* mem_ctx;
5076 struct cli_state *cli, *remote_cli;
5077 NTSTATUS nt_status;
5078 const char *domain_name = NULL;
5079 DOM_SID *queried_dom_sid;
5080 fstring padding;
5081 int ascii_dom_name_len;
5082 POLICY_HND connect_hnd;
5084 /* trusted domains listing variables */
5085 unsigned int num_domains, enum_ctx = 0;
5086 int i, pad_len, col_len = 20;
5087 DOM_SID *domain_sids;
5088 char **trusted_dom_names;
5089 fstring pdc_name;
5090 char *dummy;
5092 /* trusting domains listing variables */
5093 POLICY_HND domain_hnd;
5094 char **trusting_dom_names;
5095 uint32 *trusting_dom_rids;
5098 * Listing trusted domains (stored in secrets.tdb, if local)
5101 mem_ctx = talloc_init("trust relationships listing");
5104 * set domain and pdc name to local samba server (default)
5105 * or to remote one given in command line
5108 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5109 domain_name = opt_workgroup;
5110 opt_target_workgroup = opt_workgroup;
5111 } else {
5112 fstrcpy(pdc_name, global_myname());
5113 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5114 opt_target_workgroup = domain_name;
5117 /* open \PIPE\lsarpc and open policy handle */
5118 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5119 DEBUG(0, ("Couldn't connect to domain controller\n"));
5120 return -1;
5123 if (!cli_nt_session_open(cli, PI_LSARPC)) {
5124 DEBUG(0, ("Could not initialise lsa pipe\n"));
5125 return -1;
5128 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5129 &connect_hnd);
5130 if (NT_STATUS_IS_ERR(nt_status)) {
5131 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5132 nt_errstr(nt_status)));
5133 return -1;
5136 /* query info level 5 to obtain sid of a domain being queried */
5137 nt_status = cli_lsa_query_info_policy(
5138 cli, mem_ctx, &connect_hnd, 5 /* info level */,
5139 &dummy, &queried_dom_sid);
5141 if (NT_STATUS_IS_ERR(nt_status)) {
5142 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5143 nt_errstr(nt_status)));
5144 return -1;
5148 * Keep calling LsaEnumTrustdom over opened pipe until
5149 * the end of enumeration is reached
5152 d_printf("Trusted domains list:\n\n");
5154 do {
5155 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
5156 &num_domains,
5157 &trusted_dom_names, &domain_sids);
5159 if (NT_STATUS_IS_ERR(nt_status)) {
5160 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5161 nt_errstr(nt_status)));
5162 return -1;
5165 for (i = 0; i < num_domains; i++) {
5166 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5170 * in case of no trusted domains say something rather
5171 * than just display blank line
5173 if (!num_domains) d_printf("none\n");
5175 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5177 /* close this connection before doing next one */
5178 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
5179 if (NT_STATUS_IS_ERR(nt_status)) {
5180 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5181 nt_errstr(nt_status)));
5182 return -1;
5185 cli_nt_session_close(cli);
5188 * Listing trusting domains (stored in passdb backend, if local)
5191 d_printf("\nTrusting domains list:\n\n");
5194 * Open \PIPE\samr and get needed policy handles
5196 if (!cli_nt_session_open(cli, PI_SAMR)) {
5197 DEBUG(0, ("Could not initialise samr pipe\n"));
5198 return -1;
5201 /* SamrConnect */
5202 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
5203 &connect_hnd);
5204 if (!NT_STATUS_IS_OK(nt_status)) {
5205 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
5206 nt_errstr(nt_status)));
5207 return -1;
5210 /* SamrOpenDomain - we have to open domain policy handle in order to be
5211 able to enumerate accounts*/
5212 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
5213 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
5214 queried_dom_sid, &domain_hnd);
5215 if (!NT_STATUS_IS_OK(nt_status)) {
5216 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
5217 nt_errstr(nt_status)));
5218 return -1;
5222 * perform actual enumeration
5225 enum_ctx = 0; /* reset enumeration context from last enumeration */
5226 do {
5228 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
5229 &enum_ctx, ACB_DOMTRUST, 0xffff,
5230 &trusting_dom_names, &trusting_dom_rids,
5231 &num_domains);
5232 if (NT_STATUS_IS_ERR(nt_status)) {
5233 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
5234 nt_errstr(nt_status)));
5235 return -1;
5238 for (i = 0; i < num_domains; i++) {
5241 * get each single domain's sid (do we _really_ need this ?):
5242 * 1) connect to domain's pdc
5243 * 2) query the pdc for domain's sid
5246 /* get rid of '$' tail */
5247 ascii_dom_name_len = strlen(trusting_dom_names[i]);
5248 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
5249 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
5251 /* calculate padding space for d_printf to look nicer */
5252 pad_len = col_len - strlen(trusting_dom_names[i]);
5253 padding[pad_len] = 0;
5254 do padding[--pad_len] = ' '; while (pad_len);
5256 /* set opt_* variables to remote domain */
5257 strupper_m(trusting_dom_names[i]);
5258 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
5259 opt_target_workgroup = opt_workgroup;
5261 d_printf("%s%s", trusting_dom_names[i], padding);
5263 /* connect to remote domain controller */
5264 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
5265 if (remote_cli) {
5266 /* query for domain's sid */
5267 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
5268 d_printf("couldn't get domain's sid\n");
5270 cli_shutdown(remote_cli);
5272 } else {
5273 d_printf("domain controller is not responding\n");
5277 if (!num_domains) d_printf("none\n");
5279 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5281 /* close opened samr and domain policy handles */
5282 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
5283 if (!NT_STATUS_IS_OK(nt_status)) {
5284 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
5287 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
5288 if (!NT_STATUS_IS_OK(nt_status)) {
5289 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
5292 /* close samr pipe and connection to IPC$ */
5293 cli_nt_session_close(cli);
5294 cli_shutdown(cli);
5296 talloc_destroy(mem_ctx);
5297 return 0;
5301 * Entrypoint for 'net rpc trustdom' code
5303 * @param argc standard argc
5304 * @param argv standard argv without initial components
5306 * @return Integer status (0 means success)
5309 static int rpc_trustdom(int argc, const char **argv)
5311 struct functable func[] = {
5312 {"add", rpc_trustdom_add},
5313 {"del", rpc_trustdom_del},
5314 {"establish", rpc_trustdom_establish},
5315 {"revoke", rpc_trustdom_revoke},
5316 {"help", rpc_trustdom_usage},
5317 {"list", rpc_trustdom_list},
5318 {"vampire", rpc_trustdom_vampire},
5319 {NULL, NULL}
5322 if (argc == 0) {
5323 rpc_trustdom_usage(argc, argv);
5324 return -1;
5327 return (net_run_function(argc, argv, func, rpc_user_usage));
5331 * Check if a server will take rpc commands
5332 * @param flags Type of server to connect to (PDC, DMB, localhost)
5333 * if the host is not explicitly specified
5334 * @return BOOL (true means rpc supported)
5336 BOOL net_rpc_check(unsigned flags)
5338 struct cli_state cli;
5339 BOOL ret = False;
5340 struct in_addr server_ip;
5341 char *server_name = NULL;
5343 /* flags (i.e. server type) may depend on command */
5344 if (!net_find_server(flags, &server_ip, &server_name))
5345 return False;
5347 ZERO_STRUCT(cli);
5348 if (cli_initialise(&cli) == False)
5349 return False;
5351 if (!cli_connect(&cli, server_name, &server_ip))
5352 goto done;
5353 if (!attempt_netbios_session_request(&cli, global_myname(),
5354 server_name, &server_ip))
5355 goto done;
5356 if (!cli_negprot(&cli))
5357 goto done;
5358 if (cli.protocol < PROTOCOL_NT1)
5359 goto done;
5361 ret = True;
5362 done:
5363 cli_shutdown(&cli);
5364 return ret;
5367 /* dump sam database via samsync rpc calls */
5368 static int rpc_samdump(int argc, const char **argv) {
5369 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
5370 argc, argv);
5373 /* syncronise sam database via samsync rpc calls */
5374 static int rpc_vampire(int argc, const char **argv) {
5375 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
5376 argc, argv);
5379 /**
5380 * Migrate everything from a print-server
5382 * @param argc Standard main() style argc
5383 * @param argv Standard main() style argv. Initial components are already
5384 * stripped
5386 * @return A shell status integer (0 for success)
5388 * The order is important !
5389 * To successfully add drivers the print-queues have to exist !
5390 * Applying ACLs should be the last step, because you're easily locked out
5393 static int rpc_printer_migrate_all(int argc, const char **argv)
5395 int ret;
5397 if (!opt_host) {
5398 printf("no server to migrate\n");
5399 return -1;
5402 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
5403 if (ret)
5404 return ret;
5406 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
5407 if (ret)
5408 return ret;
5410 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
5411 if (ret)
5412 return ret;
5414 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
5415 if (ret)
5416 return ret;
5418 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
5422 /**
5423 * Migrate print-drivers from a print-server
5425 * @param argc Standard main() style argc
5426 * @param argv Standard main() style argv. Initial components are already
5427 * stripped
5429 * @return A shell status integer (0 for success)
5431 static int rpc_printer_migrate_drivers(int argc, const char **argv)
5433 if (!opt_host) {
5434 printf("no server to migrate\n");
5435 return -1;
5438 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5439 rpc_printer_migrate_drivers_internals,
5440 argc, argv);
5443 /**
5444 * Migrate print-forms from a print-server
5446 * @param argc Standard main() style argc
5447 * @param argv Standard main() style argv. Initial components are already
5448 * stripped
5450 * @return A shell status integer (0 for success)
5452 static int rpc_printer_migrate_forms(int argc, const char **argv)
5454 if (!opt_host) {
5455 printf("no server to migrate\n");
5456 return -1;
5459 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5460 rpc_printer_migrate_forms_internals,
5461 argc, argv);
5464 /**
5465 * Migrate printers from a print-server
5467 * @param argc Standard main() style argc
5468 * @param argv Standard main() style argv. Initial components are already
5469 * stripped
5471 * @return A shell status integer (0 for success)
5473 static int rpc_printer_migrate_printers(int argc, const char **argv)
5475 if (!opt_host) {
5476 printf("no server to migrate\n");
5477 return -1;
5480 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5481 rpc_printer_migrate_printers_internals,
5482 argc, argv);
5485 /**
5486 * Migrate printer-ACLs from a print-server
5488 * @param argc Standard main() style argc
5489 * @param argv Standard main() style argv. Initial components are already
5490 * stripped
5492 * @return A shell status integer (0 for success)
5494 static int rpc_printer_migrate_security(int argc, const char **argv)
5496 if (!opt_host) {
5497 printf("no server to migrate\n");
5498 return -1;
5501 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5502 rpc_printer_migrate_security_internals,
5503 argc, argv);
5506 /**
5507 * Migrate printer-settings from a print-server
5509 * @param argc Standard main() style argc
5510 * @param argv Standard main() style argv. Initial components are already
5511 * stripped
5513 * @return A shell status integer (0 for success)
5515 static int rpc_printer_migrate_settings(int argc, const char **argv)
5517 if (!opt_host) {
5518 printf("no server to migrate\n");
5519 return -1;
5522 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5523 rpc_printer_migrate_settings_internals,
5524 argc, argv);
5527 /**
5528 * 'net rpc printer' entrypoint.
5529 * @param argc Standard main() style argc
5530 * @param argv Standard main() style argv. Initial components are already
5531 * stripped
5534 int rpc_printer_migrate(int argc, const char **argv)
5537 /* ouch: when addriver and setdriver are called from within
5538 rpc_printer_migrate_drivers_internals, the printer-queue already
5539 *has* to exist */
5541 struct functable func[] = {
5542 {"all", rpc_printer_migrate_all},
5543 {"drivers", rpc_printer_migrate_drivers},
5544 {"forms", rpc_printer_migrate_forms},
5545 {"help", rpc_printer_usage},
5546 {"printers", rpc_printer_migrate_printers},
5547 {"security", rpc_printer_migrate_security},
5548 {"settings", rpc_printer_migrate_settings},
5549 {NULL, NULL}
5552 return net_run_function(argc, argv, func, rpc_printer_usage);
5556 /**
5557 * List printers on a remote RPC server
5559 * @param argc Standard main() style argc
5560 * @param argv Standard main() style argv. Initial components are already
5561 * stripped
5563 * @return A shell status integer (0 for success)
5565 static int rpc_printer_list(int argc, const char **argv)
5568 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5569 rpc_printer_list_internals,
5570 argc, argv);
5573 /**
5574 * List printer-drivers on a remote RPC server
5576 * @param argc Standard main() style argc
5577 * @param argv Standard main() style argv. Initial components are already
5578 * stripped
5580 * @return A shell status integer (0 for success)
5582 static int rpc_printer_driver_list(int argc, const char **argv)
5585 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5586 rpc_printer_driver_list_internals,
5587 argc, argv);
5590 /**
5591 * Publish printer in ADS via MSRPC
5593 * @param argc Standard main() style argc
5594 * @param argv Standard main() style argv. Initial components are already
5595 * stripped
5597 * @return A shell status integer (0 for success)
5599 static int rpc_printer_publish_publish(int argc, const char **argv)
5602 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5603 rpc_printer_publish_publish_internals,
5604 argc, argv);
5607 /**
5608 * Update printer in ADS via MSRPC
5610 * @param argc Standard main() style argc
5611 * @param argv Standard main() style argv. Initial components are already
5612 * stripped
5614 * @return A shell status integer (0 for success)
5616 static int rpc_printer_publish_update(int argc, const char **argv)
5619 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5620 rpc_printer_publish_update_internals,
5621 argc, argv);
5624 /**
5625 * UnPublish printer in ADS via MSRPC
5627 * @param argc Standard main() style argc
5628 * @param argv Standard main() style argv. Initial components are already
5629 * stripped
5631 * @return A shell status integer (0 for success)
5633 static int rpc_printer_publish_unpublish(int argc, const char **argv)
5636 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5637 rpc_printer_publish_unpublish_internals,
5638 argc, argv);
5641 /**
5642 * List published printers via MSRPC
5644 * @param argc Standard main() style argc
5645 * @param argv Standard main() style argv. Initial components are already
5646 * stripped
5648 * @return A shell status integer (0 for success)
5650 static int rpc_printer_publish_list(int argc, const char **argv)
5653 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5654 rpc_printer_publish_list_internals,
5655 argc, argv);
5659 /**
5660 * Publish printer in ADS
5662 * @param argc Standard main() style argc
5663 * @param argv Standard main() style argv. Initial components are already
5664 * stripped
5666 * @return A shell status integer (0 for success)
5668 static int rpc_printer_publish(int argc, const char **argv)
5671 struct functable func[] = {
5672 {"publish", rpc_printer_publish_publish},
5673 {"update", rpc_printer_publish_update},
5674 {"unpublish", rpc_printer_publish_unpublish},
5675 {"list", rpc_printer_publish_list},
5676 {"help", rpc_printer_usage},
5677 {NULL, NULL}
5680 if (argc == 0)
5681 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5682 rpc_printer_publish_list_internals,
5683 argc, argv);
5685 return net_run_function(argc, argv, func, rpc_printer_usage);
5690 /**
5691 * Display rpc printer help page.
5692 * @param argc Standard main() style argc
5693 * @param argv Standard main() style argv. Initial components are already
5694 * stripped
5696 int rpc_printer_usage(int argc, const char **argv)
5698 return net_help_printer(argc, argv);
5701 /**
5702 * 'net rpc printer' entrypoint.
5703 * @param argc Standard main() style argc
5704 * @param argv Standard main() style argv. Initial components are already
5705 * stripped
5707 int net_rpc_printer(int argc, const char **argv)
5709 struct functable func[] = {
5710 {"list", rpc_printer_list},
5711 {"migrate", rpc_printer_migrate},
5712 {"driver", rpc_printer_driver_list},
5713 {"publish", rpc_printer_publish},
5714 {NULL, NULL}
5717 if (argc == 0)
5718 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5719 rpc_printer_list_internals,
5720 argc, argv);
5722 return net_run_function(argc, argv, func, rpc_printer_usage);
5725 /****************************************************************************/
5728 /**
5729 * Basic usage function for 'net rpc'
5730 * @param argc Standard main() style argc
5731 * @param argv Standard main() style argv. Initial components are already
5732 * stripped
5735 int net_rpc_usage(int argc, const char **argv)
5737 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
5738 d_printf(" net rpc join \t\t\tto join a domain \n");
5739 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
5740 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
5741 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
5742 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
5743 d_printf(" net rpc group \t\tto list groups\n");
5744 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
5745 d_printf(" net rpc printer \t\tto list and migrate printers\n");
5746 d_printf(" net rpc file \t\t\tto list open files\n");
5747 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
5748 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
5749 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
5750 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
5751 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
5752 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
5753 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
5754 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
5755 d_printf(" net rpc registry\t\tto manage registry hives\n");
5756 d_printf("\n");
5757 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
5758 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
5759 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
5760 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
5761 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
5762 return -1;
5767 * Help function for 'net rpc'. Calls command specific help if requested
5768 * or displays usage of net rpc
5769 * @param argc Standard main() style argc
5770 * @param argv Standard main() style argv. Initial components are already
5771 * stripped
5774 int net_rpc_help(int argc, const char **argv)
5776 struct functable func[] = {
5777 {"join", rpc_join_usage},
5778 {"user", rpc_user_usage},
5779 {"group", rpc_group_usage},
5780 {"share", rpc_share_usage},
5781 /*{"changetrustpw", rpc_changetrustpw_usage}, */
5782 {"trustdom", rpc_trustdom_usage},
5783 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
5784 /*{"shutdown", rpc_shutdown_usage}, */
5785 {NULL, NULL}
5788 if (argc == 0) {
5789 net_rpc_usage(argc, argv);
5790 return -1;
5793 return (net_run_function(argc, argv, func, rpc_user_usage));
5797 /**
5798 * 'net rpc' entrypoint.
5799 * @param argc Standard main() style argc
5800 * @param argv Standard main() style argv. Initial components are already
5801 * stripped
5804 int net_rpc(int argc, const char **argv)
5806 struct functable func[] = {
5807 {"info", net_rpc_info},
5808 {"join", net_rpc_join},
5809 {"oldjoin", net_rpc_oldjoin},
5810 {"testjoin", net_rpc_testjoin},
5811 {"user", net_rpc_user},
5812 {"password", rpc_user_password},
5813 {"group", net_rpc_group},
5814 {"share", net_rpc_share},
5815 {"file", net_rpc_file},
5816 {"printer", net_rpc_printer},
5817 {"changetrustpw", net_rpc_changetrustpw},
5818 {"trustdom", rpc_trustdom},
5819 {"abortshutdown", rpc_shutdown_abort},
5820 {"shutdown", rpc_shutdown},
5821 {"samdump", rpc_samdump},
5822 {"vampire", rpc_vampire},
5823 {"getsid", net_rpc_getsid},
5824 {"rights", net_rpc_rights},
5825 {"service", net_rpc_service},
5826 {"registry", net_rpc_registry},
5827 {"help", net_rpc_help},
5828 {NULL, NULL}
5830 return net_run_function(argc, argv, func, net_rpc_usage);