net: Fix several typos in comments.
[Samba.git] / source / utils / net_rpc.c
blob25a4ef1a75c5b28231a694f2995e6b7591cf0177
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "includes.h"
24 #include "utils/net.h"
26 static int net_mode_share;
27 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
29 /**
30 * @file net_rpc.c
32 * @brief RPC based subcommands for the 'net' utility.
34 * This file should contain much of the functionality that used to
35 * be found in rpcclient, execpt that the commands should change
36 * less often, and the fucntionality should be sane (the user is not
37 * expected to know a rid/sid before they conduct an operation etc.)
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
41 **/
44 /**
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
51 **/
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
54 DOM_SID **domain_sid,
55 const char **domain_name)
57 struct rpc_pipe_client *lsa_pipe;
58 POLICY_HND pol;
59 NTSTATUS result = NT_STATUS_OK;
60 union lsa_PolicyInformation *info = NULL;
62 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
63 if (!lsa_pipe) {
64 d_fprintf(stderr, "Could not initialise lsa pipe\n");
65 return result;
68 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
69 SEC_RIGHTS_MAXIMUM_ALLOWED,
70 &pol);
71 if (!NT_STATUS_IS_OK(result)) {
72 d_fprintf(stderr, "open_policy failed: %s\n",
73 nt_errstr(result));
74 return result;
77 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
78 &pol,
79 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
80 &info);
81 if (!NT_STATUS_IS_OK(result)) {
82 d_fprintf(stderr, "lsaquery failed: %s\n",
83 nt_errstr(result));
84 return result;
87 *domain_name = info->account_domain.name.string;
88 *domain_sid = info->account_domain.sid;
90 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
91 TALLOC_FREE(lsa_pipe);
93 return NT_STATUS_OK;
96 /**
97 * Run a single RPC command, from start to finish.
99 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
100 * @param conn_flag a NET_FLAG_ combination. Passed to
101 * net_make_ipc_connection.
102 * @param argc Standard main() style argc.
103 * @param argv Standard main() style argv. Initial components are already
104 * stripped.
105 * @return A shell status integer (0 for success).
108 int run_rpc_command(struct net_context *c,
109 struct cli_state *cli_arg,
110 const int pipe_idx,
111 int conn_flags,
112 rpc_command_fn fn,
113 int argc,
114 const char **argv)
116 struct cli_state *cli = NULL;
117 struct rpc_pipe_client *pipe_hnd = NULL;
118 TALLOC_CTX *mem_ctx;
119 NTSTATUS nt_status;
120 DOM_SID *domain_sid;
121 const char *domain_name;
123 /* make use of cli_state handed over as an argument, if possible */
124 if (!cli_arg) {
125 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
126 if (!NT_STATUS_IS_OK(nt_status)) {
127 DEBUG(1, ("failed to make ipc connection: %s\n",
128 nt_errstr(nt_status)));
129 return -1;
131 } else {
132 cli = cli_arg;
135 if (!cli) {
136 return -1;
139 /* Create mem_ctx */
141 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
142 DEBUG(0, ("talloc_init() failed\n"));
143 cli_shutdown(cli);
144 return -1;
147 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
148 &domain_name);
149 if (!NT_STATUS_IS_OK(nt_status)) {
150 cli_shutdown(cli);
151 return -1;
154 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
155 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
156 /* Always try and create an schannel netlogon pipe. */
157 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
158 PIPE_AUTH_LEVEL_PRIVACY,
159 domain_name,
160 &nt_status);
161 if (!pipe_hnd) {
162 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
163 nt_errstr(nt_status) ));
164 cli_shutdown(cli);
165 return -1;
167 } else {
168 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
169 if (!pipe_hnd) {
170 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
171 cli_get_pipe_name(pipe_idx),
172 nt_errstr(nt_status) ));
173 cli_shutdown(cli);
174 return -1;
179 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
183 } else {
184 DEBUG(5, ("rpc command function succedded\n"));
187 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
188 if (pipe_hnd) {
189 TALLOC_FREE(pipe_hnd);
193 /* close the connection only if it was opened here */
194 if (!cli_arg) {
195 cli_shutdown(cli);
198 talloc_destroy(mem_ctx);
199 return (!NT_STATUS_IS_OK(nt_status));
203 * Force a change of the trust acccount password.
205 * All parameters are provided by the run_rpc_command function, except for
206 * argc, argv which are passed through.
208 * @param domain_sid The domain sid acquired from the remote server.
209 * @param cli A cli_state connected to the server.
210 * @param mem_ctx Talloc context, destroyed on completion of the function.
211 * @param argc Standard main() style argc.
212 * @param argv Standard main() style argv. Initial components are already
213 * stripped.
215 * @return Normal NTSTATUS return.
218 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
219 const DOM_SID *domain_sid,
220 const char *domain_name,
221 struct cli_state *cli,
222 struct rpc_pipe_client *pipe_hnd,
223 TALLOC_CTX *mem_ctx,
224 int argc,
225 const char **argv)
228 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
232 * Force a change of the trust acccount password.
234 * @param argc Standard main() style argc.
235 * @param argv Standard main() style argv. Initial components are already
236 * stripped.
238 * @return A shell status integer (0 for success).
241 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
243 if (c->display_usage) {
244 d_printf("Usage:\n"
245 "net rpc changetrustpw\n"
246 " Change the machine trust password\n");
247 return 0;
250 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
251 rpc_changetrustpw_internals,
252 argc, argv);
256 * Join a domain, the old way.
258 * This uses 'machinename' as the inital password, and changes it.
260 * The password should be created with 'server manager' or equiv first.
262 * All parameters are provided by the run_rpc_command function, except for
263 * argc, argv which are passed through.
265 * @param domain_sid The domain sid acquired from the remote server.
266 * @param cli A cli_state connected to the server.
267 * @param mem_ctx Talloc context, destroyed on completion of the function.
268 * @param argc Standard main() style argc.
269 * @param argv Standard main() style argv. Initial components are already
270 * stripped.
272 * @return Normal NTSTATUS return.
275 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
276 const DOM_SID *domain_sid,
277 const char *domain_name,
278 struct cli_state *cli,
279 struct rpc_pipe_client *pipe_hnd,
280 TALLOC_CTX *mem_ctx,
281 int argc,
282 const char **argv)
285 fstring trust_passwd;
286 unsigned char orig_trust_passwd_hash[16];
287 NTSTATUS result;
288 uint32 sec_channel_type;
290 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
291 if (!pipe_hnd) {
292 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
293 "error was %s\n",
294 cli->desthost,
295 nt_errstr(result) ));
296 return result;
300 check what type of join - if the user want's to join as
301 a BDC, the server must agree that we are a BDC.
303 if (argc >= 0) {
304 sec_channel_type = get_sec_channel_type(argv[0]);
305 } else {
306 sec_channel_type = get_sec_channel_type(NULL);
309 fstrcpy(trust_passwd, global_myname());
310 strlower_m(trust_passwd);
313 * Machine names can be 15 characters, but the max length on
314 * a password is 14. --jerry
317 trust_passwd[14] = '\0';
319 E_md4hash(trust_passwd, orig_trust_passwd_hash);
321 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
322 orig_trust_passwd_hash,
323 sec_channel_type);
325 if (NT_STATUS_IS_OK(result))
326 printf("Joined domain %s.\n", c->opt_target_workgroup);
329 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
330 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
331 result = NT_STATUS_UNSUCCESSFUL;
334 return result;
338 * Join a domain, the old way.
340 * @param argc Standard main() style argc.
341 * @param argv Standard main() style argv. Initial components are already
342 * stripped.
344 * @return A shell status integer (0 for success).
347 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
349 return run_rpc_command(c, NULL, PI_NETLOGON,
350 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
351 rpc_oldjoin_internals,
352 argc, argv);
356 * Join a domain, the old way. This function exists to allow
357 * the message to be displayed when oldjoin was explicitly
358 * requested, but not when it was implied by "net rpc join".
360 * @param argc Standard main() style argc.
361 * @param argv Standard main() style argv. Initial components are already
362 * stripped.
364 * @return A shell status integer (0 for success).
367 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
369 int rc = -1;
371 if (c->display_usage) {
372 d_printf("Usage:\n"
373 "net rpc oldjoin\n"
374 " Join a domain the old way\n");
375 return 0;
378 rc = net_rpc_perform_oldjoin(c, argc, argv);
380 if (rc) {
381 d_fprintf(stderr, "Failed to join domain\n");
384 return rc;
388 * 'net rpc join' entrypoint.
389 * @param argc Standard main() style argc.
390 * @param argv Standard main() style argv. Initial components are already
391 * stripped
393 * Main 'net_rpc_join()' (where the admin username/password is used) is
394 * in net_rpc_join.c.
395 * Try to just change the password, but if that doesn't work, use/prompt
396 * for a username/password.
399 int net_rpc_join(struct net_context *c, int argc, const char **argv)
401 if (c->display_usage) {
402 d_printf("Usage:\n"
403 "net rpc join -U <username>[%%password] <type>\n"
404 " Join a domain\n"
405 " username\tName of the admin user"
406 " password\tPassword of the admin user, will "
407 "prompt if not specified\n"
408 " type\tCan be one of the following:\n"
409 "\t\tMEMBER\tJoin as member server (default)\n"
410 "\t\tBDC\tJoin as BDC\n"
411 "\t\tPDC\tJoin as PDC\n");
412 return 0;
415 if (lp_server_role() == ROLE_STANDALONE) {
416 d_printf("cannot join as standalone machine\n");
417 return -1;
420 if (strlen(global_myname()) > 15) {
421 d_printf("Our netbios name can be at most 15 chars long, "
422 "\"%s\" is %u chars long\n",
423 global_myname(), (unsigned int)strlen(global_myname()));
424 return -1;
427 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
428 return 0;
430 return net_rpc_join_newstyle(c, argc, argv);
434 * display info about a rpc domain
436 * All parameters are provided by the run_rpc_command function, except for
437 * argc, argv which are passed 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, destroyed 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 NTSTATUS rpc_info_internals(struct net_context *c,
450 const DOM_SID *domain_sid,
451 const char *domain_name,
452 struct cli_state *cli,
453 struct rpc_pipe_client *pipe_hnd,
454 TALLOC_CTX *mem_ctx,
455 int argc,
456 const char **argv)
458 POLICY_HND connect_pol, domain_pol;
459 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
460 union samr_DomainInfo *info = NULL;
461 fstring sid_str;
463 sid_to_fstring(sid_str, domain_sid);
465 /* Get sam policy handle */
466 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
467 pipe_hnd->desthost,
468 MAXIMUM_ALLOWED_ACCESS,
469 &connect_pol);
470 if (!NT_STATUS_IS_OK(result)) {
471 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
472 goto done;
475 /* Get domain policy handle */
476 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
477 &connect_pol,
478 MAXIMUM_ALLOWED_ACCESS,
479 CONST_DISCARD(struct dom_sid2 *, domain_sid),
480 &domain_pol);
481 if (!NT_STATUS_IS_OK(result)) {
482 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
483 goto done;
486 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
487 &domain_pol,
489 &info);
490 if (NT_STATUS_IS_OK(result)) {
491 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
492 d_printf("Domain SID: %s\n", sid_str);
493 d_printf("Sequence number: %llu\n",
494 (unsigned long long)info->info2.sequence_num);
495 d_printf("Num users: %u\n", info->info2.num_users);
496 d_printf("Num domain groups: %u\n", info->info2.num_groups);
497 d_printf("Num local groups: %u\n", info->info2.num_aliases);
500 done:
501 return result;
505 * 'net rpc info' entrypoint.
506 * @param argc Standard main() style argc.
507 * @param argv Standard main() style argv. Initial components are already
508 * stripped.
511 int net_rpc_info(struct net_context *c, int argc, const char **argv)
513 if (c->display_usage) {
514 d_printf("Usage:\n"
515 "net rpc info\n"
516 " Display information about the domain\n");
517 return 0;
520 return run_rpc_command(c, NULL, PI_SAMR, NET_FLAGS_PDC,
521 rpc_info_internals,
522 argc, argv);
526 * Fetch domain SID into the local secrets.tdb.
528 * All parameters are provided by the run_rpc_command function, except for
529 * argc, argv which are passed through.
531 * @param domain_sid The domain sid acquired from the remote server.
532 * @param cli A cli_state connected to the server.
533 * @param mem_ctx Talloc context, destroyed on completion of the function.
534 * @param argc Standard main() style argc.
535 * @param argv Standard main() style argv. Initial components are already
536 * stripped.
538 * @return Normal NTSTATUS return.
541 static NTSTATUS rpc_getsid_internals(struct net_context *c,
542 const DOM_SID *domain_sid,
543 const char *domain_name,
544 struct cli_state *cli,
545 struct rpc_pipe_client *pipe_hnd,
546 TALLOC_CTX *mem_ctx,
547 int argc,
548 const char **argv)
550 fstring sid_str;
552 sid_to_fstring(sid_str, domain_sid);
553 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
554 sid_str, domain_name);
556 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
557 DEBUG(0,("Can't store domain SID\n"));
558 return NT_STATUS_UNSUCCESSFUL;
561 return NT_STATUS_OK;
565 * 'net rpc getsid' entrypoint.
566 * @param argc Standard main() style argc.
567 * @param argv Standard main() style argv. Initial components are already
568 * stripped.
571 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
573 if (c->display_usage) {
574 d_printf("Usage:\n"
575 "net rpc getsid\n"
576 " Fetch domain SID into local secrets.tdb\n");
577 return 0;
580 return run_rpc_command(c, NULL, PI_SAMR,
581 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
582 rpc_getsid_internals,
583 argc, argv);
586 /****************************************************************************/
589 * Basic usage function for 'net rpc user'.
590 * @param argc Standard main() style argc.
591 * @param argv Standard main() style argv. Initial components are already
592 * stripped.
595 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
597 return net_user_usage(c, argc, argv);
601 * Add a new user to a remote RPC server.
603 * @param argc Standard main() style argc.
604 * @param argv Standard main() style argv. Initial components are already
605 * stripped.
607 * @return A shell status integer (0 for success).
610 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
612 NET_API_STATUS status;
613 struct USER_INFO_1 info1;
614 uint32_t parm_error = 0;
616 if (argc < 1 || c->display_usage) {
617 rpc_user_usage(c, argc, argv);
618 return 0;
621 ZERO_STRUCT(info1);
623 info1.usri1_name = argv[0];
624 if (argc == 2) {
625 info1.usri1_password = argv[1];
628 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
630 if (status != 0) {
631 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
632 argv[0], libnetapi_get_error_string(c->netapi_ctx,
633 status));
634 return -1;
635 } else {
636 d_printf("Added user '%s'.\n", argv[0]);
639 return 0;
643 * Rename a user on a remote RPC server.
645 * All parameters are provided by the run_rpc_command function, except for
646 * argc, argv which are passed through.
648 * @param domain_sid The domain sid acquired from the remote server.
649 * @param cli A cli_state connected to the server.
650 * @param mem_ctx Talloc context, destroyed on completion of the function.
651 * @param argc Standard main() style argc.
652 * @param argv Standard main() style argv. Initial components are already
653 * stripped.
655 * @return Normal NTSTATUS return.
658 static NTSTATUS rpc_user_rename_internals(struct net_context *c,
659 const DOM_SID *domain_sid,
660 const char *domain_name,
661 struct cli_state *cli,
662 struct rpc_pipe_client *pipe_hnd,
663 TALLOC_CTX *mem_ctx,
664 int argc,
665 const char **argv)
667 POLICY_HND connect_pol, domain_pol, user_pol;
668 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
669 uint32 info_level = 7;
670 const char *old_name, *new_name;
671 struct samr_Ids user_rids, name_types;
672 struct lsa_String lsa_acct_name;
673 union samr_UserInfo *info = NULL;
675 if (argc != 2 || c->display_usage) {
676 rpc_user_usage(c, argc, argv);
677 return NT_STATUS_OK;
680 old_name = argv[0];
681 new_name = argv[1];
683 /* Get sam policy handle */
685 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
686 pipe_hnd->desthost,
687 MAXIMUM_ALLOWED_ACCESS,
688 &connect_pol);
690 if (!NT_STATUS_IS_OK(result)) {
691 goto done;
694 /* Get domain policy handle */
696 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
697 &connect_pol,
698 MAXIMUM_ALLOWED_ACCESS,
699 CONST_DISCARD(struct dom_sid2 *, domain_sid),
700 &domain_pol);
701 if (!NT_STATUS_IS_OK(result)) {
702 goto done;
705 init_lsa_String(&lsa_acct_name, old_name);
707 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
708 &domain_pol,
710 &lsa_acct_name,
711 &user_rids,
712 &name_types);
713 if (!NT_STATUS_IS_OK(result)) {
714 goto done;
717 /* Open domain user */
718 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
719 &domain_pol,
720 MAXIMUM_ALLOWED_ACCESS,
721 user_rids.ids[0],
722 &user_pol);
724 if (!NT_STATUS_IS_OK(result)) {
725 goto done;
728 /* Query user info */
729 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
730 &user_pol,
731 info_level,
732 &info);
734 if (!NT_STATUS_IS_OK(result)) {
735 goto done;
738 init_samr_user_info7(&info->info7, new_name);
740 /* Set new name */
741 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
742 &user_pol,
743 info_level,
744 info);
746 if (!NT_STATUS_IS_OK(result)) {
747 goto done;
750 done:
751 if (!NT_STATUS_IS_OK(result)) {
752 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
753 nt_errstr(result));
754 } else {
755 d_printf("Renamed user from %s to %s\n", old_name, new_name);
757 return result;
761 * Rename a user on a remote RPC server.
763 * @param argc Standard main() style argc.
764 * @param argv Standard main() style argv. Initial components are already
765 * stripped.
767 * @return A shell status integer (0 for success).
770 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
772 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_rename_internals,
773 argc, argv);
777 * Delete a user from a remote RPC server.
779 * @param argc Standard main() style argc.
780 * @param argv Standard main() style argv. Initial components are already
781 * stripped.
783 * @return A shell status integer (0 for success).
786 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
788 NET_API_STATUS status;
790 if (argc < 1 || c->display_usage) {
791 rpc_user_usage(c, argc, argv);
792 return 0;
795 status = NetUserDel(c->opt_host, argv[0]);
797 if (status != 0) {
798 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
799 argv[0],
800 libnetapi_get_error_string(c->netapi_ctx, status));
801 return -1;
802 } else {
803 d_printf("Deleted user '%s'.\n", argv[0]);
806 return 0;
810 * Set a password for a user on a remote RPC server.
812 * All parameters are provided by the run_rpc_command function, except for
813 * argc, argv which are passed through.
815 * @param domain_sid The domain sid acquired from the remote server.
816 * @param cli A cli_state connected to the server.
817 * @param mem_ctx Talloc context, destroyed on completion of the function.
818 * @param argc Standard main() style argc.
819 * @param argv Standard main() style argv. Initial components are already
820 * stripped.
822 * @return Normal NTSTATUS return.
825 static NTSTATUS rpc_user_password_internals(struct net_context *c,
826 const DOM_SID *domain_sid,
827 const char *domain_name,
828 struct cli_state *cli,
829 struct rpc_pipe_client *pipe_hnd,
830 TALLOC_CTX *mem_ctx,
831 int argc,
832 const char **argv)
834 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
835 POLICY_HND connect_pol, domain_pol, user_pol;
836 uchar pwbuf[516];
837 const char *user;
838 const char *new_password;
839 char *prompt = NULL;
840 union samr_UserInfo info;
842 if (argc < 1 || c->display_usage) {
843 rpc_user_usage(c, argc, argv);
844 return NT_STATUS_OK;
847 user = argv[0];
849 if (argv[1]) {
850 new_password = argv[1];
851 } else {
852 asprintf(&prompt, "Enter new password for %s:", user);
853 new_password = getpass(prompt);
854 SAFE_FREE(prompt);
857 /* Get sam policy and domain handles */
859 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
860 pipe_hnd->desthost,
861 MAXIMUM_ALLOWED_ACCESS,
862 &connect_pol);
864 if (!NT_STATUS_IS_OK(result)) {
865 goto done;
868 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
869 &connect_pol,
870 MAXIMUM_ALLOWED_ACCESS,
871 CONST_DISCARD(struct dom_sid2 *, domain_sid),
872 &domain_pol);
874 if (!NT_STATUS_IS_OK(result)) {
875 goto done;
878 /* Get handle on user */
881 struct samr_Ids user_rids, name_types;
882 struct lsa_String lsa_acct_name;
884 init_lsa_String(&lsa_acct_name, user);
886 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
887 &domain_pol,
889 &lsa_acct_name,
890 &user_rids,
891 &name_types);
892 if (!NT_STATUS_IS_OK(result)) {
893 goto done;
896 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
897 &domain_pol,
898 MAXIMUM_ALLOWED_ACCESS,
899 user_rids.ids[0],
900 &user_pol);
902 if (!NT_STATUS_IS_OK(result)) {
903 goto done;
907 /* Set password on account */
909 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
911 init_samr_user_info24(&info.info24, pwbuf, 24);
913 SamOEMhashBlob(info.info24.password.data, 516,
914 &cli->user_session_key);
916 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
917 &user_pol,
919 &info);
921 if (!NT_STATUS_IS_OK(result)) {
922 goto done;
925 /* Display results */
927 done:
928 return result;
933 * Set a user's password on a remote RPC server.
935 * @param argc Standard main() style argc.
936 * @param argv Standard main() style argv. Initial components are already
937 * stripped.
939 * @return A shell status integer (0 for success).
942 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
944 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_password_internals,
945 argc, argv);
949 * List user's groups on a remote RPC server.
951 * All parameters are provided by the run_rpc_command function, except for
952 * argc, argv which are passed through.
954 * @param domain_sid The domain sid acquired from the remote server.
955 * @param cli A cli_state connected to the server.
956 * @param mem_ctx Talloc context, destroyed on completion of the function.
957 * @param argc Standard main() style argc.
958 * @param argv Standard main() style argv. Initial components are already
959 * stripped.
961 * @return Normal NTSTATUS return.
964 static NTSTATUS rpc_user_info_internals(struct net_context *c,
965 const DOM_SID *domain_sid,
966 const char *domain_name,
967 struct cli_state *cli,
968 struct rpc_pipe_client *pipe_hnd,
969 TALLOC_CTX *mem_ctx,
970 int argc,
971 const char **argv)
973 POLICY_HND connect_pol, domain_pol, user_pol;
974 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
975 int i;
976 struct samr_RidWithAttributeArray *rid_array = NULL;
977 struct lsa_Strings names;
978 struct samr_Ids types;
979 uint32_t *lrids = NULL;
980 struct samr_Ids rids, name_types;
981 struct lsa_String lsa_acct_name;
984 if (argc < 1 || c->display_usage) {
985 rpc_user_usage(c, argc, argv);
986 return NT_STATUS_OK;
988 /* Get sam policy handle */
990 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
991 pipe_hnd->desthost,
992 MAXIMUM_ALLOWED_ACCESS,
993 &connect_pol);
994 if (!NT_STATUS_IS_OK(result)) goto done;
996 /* Get domain policy handle */
998 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
999 &connect_pol,
1000 MAXIMUM_ALLOWED_ACCESS,
1001 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1002 &domain_pol);
1003 if (!NT_STATUS_IS_OK(result)) goto done;
1005 /* Get handle on user */
1007 init_lsa_String(&lsa_acct_name, argv[0]);
1009 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1010 &domain_pol,
1012 &lsa_acct_name,
1013 &rids,
1014 &name_types);
1016 if (!NT_STATUS_IS_OK(result)) goto done;
1018 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1019 &domain_pol,
1020 MAXIMUM_ALLOWED_ACCESS,
1021 rids.ids[0],
1022 &user_pol);
1023 if (!NT_STATUS_IS_OK(result)) goto done;
1025 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
1026 &user_pol,
1027 &rid_array);
1029 if (!NT_STATUS_IS_OK(result)) goto done;
1031 /* Look up rids */
1033 if (rid_array->count) {
1034 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
1035 result = NT_STATUS_NO_MEMORY;
1036 goto done;
1039 for (i = 0; i < rid_array->count; i++)
1040 lrids[i] = rid_array->rids[i].rid;
1042 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
1043 &domain_pol,
1044 rid_array->count,
1045 lrids,
1046 &names,
1047 &types);
1049 if (!NT_STATUS_IS_OK(result)) {
1050 goto done;
1053 /* Display results */
1055 for (i = 0; i < names.count; i++)
1056 printf("%s\n", names.names[i].string);
1058 done:
1059 return result;
1063 * List a user's groups from a remote RPC server.
1065 * @param argc Standard main() style argc.
1066 * @param argv Standard main() style argv. Initial components are already
1067 * stripped.
1069 * @return A shell status integer (0 for success)
1072 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1074 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_info_internals,
1075 argc, argv);
1079 * List users on a remote RPC server.
1081 * All parameters are provided by the run_rpc_command function, except for
1082 * argc, argv which are passed through.
1084 * @param domain_sid The domain sid acquired from the remote server.
1085 * @param cli A cli_state connected to the server.
1086 * @param mem_ctx Talloc context, destroyed on completion of the function.
1087 * @param argc Standard main() style argc.
1088 * @param argv Standard main() style argv. Initial components are already
1089 * stripped.
1091 * @return Normal NTSTATUS return.
1094 static NTSTATUS rpc_user_list_internals(struct net_context *c,
1095 const DOM_SID *domain_sid,
1096 const char *domain_name,
1097 struct cli_state *cli,
1098 struct rpc_pipe_client *pipe_hnd,
1099 TALLOC_CTX *mem_ctx,
1100 int argc,
1101 const char **argv)
1103 POLICY_HND connect_pol, domain_pol;
1104 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1105 uint32 start_idx=0, num_entries, i, loop_count = 0;
1107 /* Get sam policy handle */
1109 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1110 pipe_hnd->desthost,
1111 MAXIMUM_ALLOWED_ACCESS,
1112 &connect_pol);
1113 if (!NT_STATUS_IS_OK(result)) {
1114 goto done;
1117 /* Get domain policy handle */
1119 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1120 &connect_pol,
1121 MAXIMUM_ALLOWED_ACCESS,
1122 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1123 &domain_pol);
1124 if (!NT_STATUS_IS_OK(result)) {
1125 goto done;
1128 /* Query domain users */
1129 if (c->opt_long_list_entries)
1130 d_printf("\nUser name Comment"
1131 "\n-----------------------------\n");
1132 do {
1133 const char *user = NULL;
1134 const char *desc = NULL;
1135 uint32 max_entries, max_size;
1136 uint32_t total_size, returned_size;
1137 union samr_DispInfo info;
1139 get_query_dispinfo_params(
1140 loop_count, &max_entries, &max_size);
1142 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1143 &domain_pol,
1145 start_idx,
1146 max_entries,
1147 max_size,
1148 &total_size,
1149 &returned_size,
1150 &info);
1151 loop_count++;
1152 start_idx += info.info1.count;
1153 num_entries = info.info1.count;
1155 for (i = 0; i < num_entries; i++) {
1156 user = info.info1.entries[i].account_name.string;
1157 if (c->opt_long_list_entries)
1158 desc = info.info1.entries[i].description.string;
1159 if (c->opt_long_list_entries)
1160 printf("%-21.21s %s\n", user, desc);
1161 else
1162 printf("%s\n", user);
1164 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1166 done:
1167 return result;
1171 * 'net rpc user' entrypoint.
1172 * @param argc Standard main() style argc.
1173 * @param argv Standard main() style argv. Initial components are already
1174 * stripped.
1177 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1179 NET_API_STATUS status;
1181 struct functable func[] = {
1183 "add",
1184 rpc_user_add,
1185 NET_TRANSPORT_RPC,
1186 "Add specified user",
1187 "net rpc user add\n"
1188 " Add specified user"
1191 "info",
1192 rpc_user_info,
1193 NET_TRANSPORT_RPC,
1194 "List domain groups of user",
1195 "net rpc user info\n"
1196 " Lis domain groups of user"
1199 "delete",
1200 rpc_user_delete,
1201 NET_TRANSPORT_RPC,
1202 "Remove specified user",
1203 "net rpc user delete\n"
1204 " Remove specified user"
1207 "password",
1208 rpc_user_password,
1209 NET_TRANSPORT_RPC,
1210 "Change user password",
1211 "net rpc user password\n"
1212 " Change user password"
1215 "rename",
1216 rpc_user_rename,
1217 NET_TRANSPORT_RPC,
1218 "Rename specified user",
1219 "net rpc user rename\n"
1220 " Rename specified user"
1222 {NULL, NULL, 0, NULL, NULL}
1225 status = libnetapi_init(&c->netapi_ctx);
1226 if (status != 0) {
1227 return -1;
1229 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1230 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1232 if (argc == 0) {
1233 if (c->display_usage) {
1234 d_printf("Usage:\n");
1235 d_printf("net rpc user\n"
1236 " List all users\n");
1237 net_display_usage_from_functable(func);
1238 return 0;
1241 return run_rpc_command(c, NULL,PI_SAMR, 0,
1242 rpc_user_list_internals,
1243 argc, argv);
1246 return net_run_function(c, argc, argv, "net rpc user", func);
1249 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1250 TALLOC_CTX *mem_ctx,
1251 struct rpc_sh_ctx *ctx,
1252 struct rpc_pipe_client *pipe_hnd,
1253 int argc, const char **argv)
1255 return rpc_user_list_internals(c, ctx->domain_sid, ctx->domain_name,
1256 ctx->cli, pipe_hnd, mem_ctx,
1257 argc, argv);
1260 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1261 TALLOC_CTX *mem_ctx,
1262 struct rpc_sh_ctx *ctx,
1263 struct rpc_pipe_client *pipe_hnd,
1264 int argc, const char **argv)
1266 return rpc_user_info_internals(c, ctx->domain_sid, ctx->domain_name,
1267 ctx->cli, pipe_hnd, mem_ctx,
1268 argc, argv);
1271 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1272 TALLOC_CTX *mem_ctx,
1273 struct rpc_sh_ctx *ctx,
1274 struct rpc_pipe_client *pipe_hnd,
1275 int argc, const char **argv,
1276 NTSTATUS (*fn)(
1277 struct net_context *c,
1278 TALLOC_CTX *mem_ctx,
1279 struct rpc_sh_ctx *ctx,
1280 struct rpc_pipe_client *pipe_hnd,
1281 POLICY_HND *user_hnd,
1282 int argc, const char **argv))
1284 POLICY_HND connect_pol, domain_pol, user_pol;
1285 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1286 DOM_SID sid;
1287 uint32 rid;
1288 enum lsa_SidType type;
1290 if (argc == 0) {
1291 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1292 return NT_STATUS_INVALID_PARAMETER;
1295 ZERO_STRUCT(connect_pol);
1296 ZERO_STRUCT(domain_pol);
1297 ZERO_STRUCT(user_pol);
1299 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1300 argv[0], NULL, NULL, &sid, &type);
1301 if (!NT_STATUS_IS_OK(result)) {
1302 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1303 nt_errstr(result));
1304 goto done;
1307 if (type != SID_NAME_USER) {
1308 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1309 sid_type_lookup(type));
1310 result = NT_STATUS_NO_SUCH_USER;
1311 goto done;
1314 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1315 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1316 result = NT_STATUS_NO_SUCH_USER;
1317 goto done;
1320 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1321 pipe_hnd->desthost,
1322 MAXIMUM_ALLOWED_ACCESS,
1323 &connect_pol);
1324 if (!NT_STATUS_IS_OK(result)) {
1325 goto done;
1328 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1329 &connect_pol,
1330 MAXIMUM_ALLOWED_ACCESS,
1331 ctx->domain_sid,
1332 &domain_pol);
1333 if (!NT_STATUS_IS_OK(result)) {
1334 goto done;
1337 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1338 &domain_pol,
1339 MAXIMUM_ALLOWED_ACCESS,
1340 rid,
1341 &user_pol);
1342 if (!NT_STATUS_IS_OK(result)) {
1343 goto done;
1346 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1348 done:
1349 if (is_valid_policy_hnd(&user_pol)) {
1350 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1352 if (is_valid_policy_hnd(&domain_pol)) {
1353 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1355 if (is_valid_policy_hnd(&connect_pol)) {
1356 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1358 return result;
1361 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1362 TALLOC_CTX *mem_ctx,
1363 struct rpc_sh_ctx *ctx,
1364 struct rpc_pipe_client *pipe_hnd,
1365 POLICY_HND *user_hnd,
1366 int argc, const char **argv)
1368 NTSTATUS result;
1369 union samr_UserInfo *info = NULL;
1371 if (argc != 0) {
1372 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1373 return NT_STATUS_INVALID_PARAMETER;
1376 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1377 user_hnd,
1379 &info);
1380 if (!NT_STATUS_IS_OK(result)) {
1381 return result;
1384 d_printf("user rid: %d, group rid: %d\n",
1385 info->info21.rid,
1386 info->info21.primary_gid);
1388 return result;
1391 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1392 TALLOC_CTX *mem_ctx,
1393 struct rpc_sh_ctx *ctx,
1394 struct rpc_pipe_client *pipe_hnd,
1395 int argc, const char **argv)
1397 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1398 rpc_sh_user_show_internals);
1401 #define FETCHSTR(name, rec) \
1402 do { if (strequal(ctx->thiscmd, name)) { \
1403 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1404 } while (0);
1406 #define SETSTR(name, rec, flag) \
1407 do { if (strequal(ctx->thiscmd, name)) { \
1408 init_lsa_String(&(info->info21.rec), argv[0]); \
1409 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1410 } while (0);
1412 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1413 TALLOC_CTX *mem_ctx,
1414 struct rpc_sh_ctx *ctx,
1415 struct rpc_pipe_client *pipe_hnd,
1416 POLICY_HND *user_hnd,
1417 int argc, const char **argv)
1419 NTSTATUS result;
1420 const char *username;
1421 const char *oldval = "";
1422 union samr_UserInfo *info = NULL;
1424 if (argc > 1) {
1425 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1426 ctx->whoami);
1427 return NT_STATUS_INVALID_PARAMETER;
1430 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1431 user_hnd,
1433 &info);
1434 if (!NT_STATUS_IS_OK(result)) {
1435 return result;
1438 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1440 FETCHSTR("fullname", full_name);
1441 FETCHSTR("homedir", home_directory);
1442 FETCHSTR("homedrive", home_drive);
1443 FETCHSTR("logonscript", logon_script);
1444 FETCHSTR("profilepath", profile_path);
1445 FETCHSTR("description", description);
1447 if (argc == 0) {
1448 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1449 goto done;
1452 if (strcmp(argv[0], "NULL") == 0) {
1453 argv[0] = "";
1456 ZERO_STRUCT(info->info21);
1458 SETSTR("fullname", full_name, FULL_NAME);
1459 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1460 SETSTR("homedrive", home_drive, HOME_DRIVE);
1461 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1462 SETSTR("profilepath", profile_path, PROFILE_PATH);
1463 SETSTR("description", description, DESCRIPTION);
1465 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1466 user_hnd,
1468 info);
1470 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1471 ctx->thiscmd, oldval, argv[0]);
1473 done:
1475 return result;
1478 #define HANDLEFLG(name, rec) \
1479 do { if (strequal(ctx->thiscmd, name)) { \
1480 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1481 if (newval) { \
1482 newflags = oldflags | ACB_##rec; \
1483 } else { \
1484 newflags = oldflags & ~ACB_##rec; \
1485 } } } while (0);
1487 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1488 TALLOC_CTX *mem_ctx,
1489 struct rpc_sh_ctx *ctx,
1490 struct rpc_pipe_client *pipe_hnd,
1491 int argc, const char **argv)
1493 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1494 rpc_sh_user_str_edit_internals);
1497 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1498 TALLOC_CTX *mem_ctx,
1499 struct rpc_sh_ctx *ctx,
1500 struct rpc_pipe_client *pipe_hnd,
1501 POLICY_HND *user_hnd,
1502 int argc, const char **argv)
1504 NTSTATUS result;
1505 const char *username;
1506 const char *oldval = "unknown";
1507 uint32 oldflags, newflags;
1508 bool newval;
1509 union samr_UserInfo *info = NULL;
1511 if ((argc > 1) ||
1512 ((argc == 1) && !strequal(argv[0], "yes") &&
1513 !strequal(argv[0], "no"))) {
1514 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1515 ctx->whoami);
1516 return NT_STATUS_INVALID_PARAMETER;
1519 newval = strequal(argv[0], "yes");
1521 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1522 user_hnd,
1524 &info);
1525 if (!NT_STATUS_IS_OK(result)) {
1526 return result;
1529 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1530 oldflags = info->info21.acct_flags;
1531 newflags = info->info21.acct_flags;
1533 HANDLEFLG("disabled", DISABLED);
1534 HANDLEFLG("pwnotreq", PWNOTREQ);
1535 HANDLEFLG("autolock", AUTOLOCK);
1536 HANDLEFLG("pwnoexp", PWNOEXP);
1538 if (argc == 0) {
1539 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1540 goto done;
1543 ZERO_STRUCT(info->info21);
1545 info->info21.acct_flags = newflags;
1546 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1548 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1549 user_hnd,
1551 info);
1553 if (NT_STATUS_IS_OK(result)) {
1554 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1555 ctx->thiscmd, oldval, argv[0]);
1558 done:
1560 return result;
1563 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1564 TALLOC_CTX *mem_ctx,
1565 struct rpc_sh_ctx *ctx,
1566 struct rpc_pipe_client *pipe_hnd,
1567 int argc, const char **argv)
1569 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1570 rpc_sh_user_flag_edit_internals);
1573 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1574 TALLOC_CTX *mem_ctx,
1575 struct rpc_sh_ctx *ctx)
1577 static struct rpc_sh_cmd cmds[] = {
1579 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1580 "Show/Set a user's full name" },
1582 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1583 "Show/Set a user's home directory" },
1585 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1586 "Show/Set a user's home drive" },
1588 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1589 "Show/Set a user's logon script" },
1591 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1592 "Show/Set a user's profile path" },
1594 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1595 "Show/Set a user's description" },
1597 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1598 "Show/Set whether a user is disabled" },
1600 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1601 "Show/Set whether a user locked out" },
1603 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1604 "Show/Set whether a user does not need a password" },
1606 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1607 "Show/Set whether a user's password does not expire" },
1609 { NULL, NULL, 0, NULL, NULL }
1612 return cmds;
1615 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1616 TALLOC_CTX *mem_ctx,
1617 struct rpc_sh_ctx *ctx)
1619 static struct rpc_sh_cmd cmds[] = {
1621 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1622 "List available users" },
1624 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1625 "List the domain groups a user is member of" },
1627 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1628 "Show info about a user" },
1630 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1631 "Show/Modify a user's fields" },
1633 { NULL, NULL, 0, NULL, NULL }
1636 return cmds;
1639 /****************************************************************************/
1642 * Basic usage function for 'net rpc group'.
1643 * @param argc Standard main() style argc.
1644 * @param argv Standard main() style argv. Initial components are already
1645 * stripped.
1648 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1650 return net_group_usage(c, argc, argv);
1654 * Delete group on a remote RPC server.
1656 * All parameters are provided by the run_rpc_command function, except for
1657 * argc, argv which are passed through.
1659 * @param domain_sid The domain sid acquired from the remote server.
1660 * @param cli A cli_state connected to the server.
1661 * @param mem_ctx Talloc context, destroyed on completion of the function.
1662 * @param argc Standard main() style argc.
1663 * @param argv Standard main() style argv. Initial components are already
1664 * stripped.
1666 * @return Normal NTSTATUS return.
1669 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1670 const DOM_SID *domain_sid,
1671 const char *domain_name,
1672 struct cli_state *cli,
1673 struct rpc_pipe_client *pipe_hnd,
1674 TALLOC_CTX *mem_ctx,
1675 int argc,
1676 const char **argv)
1678 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1679 bool group_is_primary = false;
1680 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1681 uint32_t group_rid;
1682 struct samr_RidTypeArray *rids = NULL;
1683 /* char **names; */
1684 int i;
1685 /* DOM_GID *user_gids; */
1687 struct samr_Ids group_rids, name_types;
1688 struct lsa_String lsa_acct_name;
1689 union samr_UserInfo *info = NULL;
1691 if (argc < 1 || c->display_usage) {
1692 rpc_group_usage(c, argc,argv);
1693 return NT_STATUS_OK; /* ok? */
1696 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1697 pipe_hnd->desthost,
1698 MAXIMUM_ALLOWED_ACCESS,
1699 &connect_pol);
1701 if (!NT_STATUS_IS_OK(result)) {
1702 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1703 goto done;
1706 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1707 &connect_pol,
1708 MAXIMUM_ALLOWED_ACCESS,
1709 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1710 &domain_pol);
1712 if (!NT_STATUS_IS_OK(result)) {
1713 d_fprintf(stderr, "Request open_domain failed\n");
1714 goto done;
1717 init_lsa_String(&lsa_acct_name, argv[0]);
1719 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1720 &domain_pol,
1722 &lsa_acct_name,
1723 &group_rids,
1724 &name_types);
1725 if (!NT_STATUS_IS_OK(result)) {
1726 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1727 goto done;
1730 switch (name_types.ids[0])
1732 case SID_NAME_DOM_GRP:
1733 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1734 &domain_pol,
1735 MAXIMUM_ALLOWED_ACCESS,
1736 group_rids.ids[0],
1737 &group_pol);
1738 if (!NT_STATUS_IS_OK(result)) {
1739 d_fprintf(stderr, "Request open_group failed");
1740 goto done;
1743 group_rid = group_rids.ids[0];
1745 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1746 &group_pol,
1747 &rids);
1749 if (!NT_STATUS_IS_OK(result)) {
1750 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1751 goto done;
1754 if (c->opt_verbose) {
1755 d_printf("Domain Group %s (rid: %d) has %d members\n",
1756 argv[0],group_rid, rids->count);
1759 /* Check if group is anyone's primary group */
1760 for (i = 0; i < rids->count; i++)
1762 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1763 &domain_pol,
1764 MAXIMUM_ALLOWED_ACCESS,
1765 rids->rids[i],
1766 &user_pol);
1768 if (!NT_STATUS_IS_OK(result)) {
1769 d_fprintf(stderr, "Unable to open group member %d\n",
1770 rids->rids[i]);
1771 goto done;
1774 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1775 &user_pol,
1777 &info);
1779 if (!NT_STATUS_IS_OK(result)) {
1780 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1781 rids->rids[i]);
1782 goto done;
1785 if (info->info21.primary_gid == group_rid) {
1786 if (c->opt_verbose) {
1787 d_printf("Group is primary group of %s\n",
1788 info->info21.account_name.string);
1790 group_is_primary = true;
1793 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1796 if (group_is_primary) {
1797 d_fprintf(stderr, "Unable to delete group because some "
1798 "of it's members have it as primary group\n");
1799 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1800 goto done;
1803 /* remove all group members */
1804 for (i = 0; i < rids->count; i++)
1806 if (c->opt_verbose)
1807 d_printf("Remove group member %d...",
1808 rids->rids[i]);
1809 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1810 &group_pol,
1811 rids->rids[i]);
1813 if (NT_STATUS_IS_OK(result)) {
1814 if (c->opt_verbose)
1815 d_printf("ok\n");
1816 } else {
1817 if (c->opt_verbose)
1818 d_printf("failed\n");
1819 goto done;
1823 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1824 &group_pol);
1826 break;
1827 /* removing a local group is easier... */
1828 case SID_NAME_ALIAS:
1829 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1830 &domain_pol,
1831 MAXIMUM_ALLOWED_ACCESS,
1832 group_rids.ids[0],
1833 &group_pol);
1835 if (!NT_STATUS_IS_OK(result)) {
1836 d_fprintf(stderr, "Request open_alias failed\n");
1837 goto done;
1840 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1841 &group_pol);
1842 break;
1843 default:
1844 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1845 argv[0],sid_type_lookup(name_types.ids[0]));
1846 result = NT_STATUS_UNSUCCESSFUL;
1847 goto done;
1850 if (NT_STATUS_IS_OK(result)) {
1851 if (c->opt_verbose)
1852 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1853 } else {
1854 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1855 get_friendly_nt_error_msg(result));
1858 done:
1859 return result;
1863 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1865 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_group_delete_internals,
1866 argc,argv);
1869 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1871 NET_API_STATUS status;
1872 struct GROUP_INFO_1 info1;
1873 uint32_t parm_error = 0;
1875 if (argc != 1 || c->display_usage) {
1876 rpc_group_usage(c, argc, argv);
1877 return 0;
1880 ZERO_STRUCT(info1);
1882 info1.grpi1_name = argv[0];
1883 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1884 info1.grpi1_comment = c->opt_comment;
1887 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1889 if (status != 0) {
1890 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1891 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1892 status));
1893 return -1;
1894 } else {
1895 d_printf("Added group '%s'.\n", argv[0]);
1898 return 0;
1901 static NTSTATUS rpc_alias_add_internals(struct net_context *c,
1902 const DOM_SID *domain_sid,
1903 const char *domain_name,
1904 struct cli_state *cli,
1905 struct rpc_pipe_client *pipe_hnd,
1906 TALLOC_CTX *mem_ctx,
1907 int argc,
1908 const char **argv)
1910 POLICY_HND connect_pol, domain_pol, alias_pol;
1911 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1912 union samr_AliasInfo alias_info;
1913 struct lsa_String alias_name;
1914 uint32_t rid = 0;
1916 if (argc != 1 || c->display_usage) {
1917 rpc_group_usage(c, argc, argv);
1918 return NT_STATUS_OK;
1921 init_lsa_String(&alias_name, argv[0]);
1923 /* Get sam policy handle */
1925 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1926 pipe_hnd->desthost,
1927 MAXIMUM_ALLOWED_ACCESS,
1928 &connect_pol);
1929 if (!NT_STATUS_IS_OK(result)) goto done;
1931 /* Get domain policy handle */
1933 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1934 &connect_pol,
1935 MAXIMUM_ALLOWED_ACCESS,
1936 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1937 &domain_pol);
1938 if (!NT_STATUS_IS_OK(result)) goto done;
1940 /* Create the group */
1942 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
1943 &domain_pol,
1944 &alias_name,
1945 MAXIMUM_ALLOWED_ACCESS,
1946 &alias_pol,
1947 &rid);
1948 if (!NT_STATUS_IS_OK(result)) goto done;
1950 if (strlen(c->opt_comment) == 0) goto done;
1952 /* We've got a comment to set */
1954 init_lsa_String(&alias_info.description, c->opt_comment);
1956 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
1957 &alias_pol,
1959 &alias_info);
1961 if (!NT_STATUS_IS_OK(result)) goto done;
1963 done:
1964 if (NT_STATUS_IS_OK(result))
1965 DEBUG(5, ("add alias succeeded\n"));
1966 else
1967 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1969 return result;
1972 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1974 if (c->opt_localgroup)
1975 return run_rpc_command(c, NULL, PI_SAMR, 0,
1976 rpc_alias_add_internals,
1977 argc, argv);
1979 return rpc_group_add_internals(c, argc, argv);
1982 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1983 TALLOC_CTX *mem_ctx,
1984 const char *name,
1985 DOM_SID *sid,
1986 enum lsa_SidType *type)
1988 DOM_SID *sids = NULL;
1989 enum lsa_SidType *types = NULL;
1990 struct rpc_pipe_client *pipe_hnd;
1991 POLICY_HND lsa_pol;
1992 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1994 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1995 if (!pipe_hnd) {
1996 goto done;
1999 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2000 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2002 if (!NT_STATUS_IS_OK(result)) {
2003 goto done;
2006 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2007 &name, NULL, 1, &sids, &types);
2009 if (NT_STATUS_IS_OK(result)) {
2010 sid_copy(sid, &sids[0]);
2011 *type = types[0];
2014 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2016 done:
2017 if (pipe_hnd) {
2018 TALLOC_FREE(pipe_hnd);
2021 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2023 /* Try as S-1-5-whatever */
2025 DOM_SID tmp_sid;
2027 if (string_to_sid(&tmp_sid, name)) {
2028 sid_copy(sid, &tmp_sid);
2029 *type = SID_NAME_UNKNOWN;
2030 result = NT_STATUS_OK;
2034 return result;
2037 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2038 TALLOC_CTX *mem_ctx,
2039 const DOM_SID *group_sid,
2040 const char *member)
2042 POLICY_HND connect_pol, domain_pol;
2043 NTSTATUS result;
2044 uint32 group_rid;
2045 POLICY_HND group_pol;
2047 struct samr_Ids rids, rid_types;
2048 struct lsa_String lsa_acct_name;
2050 DOM_SID sid;
2052 sid_copy(&sid, group_sid);
2054 if (!sid_split_rid(&sid, &group_rid)) {
2055 return NT_STATUS_UNSUCCESSFUL;
2058 /* Get sam policy handle */
2059 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2060 pipe_hnd->desthost,
2061 MAXIMUM_ALLOWED_ACCESS,
2062 &connect_pol);
2063 if (!NT_STATUS_IS_OK(result)) {
2064 return result;
2067 /* Get domain policy handle */
2068 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2069 &connect_pol,
2070 MAXIMUM_ALLOWED_ACCESS,
2071 &sid,
2072 &domain_pol);
2073 if (!NT_STATUS_IS_OK(result)) {
2074 return result;
2077 init_lsa_String(&lsa_acct_name, member);
2079 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2080 &domain_pol,
2082 &lsa_acct_name,
2083 &rids,
2084 &rid_types);
2086 if (!NT_STATUS_IS_OK(result)) {
2087 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2088 goto done;
2091 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2092 &domain_pol,
2093 MAXIMUM_ALLOWED_ACCESS,
2094 group_rid,
2095 &group_pol);
2097 if (!NT_STATUS_IS_OK(result)) {
2098 goto done;
2101 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2102 &group_pol,
2103 rids.ids[0],
2104 0x0005); /* unknown flags */
2106 done:
2107 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2108 return result;
2111 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2112 TALLOC_CTX *mem_ctx,
2113 const DOM_SID *alias_sid,
2114 const char *member)
2116 POLICY_HND connect_pol, domain_pol;
2117 NTSTATUS result;
2118 uint32 alias_rid;
2119 POLICY_HND alias_pol;
2121 DOM_SID member_sid;
2122 enum lsa_SidType member_type;
2124 DOM_SID sid;
2126 sid_copy(&sid, alias_sid);
2128 if (!sid_split_rid(&sid, &alias_rid)) {
2129 return NT_STATUS_UNSUCCESSFUL;
2132 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2133 member, &member_sid, &member_type);
2135 if (!NT_STATUS_IS_OK(result)) {
2136 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2137 return result;
2140 /* Get sam policy handle */
2141 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2142 pipe_hnd->desthost,
2143 MAXIMUM_ALLOWED_ACCESS,
2144 &connect_pol);
2145 if (!NT_STATUS_IS_OK(result)) {
2146 goto done;
2149 /* Get domain policy handle */
2150 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2151 &connect_pol,
2152 MAXIMUM_ALLOWED_ACCESS,
2153 &sid,
2154 &domain_pol);
2155 if (!NT_STATUS_IS_OK(result)) {
2156 goto done;
2159 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2160 &domain_pol,
2161 MAXIMUM_ALLOWED_ACCESS,
2162 alias_rid,
2163 &alias_pol);
2165 if (!NT_STATUS_IS_OK(result)) {
2166 return result;
2169 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2170 &alias_pol,
2171 &member_sid);
2173 if (!NT_STATUS_IS_OK(result)) {
2174 return result;
2177 done:
2178 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2179 return result;
2182 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2183 const DOM_SID *domain_sid,
2184 const char *domain_name,
2185 struct cli_state *cli,
2186 struct rpc_pipe_client *pipe_hnd,
2187 TALLOC_CTX *mem_ctx,
2188 int argc,
2189 const char **argv)
2191 DOM_SID group_sid;
2192 enum lsa_SidType group_type;
2194 if (argc != 2 || c->display_usage) {
2195 d_printf("Usage:\n"
2196 "net rpc group addmem <group> <member>\n"
2197 " Add a member to a group\n"
2198 " group\tGroup to add member to\n"
2199 " member\tMember to add to group\n");
2200 return NT_STATUS_UNSUCCESSFUL;
2203 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2204 &group_sid, &group_type))) {
2205 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2206 return NT_STATUS_UNSUCCESSFUL;
2209 if (group_type == SID_NAME_DOM_GRP) {
2210 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2211 &group_sid, argv[1]);
2213 if (!NT_STATUS_IS_OK(result)) {
2214 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2215 argv[1], argv[0], nt_errstr(result));
2217 return result;
2220 if (group_type == SID_NAME_ALIAS) {
2221 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2222 &group_sid, argv[1]);
2224 if (!NT_STATUS_IS_OK(result)) {
2225 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2226 argv[1], argv[0], nt_errstr(result));
2228 return result;
2231 d_fprintf(stderr, "Can only add members to global or local groups "
2232 "which %s is not\n", argv[0]);
2234 return NT_STATUS_UNSUCCESSFUL;
2237 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2239 return run_rpc_command(c, NULL, PI_SAMR, 0,
2240 rpc_group_addmem_internals,
2241 argc, argv);
2244 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2245 struct rpc_pipe_client *pipe_hnd,
2246 TALLOC_CTX *mem_ctx,
2247 const DOM_SID *group_sid,
2248 const char *member)
2250 POLICY_HND connect_pol, domain_pol;
2251 NTSTATUS result;
2252 uint32 group_rid;
2253 POLICY_HND group_pol;
2255 struct samr_Ids rids, rid_types;
2256 struct lsa_String lsa_acct_name;
2258 DOM_SID sid;
2260 sid_copy(&sid, group_sid);
2262 if (!sid_split_rid(&sid, &group_rid))
2263 return NT_STATUS_UNSUCCESSFUL;
2265 /* Get sam policy handle */
2266 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2267 pipe_hnd->desthost,
2268 MAXIMUM_ALLOWED_ACCESS,
2269 &connect_pol);
2270 if (!NT_STATUS_IS_OK(result))
2271 return result;
2273 /* Get domain policy handle */
2274 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2275 &connect_pol,
2276 MAXIMUM_ALLOWED_ACCESS,
2277 &sid,
2278 &domain_pol);
2279 if (!NT_STATUS_IS_OK(result))
2280 return result;
2282 init_lsa_String(&lsa_acct_name, member);
2284 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2285 &domain_pol,
2287 &lsa_acct_name,
2288 &rids,
2289 &rid_types);
2290 if (!NT_STATUS_IS_OK(result)) {
2291 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2292 goto done;
2295 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2296 &domain_pol,
2297 MAXIMUM_ALLOWED_ACCESS,
2298 group_rid,
2299 &group_pol);
2301 if (!NT_STATUS_IS_OK(result))
2302 goto done;
2304 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2305 &group_pol,
2306 rids.ids[0]);
2308 done:
2309 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2310 return result;
2313 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2314 TALLOC_CTX *mem_ctx,
2315 const DOM_SID *alias_sid,
2316 const char *member)
2318 POLICY_HND connect_pol, domain_pol;
2319 NTSTATUS result;
2320 uint32 alias_rid;
2321 POLICY_HND alias_pol;
2323 DOM_SID member_sid;
2324 enum lsa_SidType member_type;
2326 DOM_SID sid;
2328 sid_copy(&sid, alias_sid);
2330 if (!sid_split_rid(&sid, &alias_rid))
2331 return NT_STATUS_UNSUCCESSFUL;
2333 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2334 member, &member_sid, &member_type);
2336 if (!NT_STATUS_IS_OK(result)) {
2337 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2338 return result;
2341 /* Get sam policy handle */
2342 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2343 pipe_hnd->desthost,
2344 MAXIMUM_ALLOWED_ACCESS,
2345 &connect_pol);
2346 if (!NT_STATUS_IS_OK(result)) {
2347 goto done;
2350 /* Get domain policy handle */
2351 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2352 &connect_pol,
2353 MAXIMUM_ALLOWED_ACCESS,
2354 &sid,
2355 &domain_pol);
2356 if (!NT_STATUS_IS_OK(result)) {
2357 goto done;
2360 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2361 &domain_pol,
2362 MAXIMUM_ALLOWED_ACCESS,
2363 alias_rid,
2364 &alias_pol);
2366 if (!NT_STATUS_IS_OK(result))
2367 return result;
2369 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2370 &alias_pol,
2371 &member_sid);
2373 if (!NT_STATUS_IS_OK(result))
2374 return result;
2376 done:
2377 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2378 return result;
2381 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2382 const DOM_SID *domain_sid,
2383 const char *domain_name,
2384 struct cli_state *cli,
2385 struct rpc_pipe_client *pipe_hnd,
2386 TALLOC_CTX *mem_ctx,
2387 int argc,
2388 const char **argv)
2390 DOM_SID group_sid;
2391 enum lsa_SidType group_type;
2393 if (argc != 2 || c->display_usage) {
2394 d_printf("Usage:\n"
2395 "net rpc group delmem <group> <member>\n"
2396 " Delete a member from a group\n"
2397 " group\tGroup to delete member from\n"
2398 " member\tMember to delete from group\n");
2399 return NT_STATUS_UNSUCCESSFUL;
2402 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2403 &group_sid, &group_type))) {
2404 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2405 return NT_STATUS_UNSUCCESSFUL;
2408 if (group_type == SID_NAME_DOM_GRP) {
2409 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2410 &group_sid, argv[1]);
2412 if (!NT_STATUS_IS_OK(result)) {
2413 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2414 argv[1], argv[0], nt_errstr(result));
2416 return result;
2419 if (group_type == SID_NAME_ALIAS) {
2420 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2421 &group_sid, argv[1]);
2423 if (!NT_STATUS_IS_OK(result)) {
2424 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2425 argv[1], argv[0], nt_errstr(result));
2427 return result;
2430 d_fprintf(stderr, "Can only delete members from global or local groups "
2431 "which %s is not\n", argv[0]);
2433 return NT_STATUS_UNSUCCESSFUL;
2436 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2438 return run_rpc_command(c, NULL, PI_SAMR, 0,
2439 rpc_group_delmem_internals,
2440 argc, argv);
2444 * List groups on a remote RPC server.
2446 * All parameters are provided by the run_rpc_command function, except for
2447 * argc, argv which are passes through.
2449 * @param domain_sid The domain sid acquired from the remote server.
2450 * @param cli A cli_state connected to the server.
2451 * @param mem_ctx Talloc context, destroyed on completion of the function.
2452 * @param argc Standard main() style argc.
2453 * @param argv Standard main() style argv. Initial components are already
2454 * stripped.
2456 * @return Normal NTSTATUS return.
2459 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2460 const DOM_SID *domain_sid,
2461 const char *domain_name,
2462 struct cli_state *cli,
2463 struct rpc_pipe_client *pipe_hnd,
2464 TALLOC_CTX *mem_ctx,
2465 int argc,
2466 const char **argv)
2468 POLICY_HND connect_pol, domain_pol;
2469 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2470 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2471 struct samr_SamArray *groups = NULL;
2472 bool global = false;
2473 bool local = false;
2474 bool builtin = false;
2476 if (c->display_usage) {
2477 d_printf("Usage:\n"
2478 "net rpc group list [global] [local] [builtin]\n"
2479 " List groups on RPC server\n"
2480 " global\tList global groups\n"
2481 " local\tList local groups\n"
2482 " builtin\tList builtin groups\n"
2483 " If none of global, local or builtin is "
2484 "specified, all three options are considered set\n");
2485 return NT_STATUS_OK;
2488 if (argc == 0) {
2489 global = true;
2490 local = true;
2491 builtin = true;
2494 for (i=0; i<argc; i++) {
2495 if (strequal(argv[i], "global"))
2496 global = true;
2498 if (strequal(argv[i], "local"))
2499 local = true;
2501 if (strequal(argv[i], "builtin"))
2502 builtin = true;
2505 /* Get sam policy handle */
2507 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2508 pipe_hnd->desthost,
2509 MAXIMUM_ALLOWED_ACCESS,
2510 &connect_pol);
2511 if (!NT_STATUS_IS_OK(result)) {
2512 goto done;
2515 /* Get domain policy handle */
2517 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2518 &connect_pol,
2519 MAXIMUM_ALLOWED_ACCESS,
2520 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2521 &domain_pol);
2522 if (!NT_STATUS_IS_OK(result)) {
2523 goto done;
2526 /* Query domain groups */
2527 if (c->opt_long_list_entries)
2528 d_printf("\nGroup name Comment"
2529 "\n-----------------------------\n");
2530 do {
2531 uint32_t max_size, total_size, returned_size;
2532 union samr_DispInfo info;
2534 if (!global) break;
2536 get_query_dispinfo_params(
2537 loop_count, &max_entries, &max_size);
2539 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2540 &domain_pol,
2542 start_idx,
2543 max_entries,
2544 max_size,
2545 &total_size,
2546 &returned_size,
2547 &info);
2548 num_entries = info.info3.count;
2549 start_idx += info.info3.count;
2551 if (!NT_STATUS_IS_OK(result) &&
2552 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2553 break;
2555 for (i = 0; i < num_entries; i++) {
2557 const char *group = NULL;
2558 const char *desc = NULL;
2560 group = info.info3.entries[i].account_name.string;
2561 desc = info.info3.entries[i].description.string;
2563 if (c->opt_long_list_entries)
2564 printf("%-21.21s %-50.50s\n",
2565 group, desc);
2566 else
2567 printf("%s\n", group);
2569 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2570 /* query domain aliases */
2571 start_idx = 0;
2572 do {
2573 if (!local) break;
2575 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2576 &domain_pol,
2577 &start_idx,
2578 &groups,
2579 0xffff,
2580 &num_entries);
2581 if (!NT_STATUS_IS_OK(result) &&
2582 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2583 break;
2585 for (i = 0; i < num_entries; i++) {
2587 const char *description = NULL;
2589 if (c->opt_long_list_entries) {
2591 POLICY_HND alias_pol;
2592 union samr_AliasInfo *info = NULL;
2594 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2595 &domain_pol,
2596 0x8,
2597 groups->entries[i].idx,
2598 &alias_pol))) &&
2599 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2600 &alias_pol,
2602 &info))) &&
2603 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2604 &alias_pol)))) {
2605 description = info->description.string;
2609 if (description != NULL) {
2610 printf("%-21.21s %-50.50s\n",
2611 groups->entries[i].name.string,
2612 description);
2613 } else {
2614 printf("%s\n", groups->entries[i].name.string);
2617 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2618 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2619 /* Get builtin policy handle */
2621 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2622 &connect_pol,
2623 MAXIMUM_ALLOWED_ACCESS,
2624 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2625 &domain_pol);
2626 if (!NT_STATUS_IS_OK(result)) {
2627 goto done;
2629 /* query builtin aliases */
2630 start_idx = 0;
2631 do {
2632 if (!builtin) break;
2634 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2635 &domain_pol,
2636 &start_idx,
2637 &groups,
2638 max_entries,
2639 &num_entries);
2640 if (!NT_STATUS_IS_OK(result) &&
2641 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2642 break;
2644 for (i = 0; i < num_entries; i++) {
2646 const char *description = NULL;
2648 if (c->opt_long_list_entries) {
2650 POLICY_HND alias_pol;
2651 union samr_AliasInfo *info = NULL;
2653 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2654 &domain_pol,
2655 0x8,
2656 groups->entries[i].idx,
2657 &alias_pol))) &&
2658 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2659 &alias_pol,
2661 &info))) &&
2662 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2663 &alias_pol)))) {
2664 description = info->description.string;
2668 if (description != NULL) {
2669 printf("%-21.21s %-50.50s\n",
2670 groups->entries[i].name.string,
2671 description);
2672 } else {
2673 printf("%s\n", groups->entries[i].name.string);
2676 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2678 done:
2679 return result;
2682 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2684 return run_rpc_command(c, NULL, PI_SAMR, 0,
2685 rpc_group_list_internals,
2686 argc, argv);
2689 static NTSTATUS rpc_list_group_members(struct net_context *c,
2690 struct rpc_pipe_client *pipe_hnd,
2691 TALLOC_CTX *mem_ctx,
2692 const char *domain_name,
2693 const DOM_SID *domain_sid,
2694 POLICY_HND *domain_pol,
2695 uint32 rid)
2697 NTSTATUS result;
2698 POLICY_HND group_pol;
2699 uint32 num_members, *group_rids;
2700 int i;
2701 struct samr_RidTypeArray *rids = NULL;
2702 struct lsa_Strings names;
2703 struct samr_Ids types;
2705 fstring sid_str;
2706 sid_to_fstring(sid_str, domain_sid);
2708 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2709 domain_pol,
2710 MAXIMUM_ALLOWED_ACCESS,
2711 rid,
2712 &group_pol);
2714 if (!NT_STATUS_IS_OK(result))
2715 return result;
2717 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2718 &group_pol,
2719 &rids);
2721 if (!NT_STATUS_IS_OK(result))
2722 return result;
2724 num_members = rids->count;
2725 group_rids = rids->rids;
2727 while (num_members > 0) {
2728 int this_time = 512;
2730 if (num_members < this_time)
2731 this_time = num_members;
2733 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2734 domain_pol,
2735 this_time,
2736 group_rids,
2737 &names,
2738 &types);
2740 if (!NT_STATUS_IS_OK(result))
2741 return result;
2743 /* We only have users as members, but make the output
2744 the same as the output of alias members */
2746 for (i = 0; i < this_time; i++) {
2748 if (c->opt_long_list_entries) {
2749 printf("%s-%d %s\\%s %d\n", sid_str,
2750 group_rids[i], domain_name,
2751 names.names[i].string,
2752 SID_NAME_USER);
2753 } else {
2754 printf("%s\\%s\n", domain_name,
2755 names.names[i].string);
2759 num_members -= this_time;
2760 group_rids += 512;
2763 return NT_STATUS_OK;
2766 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2767 struct rpc_pipe_client *pipe_hnd,
2768 TALLOC_CTX *mem_ctx,
2769 POLICY_HND *domain_pol,
2770 uint32 rid)
2772 NTSTATUS result;
2773 struct rpc_pipe_client *lsa_pipe;
2774 POLICY_HND alias_pol, lsa_pol;
2775 uint32 num_members;
2776 DOM_SID *alias_sids;
2777 char **domains;
2778 char **names;
2779 enum lsa_SidType *types;
2780 int i;
2781 struct lsa_SidArray sid_array;
2783 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2784 domain_pol,
2785 MAXIMUM_ALLOWED_ACCESS,
2786 rid,
2787 &alias_pol);
2789 if (!NT_STATUS_IS_OK(result))
2790 return result;
2792 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2793 &alias_pol,
2794 &sid_array);
2796 if (!NT_STATUS_IS_OK(result)) {
2797 d_fprintf(stderr, "Couldn't list alias members\n");
2798 return result;
2801 num_members = sid_array.num_sids;
2803 if (num_members == 0) {
2804 return NT_STATUS_OK;
2807 lsa_pipe = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2808 PI_LSARPC, &result);
2809 if (!lsa_pipe) {
2810 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2811 nt_errstr(result) );
2812 return result;
2815 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2816 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2818 if (!NT_STATUS_IS_OK(result)) {
2819 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2820 TALLOC_FREE(lsa_pipe);
2821 return result;
2824 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2825 if (!alias_sids) {
2826 d_fprintf(stderr, "Out of memory\n");
2827 TALLOC_FREE(lsa_pipe);
2828 return NT_STATUS_NO_MEMORY;
2831 for (i=0; i<num_members; i++) {
2832 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2835 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2836 num_members, alias_sids,
2837 &domains, &names, &types);
2839 if (!NT_STATUS_IS_OK(result) &&
2840 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2841 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2842 TALLOC_FREE(lsa_pipe);
2843 return result;
2846 for (i = 0; i < num_members; i++) {
2847 fstring sid_str;
2848 sid_to_fstring(sid_str, &alias_sids[i]);
2850 if (c->opt_long_list_entries) {
2851 printf("%s %s\\%s %d\n", sid_str,
2852 domains[i] ? domains[i] : "*unknown*",
2853 names[i] ? names[i] : "*unknown*", types[i]);
2854 } else {
2855 if (domains[i])
2856 printf("%s\\%s\n", domains[i], names[i]);
2857 else
2858 printf("%s\n", sid_str);
2862 TALLOC_FREE(lsa_pipe);
2863 return NT_STATUS_OK;
2866 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2867 const DOM_SID *domain_sid,
2868 const char *domain_name,
2869 struct cli_state *cli,
2870 struct rpc_pipe_client *pipe_hnd,
2871 TALLOC_CTX *mem_ctx,
2872 int argc,
2873 const char **argv)
2875 NTSTATUS result;
2876 POLICY_HND connect_pol, domain_pol;
2877 struct samr_Ids rids, rid_types;
2878 struct lsa_String lsa_acct_name;
2880 /* Get sam policy handle */
2882 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2883 pipe_hnd->desthost,
2884 MAXIMUM_ALLOWED_ACCESS,
2885 &connect_pol);
2887 if (!NT_STATUS_IS_OK(result))
2888 return result;
2890 /* Get domain policy handle */
2892 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2893 &connect_pol,
2894 MAXIMUM_ALLOWED_ACCESS,
2895 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2896 &domain_pol);
2898 if (!NT_STATUS_IS_OK(result))
2899 return result;
2901 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2903 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2904 &domain_pol,
2906 &lsa_acct_name,
2907 &rids,
2908 &rid_types);
2910 if (!NT_STATUS_IS_OK(result)) {
2912 /* Ok, did not find it in the global sam, try with builtin */
2914 DOM_SID sid_Builtin;
2916 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2918 sid_copy(&sid_Builtin, &global_sid_Builtin);
2920 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2921 &connect_pol,
2922 MAXIMUM_ALLOWED_ACCESS,
2923 &sid_Builtin,
2924 &domain_pol);
2926 if (!NT_STATUS_IS_OK(result)) {
2927 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2928 return result;
2931 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2932 &domain_pol,
2934 &lsa_acct_name,
2935 &rids,
2936 &rid_types);
2938 if (!NT_STATUS_IS_OK(result)) {
2939 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2940 return result;
2944 if (rids.count != 1) {
2945 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2946 return result;
2949 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2950 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2951 domain_sid, &domain_pol,
2952 rids.ids[0]);
2955 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2956 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2957 rids.ids[0]);
2960 return NT_STATUS_NO_SUCH_GROUP;
2963 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2965 if (argc != 1 || c->display_usage) {
2966 return rpc_group_usage(c, argc, argv);
2969 return run_rpc_command(c, NULL, PI_SAMR, 0,
2970 rpc_group_members_internals,
2971 argc, argv);
2974 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2976 NET_API_STATUS status;
2977 struct GROUP_INFO_0 g0;
2978 uint32_t parm_err;
2980 if (argc != 2) {
2981 d_printf("Usage: 'net rpc group rename group newname'\n");
2982 return -1;
2985 g0.grpi0_name = argv[1];
2987 status = NetGroupSetInfo(c->opt_host,
2988 argv[0],
2990 (uint8_t *)&g0,
2991 &parm_err);
2993 if (status != 0) {
2994 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2995 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2996 status));
2997 return -1;
3000 return 0;
3003 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3005 if (argc != 2 || c->display_usage) {
3006 return rpc_group_usage(c, argc, argv);
3009 return rpc_group_rename_internals(c, argc, argv);
3013 * 'net rpc group' entrypoint.
3014 * @param argc Standard main() style argc.
3015 * @param argv Standard main() style argv. Initial components are already
3016 * stripped.
3019 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3021 NET_API_STATUS status;
3023 struct functable func[] = {
3025 "add",
3026 rpc_group_add,
3027 NET_TRANSPORT_RPC,
3028 "Create specified group",
3029 "net rpc group add\n"
3030 " Create specified group"
3033 "delete",
3034 rpc_group_delete,
3035 NET_TRANSPORT_RPC,
3036 "Delete specified group",
3037 "net rpc group delete\n"
3038 " Delete specified group"
3041 "addmem",
3042 rpc_group_addmem,
3043 NET_TRANSPORT_RPC,
3044 "Add member to group",
3045 "net rpc group addmem\n"
3046 " Add member to group"
3049 "delmem",
3050 rpc_group_delmem,
3051 NET_TRANSPORT_RPC,
3052 "Remove member from group",
3053 "net rpc group delmem\n"
3054 " Remove member from group"
3057 "list",
3058 rpc_group_list,
3059 NET_TRANSPORT_RPC,
3060 "List groups",
3061 "net rpc group list\n"
3062 " List groups"
3065 "members",
3066 rpc_group_members,
3067 NET_TRANSPORT_RPC,
3068 "List group members",
3069 "net rpc group members\n"
3070 " List group members"
3073 "rename",
3074 rpc_group_rename,
3075 NET_TRANSPORT_RPC,
3076 "Rename group",
3077 "net rpc group rename\n"
3078 " Rename group"
3080 {NULL, NULL, 0, NULL, NULL}
3083 status = libnetapi_init(&c->netapi_ctx);
3084 if (status != 0) {
3085 return -1;
3087 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3088 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3090 if (argc == 0) {
3091 if (c->display_usage) {
3092 d_printf("Usage:\n");
3093 d_printf("net rpc group\n"
3094 " Alias for net rpc group list global local "
3095 "builtin\n");
3096 net_display_usage_from_functable(func);
3097 return 0;
3100 return run_rpc_command(c, NULL, PI_SAMR, 0,
3101 rpc_group_list_internals,
3102 argc, argv);
3105 return net_run_function(c, argc, argv, "net rpc group", func);
3108 /****************************************************************************/
3110 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3112 return net_share_usage(c, argc, argv);
3116 * Add a share on a remote RPC server.
3118 * All parameters are provided by the run_rpc_command function, except for
3119 * argc, argv which are passed through.
3121 * @param domain_sid The domain sid acquired from the remote server.
3122 * @param cli A cli_state connected to the server.
3123 * @param mem_ctx Talloc context, destroyed on completion of the function.
3124 * @param argc Standard main() style argc.
3125 * @param argv Standard main() style argv. Initial components are already
3126 * stripped.
3128 * @return Normal NTSTATUS return.
3130 static NTSTATUS rpc_share_add_internals(struct net_context *c,
3131 const DOM_SID *domain_sid,
3132 const char *domain_name,
3133 struct cli_state *cli,
3134 struct rpc_pipe_client *pipe_hnd,
3135 TALLOC_CTX *mem_ctx,int argc,
3136 const char **argv)
3138 WERROR result;
3139 NTSTATUS status;
3140 char *sharename;
3141 char *path;
3142 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3143 uint32 num_users=0, perms=0;
3144 char *password=NULL; /* don't allow a share password */
3145 uint32 level = 2;
3146 union srvsvc_NetShareInfo info;
3147 struct srvsvc_NetShareInfo2 info2;
3148 uint32_t parm_error = 0;
3150 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3151 return NT_STATUS_NO_MEMORY;
3154 path = strchr(sharename, '=');
3155 if (!path)
3156 return NT_STATUS_UNSUCCESSFUL;
3157 *path++ = '\0';
3159 info2.name = sharename;
3160 info2.type = type;
3161 info2.comment = c->opt_comment;
3162 info2.permissions = perms;
3163 info2.max_users = c->opt_maxusers;
3164 info2.current_users = num_users;
3165 info2.path = path;
3166 info2.password = password;
3168 info.info2 = &info2;
3170 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3171 pipe_hnd->desthost,
3172 level,
3173 &info,
3174 &parm_error,
3175 &result);
3176 return status;
3179 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3181 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3182 return rpc_share_usage(c, argc, argv);
3184 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3185 rpc_share_add_internals,
3186 argc, argv);
3190 * Delete a share on a remote RPC server.
3192 * All parameters are provided by the run_rpc_command function, except for
3193 * argc, argv which are passed through.
3195 * @param domain_sid The domain sid acquired from the remote server.
3196 * @param cli A cli_state connected to the server.
3197 * @param mem_ctx Talloc context, destroyed on completion of the function.
3198 * @param argc Standard main() style argc.
3199 * @param argv Standard main() style argv. Initial components are already
3200 * stripped.
3202 * @return Normal NTSTATUS return.
3204 static NTSTATUS rpc_share_del_internals(struct net_context *c,
3205 const DOM_SID *domain_sid,
3206 const char *domain_name,
3207 struct cli_state *cli,
3208 struct rpc_pipe_client *pipe_hnd,
3209 TALLOC_CTX *mem_ctx,
3210 int argc,
3211 const char **argv)
3213 WERROR result;
3215 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3216 pipe_hnd->desthost,
3217 argv[0],
3219 &result);
3223 * Delete a share on a remote RPC server.
3225 * @param domain_sid The domain sid acquired from the remote server.
3226 * @param argc Standard main() style argc.
3227 * @param argv Standard main() style argv. Initial components are already
3228 * stripped.
3230 * @return A shell status integer (0 for success).
3232 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3234 if (argc < 1 || c->display_usage) {
3235 return rpc_share_usage(c, argc, argv);
3237 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3238 rpc_share_del_internals,
3239 argc, argv);
3243 * Formatted print of share info
3245 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3248 static void display_share_info_1(struct net_context *c,
3249 struct srvsvc_NetShareInfo1 *r)
3251 if (c->opt_long_list_entries) {
3252 d_printf("%-12s %-8.8s %-50s\n",
3253 r->name,
3254 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3255 r->comment);
3256 } else {
3257 d_printf("%s\n", r->name);
3261 static WERROR get_share_info(struct net_context *c,
3262 struct rpc_pipe_client *pipe_hnd,
3263 TALLOC_CTX *mem_ctx,
3264 uint32 level,
3265 int argc,
3266 const char **argv,
3267 struct srvsvc_NetShareInfoCtr *info_ctr)
3269 WERROR result;
3270 NTSTATUS status;
3271 union srvsvc_NetShareInfo info;
3273 /* no specific share requested, enumerate all */
3274 if (argc == 0) {
3276 uint32_t preferred_len = 0xffffffff;
3277 uint32_t total_entries = 0;
3278 uint32_t resume_handle = 0;
3280 info_ctr->level = level;
3282 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3283 pipe_hnd->desthost,
3284 info_ctr,
3285 preferred_len,
3286 &total_entries,
3287 &resume_handle,
3288 &result);
3289 return result;
3292 /* request just one share */
3293 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3294 pipe_hnd->desthost,
3295 argv[0],
3296 level,
3297 &info,
3298 &result);
3300 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3301 goto done;
3304 /* construct ctr */
3305 ZERO_STRUCTP(info_ctr);
3307 info_ctr->level = level;
3309 switch (level) {
3310 case 1:
3312 struct srvsvc_NetShareCtr1 *ctr1;
3314 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3315 W_ERROR_HAVE_NO_MEMORY(ctr1);
3317 ctr1->count = 1;
3318 ctr1->array = info.info1;
3320 info_ctr->ctr.ctr1 = ctr1;
3322 case 2:
3324 struct srvsvc_NetShareCtr2 *ctr2;
3326 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3327 W_ERROR_HAVE_NO_MEMORY(ctr2);
3329 ctr2->count = 1;
3330 ctr2->array = info.info2;
3332 info_ctr->ctr.ctr2 = ctr2;
3334 case 502:
3336 struct srvsvc_NetShareCtr502 *ctr502;
3338 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3339 W_ERROR_HAVE_NO_MEMORY(ctr502);
3341 ctr502->count = 1;
3342 ctr502->array = info.info502;
3344 info_ctr->ctr.ctr502 = ctr502;
3346 } /* switch */
3347 done:
3348 return result;
3352 * List shares on a remote RPC server.
3354 * All parameters are provided by the run_rpc_command function, except for
3355 * argc, argv which are passed through.
3357 * @param domain_sid The domain sid acquired from the remote server.
3358 * @param cli A cli_state connected to the server.
3359 * @param mem_ctx Talloc context, destroyed on completion of the function.
3360 * @param argc Standard main() style argc.
3361 * @param argv Standard main() style argv. Initial components are already
3362 * stripped.
3364 * @return Normal NTSTATUS return.
3367 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3368 const DOM_SID *domain_sid,
3369 const char *domain_name,
3370 struct cli_state *cli,
3371 struct rpc_pipe_client *pipe_hnd,
3372 TALLOC_CTX *mem_ctx,
3373 int argc,
3374 const char **argv)
3376 struct srvsvc_NetShareInfoCtr info_ctr;
3377 struct srvsvc_NetShareCtr1 ctr1;
3378 WERROR result;
3379 uint32 i, level = 1;
3381 ZERO_STRUCT(info_ctr);
3382 ZERO_STRUCT(ctr1);
3384 info_ctr.level = 1;
3385 info_ctr.ctr.ctr1 = &ctr1;
3387 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3388 &info_ctr);
3389 if (!W_ERROR_IS_OK(result))
3390 goto done;
3392 /* Display results */
3394 if (c->opt_long_list_entries) {
3395 d_printf(
3396 "\nEnumerating shared resources (exports) on remote server:\n\n"
3397 "\nShare name Type Description\n"
3398 "---------- ---- -----------\n");
3400 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3401 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3402 done:
3403 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3406 /***
3407 * 'net rpc share list' entrypoint.
3408 * @param argc Standard main() style argc.
3409 * @param argv Standard main() style argv. Initial components are already
3410 * stripped.
3412 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3414 if (c->display_usage) {
3415 d_printf("Usage\n"
3416 "net rpc share list\n"
3417 " List shares on remote server\n");
3418 return 0;
3421 return run_rpc_command(c, NULL, PI_SRVSVC, 0, rpc_share_list_internals,
3422 argc, argv);
3425 static bool check_share_availability(struct cli_state *cli, const char *netname)
3427 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3428 d_printf("skipping [%s]: not a file share.\n", netname);
3429 return false;
3432 if (!cli_tdis(cli))
3433 return false;
3435 return true;
3438 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3439 const char *netname, uint32 type)
3441 /* only support disk shares */
3442 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3443 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3444 return false;
3447 /* skip builtin shares */
3448 /* FIXME: should print$ be added too ? */
3449 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3450 strequal(netname,"global"))
3451 return false;
3453 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3454 printf("excluding [%s]\n", netname);
3455 return false;
3458 return check_share_availability(cli, netname);
3462 * Migrate shares from a remote RPC server to the local RPC server.
3464 * All parameters are provided by the run_rpc_command function, except for
3465 * argc, argv which are passed through.
3467 * @param domain_sid The domain sid acquired from the remote server.
3468 * @param cli A cli_state connected to the server.
3469 * @param mem_ctx Talloc context, destroyed on completion of the function.
3470 * @param argc Standard main() style argc.
3471 * @param argv Standard main() style argv. Initial components are already
3472 * stripped.
3474 * @return Normal NTSTATUS return.
3477 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3478 const DOM_SID *domain_sid,
3479 const char *domain_name,
3480 struct cli_state *cli,
3481 struct rpc_pipe_client *pipe_hnd,
3482 TALLOC_CTX *mem_ctx,
3483 int argc,
3484 const char **argv)
3486 WERROR result;
3487 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3488 struct srvsvc_NetShareInfoCtr ctr_src;
3489 uint32 i;
3490 struct rpc_pipe_client *srvsvc_pipe = NULL;
3491 struct cli_state *cli_dst = NULL;
3492 uint32 level = 502; /* includes secdesc */
3493 uint32_t parm_error = 0;
3495 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3496 &ctr_src);
3497 if (!W_ERROR_IS_OK(result))
3498 goto done;
3500 /* connect destination PI_SRVSVC */
3501 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3502 if (!NT_STATUS_IS_OK(nt_status))
3503 return nt_status;
3506 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3508 union srvsvc_NetShareInfo info;
3509 struct srvsvc_NetShareInfo502 info502 =
3510 ctr_src.ctr.ctr502->array[i];
3512 /* reset error-code */
3513 nt_status = NT_STATUS_UNSUCCESSFUL;
3515 if (!check_share_sanity(c, cli, info502.name, info502.type))
3516 continue;
3518 /* finally add the share on the dst server */
3520 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3521 info502.name, info502.path, info502.comment);
3523 info.info502 = &info502;
3525 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3526 srvsvc_pipe->desthost,
3527 502,
3528 &info,
3529 &parm_error,
3530 &result);
3532 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3533 printf(" [%s] does already exist\n",
3534 info502.name);
3535 continue;
3538 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3539 printf("cannot add share: %s\n", dos_errstr(result));
3540 goto done;
3545 nt_status = NT_STATUS_OK;
3547 done:
3548 if (cli_dst) {
3549 cli_shutdown(cli_dst);
3552 return nt_status;
3557 * Migrate shares from a RPC server to another.
3559 * @param argc Standard main() style argc.
3560 * @param argv Standard main() style argv. Initial components are already
3561 * stripped.
3563 * @return A shell status integer (0 for success).
3565 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3566 const char **argv)
3568 if (c->display_usage) {
3569 d_printf("Usage:\n"
3570 "net rpc share migrate shares\n"
3571 " Migrate shares to local server\n");
3572 return 0;
3575 if (!c->opt_host) {
3576 printf("no server to migrate\n");
3577 return -1;
3580 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3581 rpc_share_migrate_shares_internals,
3582 argc, argv);
3586 * Copy a file/dir
3588 * @param f file_info
3589 * @param mask current search mask
3590 * @param state arg-pointer
3593 static void copy_fn(const char *mnt, file_info *f,
3594 const char *mask, void *state)
3596 static NTSTATUS nt_status;
3597 static struct copy_clistate *local_state;
3598 static fstring filename, new_mask;
3599 fstring dir;
3600 char *old_dir;
3601 struct net_context *c;
3603 local_state = (struct copy_clistate *)state;
3604 nt_status = NT_STATUS_UNSUCCESSFUL;
3606 c = local_state->c;
3608 if (strequal(f->name, ".") || strequal(f->name, ".."))
3609 return;
3611 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3613 /* DIRECTORY */
3614 if (f->mode & aDIR) {
3616 DEBUG(3,("got dir: %s\n", f->name));
3618 fstrcpy(dir, local_state->cwd);
3619 fstrcat(dir, "\\");
3620 fstrcat(dir, f->name);
3622 switch (net_mode_share)
3624 case NET_MODE_SHARE_MIGRATE:
3625 /* create that directory */
3626 nt_status = net_copy_file(c, local_state->mem_ctx,
3627 local_state->cli_share_src,
3628 local_state->cli_share_dst,
3629 dir, dir,
3630 c->opt_acls? true : false,
3631 c->opt_attrs? true : false,
3632 c->opt_timestamps? true:false,
3633 false);
3634 break;
3635 default:
3636 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3637 return;
3640 if (!NT_STATUS_IS_OK(nt_status))
3641 printf("could not handle dir %s: %s\n",
3642 dir, nt_errstr(nt_status));
3644 /* search below that directory */
3645 fstrcpy(new_mask, dir);
3646 fstrcat(new_mask, "\\*");
3648 old_dir = local_state->cwd;
3649 local_state->cwd = dir;
3650 if (!sync_files(local_state, new_mask))
3651 printf("could not handle files\n");
3652 local_state->cwd = old_dir;
3654 return;
3658 /* FILE */
3659 fstrcpy(filename, local_state->cwd);
3660 fstrcat(filename, "\\");
3661 fstrcat(filename, f->name);
3663 DEBUG(3,("got file: %s\n", filename));
3665 switch (net_mode_share)
3667 case NET_MODE_SHARE_MIGRATE:
3668 nt_status = net_copy_file(c, local_state->mem_ctx,
3669 local_state->cli_share_src,
3670 local_state->cli_share_dst,
3671 filename, filename,
3672 c->opt_acls? true : false,
3673 c->opt_attrs? true : false,
3674 c->opt_timestamps? true: false,
3675 true);
3676 break;
3677 default:
3678 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3679 return;
3682 if (!NT_STATUS_IS_OK(nt_status))
3683 printf("could not handle file %s: %s\n",
3684 filename, nt_errstr(nt_status));
3689 * sync files, can be called recursivly to list files
3690 * and then call copy_fn for each file
3692 * @param cp_clistate pointer to the copy_clistate we work with
3693 * @param mask the current search mask
3695 * @return Boolean result
3697 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3699 struct cli_state *targetcli;
3700 char *targetpath = NULL;
3702 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3704 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3705 mask, &targetcli, &targetpath ) ) {
3706 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3707 mask, cli_errstr(cp_clistate->cli_share_src));
3708 return false;
3711 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3712 d_fprintf(stderr, "listing %s failed with error: %s\n",
3713 mask, cli_errstr(targetcli));
3714 return false;
3717 return true;
3722 * Set the top level directory permissions before we do any further copies.
3723 * Should set up ACL inheritance.
3726 bool copy_top_level_perms(struct net_context *c,
3727 struct copy_clistate *cp_clistate,
3728 const char *sharename)
3730 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3732 switch (net_mode_share) {
3733 case NET_MODE_SHARE_MIGRATE:
3734 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3735 nt_status = net_copy_fileattr(c,
3736 cp_clistate->mem_ctx,
3737 cp_clistate->cli_share_src,
3738 cp_clistate->cli_share_dst,
3739 "\\", "\\",
3740 c->opt_acls? true : false,
3741 c->opt_attrs? true : false,
3742 c->opt_timestamps? true: false,
3743 false);
3744 break;
3745 default:
3746 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3747 break;
3750 if (!NT_STATUS_IS_OK(nt_status)) {
3751 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3752 sharename, nt_errstr(nt_status));
3753 return false;
3756 return true;
3760 * Sync all files inside a remote share to another share (over smb).
3762 * All parameters are provided by the run_rpc_command function, except for
3763 * argc, argv which are passed through.
3765 * @param domain_sid The domain sid acquired from the remote server.
3766 * @param cli A cli_state connected to the server.
3767 * @param mem_ctx Talloc context, destroyed on completion of the function.
3768 * @param argc Standard main() style argc.
3769 * @param argv Standard main() style argv. Initial components are already
3770 * stripped.
3772 * @return Normal NTSTATUS return.
3775 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3776 const DOM_SID *domain_sid,
3777 const char *domain_name,
3778 struct cli_state *cli,
3779 struct rpc_pipe_client *pipe_hnd,
3780 TALLOC_CTX *mem_ctx,
3781 int argc,
3782 const char **argv)
3784 WERROR result;
3785 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3786 struct srvsvc_NetShareInfoCtr ctr_src;
3787 uint32 i;
3788 uint32 level = 502;
3789 struct copy_clistate cp_clistate;
3790 bool got_src_share = false;
3791 bool got_dst_share = false;
3792 const char *mask = "\\*";
3793 char *dst = NULL;
3795 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3796 if (dst == NULL) {
3797 nt_status = NT_STATUS_NO_MEMORY;
3798 goto done;
3801 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3802 &ctr_src);
3804 if (!W_ERROR_IS_OK(result))
3805 goto done;
3807 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3809 struct srvsvc_NetShareInfo502 info502 =
3810 ctr_src.ctr.ctr502->array[i];
3812 if (!check_share_sanity(c, cli, info502.name, info502.type))
3813 continue;
3815 /* one might not want to mirror whole discs :) */
3816 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3817 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3818 continue;
3821 switch (net_mode_share)
3823 case NET_MODE_SHARE_MIGRATE:
3824 printf("syncing");
3825 break;
3826 default:
3827 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3828 break;
3830 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3831 info502.name,
3832 c->opt_acls ? "including" : "without",
3833 c->opt_attrs ? "including" : "without",
3834 c->opt_timestamps ? "(preserving timestamps)" : "");
3836 cp_clistate.mem_ctx = mem_ctx;
3837 cp_clistate.cli_share_src = NULL;
3838 cp_clistate.cli_share_dst = NULL;
3839 cp_clistate.cwd = NULL;
3840 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3841 cp_clistate.c = c;
3843 /* open share source */
3844 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3845 &cli->dest_ss, cli->desthost,
3846 info502.name, "A:");
3847 if (!NT_STATUS_IS_OK(nt_status))
3848 goto done;
3850 got_src_share = true;
3852 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3853 /* open share destination */
3854 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3855 NULL, dst, info502.name, "A:");
3856 if (!NT_STATUS_IS_OK(nt_status))
3857 goto done;
3859 got_dst_share = true;
3862 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3863 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3864 nt_status = NT_STATUS_UNSUCCESSFUL;
3865 goto done;
3868 if (!sync_files(&cp_clistate, mask)) {
3869 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3870 nt_status = NT_STATUS_UNSUCCESSFUL;
3871 goto done;
3875 nt_status = NT_STATUS_OK;
3877 done:
3879 if (got_src_share)
3880 cli_shutdown(cp_clistate.cli_share_src);
3882 if (got_dst_share)
3883 cli_shutdown(cp_clistate.cli_share_dst);
3885 SAFE_FREE(dst);
3886 return nt_status;
3890 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3892 if (c->display_usage) {
3893 d_printf("Usage:\n"
3894 "net share migrate files\n"
3895 " Migrate files to local server\n");
3896 return 0;
3899 if (!c->opt_host) {
3900 d_printf("no server to migrate\n");
3901 return -1;
3904 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3905 rpc_share_migrate_files_internals,
3906 argc, argv);
3910 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3912 * All parameters are provided by the run_rpc_command function, except for
3913 * argc, argv which are passed through.
3915 * @param domain_sid The domain sid acquired from the remote server.
3916 * @param cli A cli_state connected to the server.
3917 * @param mem_ctx Talloc context, destroyed on completion of the function.
3918 * @param argc Standard main() style argc.
3919 * @param argv Standard main() style argv. Initial components are already
3920 * stripped.
3922 * @return Normal NTSTATUS return.
3925 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3926 const DOM_SID *domain_sid,
3927 const char *domain_name,
3928 struct cli_state *cli,
3929 struct rpc_pipe_client *pipe_hnd,
3930 TALLOC_CTX *mem_ctx,
3931 int argc,
3932 const char **argv)
3934 WERROR result;
3935 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3936 struct srvsvc_NetShareInfoCtr ctr_src;
3937 union srvsvc_NetShareInfo info;
3938 uint32 i;
3939 struct rpc_pipe_client *srvsvc_pipe = NULL;
3940 struct cli_state *cli_dst = NULL;
3941 uint32 level = 502; /* includes secdesc */
3942 uint32_t parm_error = 0;
3944 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3945 &ctr_src);
3947 if (!W_ERROR_IS_OK(result))
3948 goto done;
3950 /* connect destination PI_SRVSVC */
3951 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3952 if (!NT_STATUS_IS_OK(nt_status))
3953 return nt_status;
3956 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3958 struct srvsvc_NetShareInfo502 info502 =
3959 ctr_src.ctr.ctr502->array[i];
3961 /* reset error-code */
3962 nt_status = NT_STATUS_UNSUCCESSFUL;
3964 if (!check_share_sanity(c, cli, info502.name, info502.type))
3965 continue;
3967 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3968 info502.name, info502.path, info502.comment);
3970 if (c->opt_verbose)
3971 display_sec_desc(info502.sd_buf.sd);
3973 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3974 info.info502 = &info502;
3976 /* finally modify the share on the dst server */
3977 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3978 srvsvc_pipe->desthost,
3979 info502.name,
3980 level,
3981 &info,
3982 &parm_error,
3983 &result);
3984 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3985 printf("cannot set share-acl: %s\n", dos_errstr(result));
3986 goto done;
3991 nt_status = NT_STATUS_OK;
3993 done:
3994 if (cli_dst) {
3995 cli_shutdown(cli_dst);
3998 return nt_status;
4003 * Migrate share-acls from a RPC server to another.
4005 * @param argc Standard main() style argc.
4006 * @param argv Standard main() style argv. Initial components are already
4007 * stripped.
4009 * @return A shell status integer (0 for success).
4011 static int rpc_share_migrate_security(struct net_context *c, int argc,
4012 const char **argv)
4014 if (c->display_usage) {
4015 d_printf("Usage:\n"
4016 "net rpc share migrate security\n"
4017 " Migrate share-acls to local server\n");
4018 return 0;
4021 if (!c->opt_host) {
4022 d_printf("no server to migrate\n");
4023 return -1;
4026 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4027 rpc_share_migrate_security_internals,
4028 argc, argv);
4032 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4033 * from one server to another.
4035 * @param argc Standard main() style argc.
4036 * @param argv Standard main() style argv. Initial components are already
4037 * stripped.
4039 * @return A shell status integer (0 for success).
4042 static int rpc_share_migrate_all(struct net_context *c, int argc,
4043 const char **argv)
4045 int ret;
4047 if (c->display_usage) {
4048 d_printf("Usage:\n"
4049 "net rpc share migrate all\n"
4050 " Migrates shares including all share settings\n");
4051 return 0;
4054 if (!c->opt_host) {
4055 d_printf("no server to migrate\n");
4056 return -1;
4059 /* order is important. we don't want to be locked out by the share-acl
4060 * before copying files - gd */
4062 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
4063 rpc_share_migrate_shares_internals, argc, argv);
4064 if (ret)
4065 return ret;
4067 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
4068 rpc_share_migrate_files_internals, argc, argv);
4069 if (ret)
4070 return ret;
4072 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4073 rpc_share_migrate_security_internals, argc,
4074 argv);
4079 * 'net rpc share migrate' entrypoint.
4080 * @param argc Standard main() style argc.
4081 * @param argv Standard main() style argv. Initial components are already
4082 * stripped.
4084 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4087 struct functable func[] = {
4089 "all",
4090 rpc_share_migrate_all,
4091 NET_TRANSPORT_RPC,
4092 "Migrate shares from remote to local server",
4093 "net rpc share migrate all\n"
4094 " Migrate shares from remote to local server"
4097 "files",
4098 rpc_share_migrate_files,
4099 NET_TRANSPORT_RPC,
4100 "Migrate files from remote to local server",
4101 "net rpc share migrate files\n"
4102 " Migrate files from remote to local server"
4105 "security",
4106 rpc_share_migrate_security,
4107 NET_TRANSPORT_RPC,
4108 "Migrate share-ACLs from remote to local server",
4109 "net rpc share migrate security\n"
4110 " Migrate share-ACLs from remote to local server"
4113 "shares",
4114 rpc_share_migrate_shares,
4115 NET_TRANSPORT_RPC,
4116 "Migrate shares from remote to local server",
4117 "net rpc share migrate shares\n"
4118 " Migrate shares from remote to local server"
4120 {NULL, NULL, 0, NULL, NULL}
4123 net_mode_share = NET_MODE_SHARE_MIGRATE;
4125 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4128 struct full_alias {
4129 DOM_SID sid;
4130 uint32 num_members;
4131 DOM_SID *members;
4134 static int num_server_aliases;
4135 static struct full_alias *server_aliases;
4138 * Add an alias to the static list.
4140 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4142 if (server_aliases == NULL)
4143 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4145 server_aliases[num_server_aliases] = *alias;
4146 num_server_aliases += 1;
4150 * For a specific domain on the server, fetch all the aliases
4151 * and their members. Add all of them to the server_aliases.
4154 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4155 TALLOC_CTX *mem_ctx,
4156 POLICY_HND *connect_pol,
4157 const DOM_SID *domain_sid)
4159 uint32 start_idx, max_entries, num_entries, i;
4160 struct samr_SamArray *groups = NULL;
4161 NTSTATUS result;
4162 POLICY_HND domain_pol;
4164 /* Get domain policy handle */
4166 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4167 connect_pol,
4168 MAXIMUM_ALLOWED_ACCESS,
4169 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4170 &domain_pol);
4171 if (!NT_STATUS_IS_OK(result))
4172 return result;
4174 start_idx = 0;
4175 max_entries = 250;
4177 do {
4178 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4179 &domain_pol,
4180 &start_idx,
4181 &groups,
4182 max_entries,
4183 &num_entries);
4184 for (i = 0; i < num_entries; i++) {
4186 POLICY_HND alias_pol;
4187 struct full_alias alias;
4188 struct lsa_SidArray sid_array;
4189 int j;
4191 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4192 &domain_pol,
4193 MAXIMUM_ALLOWED_ACCESS,
4194 groups->entries[i].idx,
4195 &alias_pol);
4196 if (!NT_STATUS_IS_OK(result))
4197 goto done;
4199 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4200 &alias_pol,
4201 &sid_array);
4202 if (!NT_STATUS_IS_OK(result))
4203 goto done;
4205 alias.num_members = sid_array.num_sids;
4207 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4208 if (!NT_STATUS_IS_OK(result))
4209 goto done;
4211 alias.members = NULL;
4213 if (alias.num_members > 0) {
4214 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4216 for (j = 0; j < alias.num_members; j++)
4217 sid_copy(&alias.members[j],
4218 sid_array.sids[j].sid);
4221 sid_copy(&alias.sid, domain_sid);
4222 sid_append_rid(&alias.sid, groups->entries[i].idx);
4224 push_alias(mem_ctx, &alias);
4226 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4228 result = NT_STATUS_OK;
4230 done:
4231 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4233 return result;
4237 * Dump server_aliases as names for debugging purposes.
4240 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4241 const DOM_SID *domain_sid,
4242 const char *domain_name,
4243 struct cli_state *cli,
4244 struct rpc_pipe_client *pipe_hnd,
4245 TALLOC_CTX *mem_ctx,
4246 int argc,
4247 const char **argv)
4249 int i;
4250 NTSTATUS result;
4251 POLICY_HND lsa_pol;
4253 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4254 SEC_RIGHTS_MAXIMUM_ALLOWED,
4255 &lsa_pol);
4256 if (!NT_STATUS_IS_OK(result))
4257 return result;
4259 for (i=0; i<num_server_aliases; i++) {
4260 char **names;
4261 char **domains;
4262 enum lsa_SidType *types;
4263 int j;
4265 struct full_alias *alias = &server_aliases[i];
4267 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4268 &alias->sid,
4269 &domains, &names, &types);
4270 if (!NT_STATUS_IS_OK(result))
4271 continue;
4273 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4275 if (alias->num_members == 0) {
4276 DEBUG(1, ("\n"));
4277 continue;
4280 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4281 alias->num_members,
4282 alias->members,
4283 &domains, &names, &types);
4285 if (!NT_STATUS_IS_OK(result) &&
4286 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4287 continue;
4289 for (j=0; j<alias->num_members; j++)
4290 DEBUG(1, ("%s\\%s (%d); ",
4291 domains[j] ? domains[j] : "*unknown*",
4292 names[j] ? names[j] : "*unknown*",types[j]));
4293 DEBUG(1, ("\n"));
4296 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4298 return NT_STATUS_OK;
4302 * Fetch a list of all server aliases and their members into
4303 * server_aliases.
4306 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4307 const DOM_SID *domain_sid,
4308 const char *domain_name,
4309 struct cli_state *cli,
4310 struct rpc_pipe_client *pipe_hnd,
4311 TALLOC_CTX *mem_ctx,
4312 int argc,
4313 const char **argv)
4315 NTSTATUS result;
4316 POLICY_HND connect_pol;
4318 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4319 pipe_hnd->desthost,
4320 MAXIMUM_ALLOWED_ACCESS,
4321 &connect_pol);
4323 if (!NT_STATUS_IS_OK(result))
4324 goto done;
4326 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4327 &global_sid_Builtin);
4329 if (!NT_STATUS_IS_OK(result))
4330 goto done;
4332 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4333 domain_sid);
4335 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4336 done:
4337 return result;
4340 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4342 token->num_sids = 4;
4344 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4345 d_fprintf(stderr, "malloc failed\n");
4346 token->num_sids = 0;
4347 return;
4350 token->user_sids[0] = *user_sid;
4351 sid_copy(&token->user_sids[1], &global_sid_World);
4352 sid_copy(&token->user_sids[2], &global_sid_Network);
4353 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4356 static void free_user_token(NT_USER_TOKEN *token)
4358 SAFE_FREE(token->user_sids);
4361 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4363 int i;
4365 for (i=0; i<token->num_sids; i++) {
4366 if (sid_compare(sid, &token->user_sids[i]) == 0)
4367 return true;
4369 return false;
4372 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4374 if (is_sid_in_token(token, sid))
4375 return;
4377 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4378 if (!token->user_sids) {
4379 return;
4382 sid_copy(&token->user_sids[token->num_sids], sid);
4384 token->num_sids += 1;
4387 struct user_token {
4388 fstring name;
4389 NT_USER_TOKEN token;
4392 static void dump_user_token(struct user_token *token)
4394 int i;
4396 d_printf("%s\n", token->name);
4398 for (i=0; i<token->token.num_sids; i++) {
4399 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4403 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4405 int i;
4407 for (i=0; i<alias->num_members; i++) {
4408 if (sid_compare(sid, &alias->members[i]) == 0)
4409 return true;
4412 return false;
4415 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4417 int i;
4419 for (i=0; i<num_server_aliases; i++) {
4420 if (is_alias_member(&sid, &server_aliases[i]))
4421 add_sid_to_token(token, &server_aliases[i].sid);
4426 * We got a user token with all the SIDs we can know about without asking the
4427 * server directly. These are the user and domain group sids. All of these can
4428 * be members of aliases. So scan the list of aliases for each of the SIDs and
4429 * add them to the token.
4432 static void collect_alias_memberships(NT_USER_TOKEN *token)
4434 int num_global_sids = token->num_sids;
4435 int i;
4437 for (i=0; i<num_global_sids; i++) {
4438 collect_sid_memberships(token, token->user_sids[i]);
4442 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4444 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4445 enum wbcSidType type;
4446 fstring full_name;
4447 struct wbcDomainSid wsid;
4448 char *sid_str = NULL;
4449 DOM_SID user_sid;
4450 uint32_t num_groups;
4451 gid_t *groups = NULL;
4452 uint32_t i;
4454 fstr_sprintf(full_name, "%s%c%s",
4455 domain, *lp_winbind_separator(), user);
4457 /* First let's find out the user sid */
4459 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4461 if (!WBC_ERROR_IS_OK(wbc_status)) {
4462 DEBUG(1, ("winbind could not find %s: %s\n",
4463 full_name, wbcErrorString(wbc_status)));
4464 return false;
4467 wbc_status = wbcSidToString(&wsid, &sid_str);
4468 if (!WBC_ERROR_IS_OK(wbc_status)) {
4469 return false;
4472 if (type != SID_NAME_USER) {
4473 wbcFreeMemory(sid_str);
4474 DEBUG(1, ("%s is not a user\n", full_name));
4475 return false;
4478 string_to_sid(&user_sid, sid_str);
4479 wbcFreeMemory(sid_str);
4480 sid_str = NULL;
4482 init_user_token(token, &user_sid);
4484 /* And now the groups winbind knows about */
4486 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4487 if (!WBC_ERROR_IS_OK(wbc_status)) {
4488 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4489 full_name, wbcErrorString(wbc_status)));
4490 return false;
4493 for (i = 0; i < num_groups; i++) {
4494 gid_t gid = groups[i];
4495 DOM_SID sid;
4497 wbc_status = wbcGidToSid(gid, &wsid);
4498 if (!WBC_ERROR_IS_OK(wbc_status)) {
4499 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4500 gid, wbcErrorString(wbc_status)));
4501 wbcFreeMemory(groups);
4502 return false;
4505 wbc_status = wbcSidToString(&wsid, &sid_str);
4506 if (!WBC_ERROR_IS_OK(wbc_status)) {
4507 wbcFreeMemory(groups);
4508 return false;
4511 DEBUG(3, (" %s\n", sid_str));
4513 string_to_sid(&sid, sid_str);
4514 wbcFreeMemory(sid_str);
4515 sid_str = NULL;
4517 add_sid_to_token(token, &sid);
4519 wbcFreeMemory(groups);
4521 return true;
4525 * Get a list of all user tokens we want to look at
4528 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4529 struct user_token **user_tokens)
4531 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4532 uint32_t i, num_users;
4533 const char **users;
4534 struct user_token *result;
4535 TALLOC_CTX *frame = NULL;
4537 if (lp_winbind_use_default_domain() &&
4538 (c->opt_target_workgroup == NULL)) {
4539 d_fprintf(stderr, "winbind use default domain = yes set, "
4540 "please specify a workgroup\n");
4541 return false;
4544 /* Send request to winbind daemon */
4546 wbc_status = wbcListUsers(NULL, &num_users, &users);
4547 if (!WBC_ERROR_IS_OK(wbc_status)) {
4548 DEBUG(1, ("winbind could not list users: %s\n",
4549 wbcErrorString(wbc_status)));
4550 return false;
4553 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4555 if (result == NULL) {
4556 DEBUG(1, ("Could not malloc sid array\n"));
4557 wbcFreeMemory(users);
4558 return false;
4561 frame = talloc_stackframe();
4562 for (i=0; i < num_users; i++) {
4563 fstring domain, user;
4564 char *p;
4566 fstrcpy(result[i].name, users[i]);
4568 p = strchr(users[i], *lp_winbind_separator());
4570 DEBUG(3, ("%s\n", users[i]));
4572 if (p == NULL) {
4573 fstrcpy(domain, c->opt_target_workgroup);
4574 fstrcpy(user, users[i]);
4575 } else {
4576 *p++ = '\0';
4577 fstrcpy(domain, users[i]);
4578 strupper_m(domain);
4579 fstrcpy(user, p);
4582 get_user_sids(domain, user, &(result[i].token));
4583 i+=1;
4585 TALLOC_FREE(frame);
4586 wbcFreeMemory(users);
4588 *num_tokens = num_users;
4589 *user_tokens = result;
4591 return true;
4594 static bool get_user_tokens_from_file(FILE *f,
4595 int *num_tokens,
4596 struct user_token **tokens)
4598 struct user_token *token = NULL;
4600 while (!feof(f)) {
4601 fstring line;
4603 if (fgets(line, sizeof(line)-1, f) == NULL) {
4604 return true;
4607 if (line[strlen(line)-1] == '\n')
4608 line[strlen(line)-1] = '\0';
4610 if (line[0] == ' ') {
4611 /* We have a SID */
4613 DOM_SID sid;
4614 string_to_sid(&sid, &line[1]);
4616 if (token == NULL) {
4617 DEBUG(0, ("File does not begin with username"));
4618 return false;
4621 add_sid_to_token(&token->token, &sid);
4622 continue;
4625 /* And a new user... */
4627 *num_tokens += 1;
4628 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4629 if (*tokens == NULL) {
4630 DEBUG(0, ("Could not realloc tokens\n"));
4631 return false;
4634 token = &((*tokens)[*num_tokens-1]);
4636 fstrcpy(token->name, line);
4637 token->token.num_sids = 0;
4638 token->token.user_sids = NULL;
4639 continue;
4642 return false;
4647 * Show the list of all users that have access to a share
4650 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4651 TALLOC_CTX *mem_ctx,
4652 const char *netname,
4653 int num_tokens,
4654 struct user_token *tokens)
4656 int fnum;
4657 SEC_DESC *share_sd = NULL;
4658 SEC_DESC *root_sd = NULL;
4659 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4660 int i;
4661 union srvsvc_NetShareInfo info;
4662 WERROR result;
4663 NTSTATUS status;
4664 uint16 cnum;
4666 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4667 pipe_hnd->desthost,
4668 netname,
4669 502,
4670 &info,
4671 &result);
4673 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4674 DEBUG(1, ("Coult not query secdesc for share %s\n",
4675 netname));
4676 return;
4679 share_sd = info.info502->sd_buf.sd;
4680 if (share_sd == NULL) {
4681 DEBUG(1, ("Got no secdesc for share %s\n",
4682 netname));
4685 cnum = cli->cnum;
4687 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4688 return;
4691 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4693 if (fnum != -1) {
4694 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4697 for (i=0; i<num_tokens; i++) {
4698 uint32 acc_granted;
4700 if (share_sd != NULL) {
4701 if (!se_access_check(share_sd, &tokens[i].token,
4702 1, &acc_granted, &status)) {
4703 DEBUG(1, ("Could not check share_sd for "
4704 "user %s\n",
4705 tokens[i].name));
4706 continue;
4709 if (!NT_STATUS_IS_OK(status))
4710 continue;
4713 if (root_sd == NULL) {
4714 d_printf(" %s\n", tokens[i].name);
4715 continue;
4718 if (!se_access_check(root_sd, &tokens[i].token,
4719 1, &acc_granted, &status)) {
4720 DEBUG(1, ("Could not check root_sd for user %s\n",
4721 tokens[i].name));
4722 continue;
4725 if (!NT_STATUS_IS_OK(status))
4726 continue;
4728 d_printf(" %s\n", tokens[i].name);
4731 if (fnum != -1)
4732 cli_close(cli, fnum);
4733 cli_tdis(cli);
4734 cli->cnum = cnum;
4736 return;
4739 struct share_list {
4740 int num_shares;
4741 char **shares;
4744 static void collect_share(const char *name, uint32 m,
4745 const char *comment, void *state)
4747 struct share_list *share_list = (struct share_list *)state;
4749 if (m != STYPE_DISKTREE)
4750 return;
4752 share_list->num_shares += 1;
4753 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4754 if (!share_list->shares) {
4755 share_list->num_shares = 0;
4756 return;
4758 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4762 * List shares on a remote RPC server, including the security descriptors.
4764 * All parameters are provided by the run_rpc_command function, except for
4765 * argc, argv which are passed through.
4767 * @param domain_sid The domain sid acquired from the remote server.
4768 * @param cli A cli_state connected to the server.
4769 * @param mem_ctx Talloc context, destroyed on completion of the function.
4770 * @param argc Standard main() style argc.
4771 * @param argv Standard main() style argv. Initial components are already
4772 * stripped.
4774 * @return Normal NTSTATUS return.
4777 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4778 const DOM_SID *domain_sid,
4779 const char *domain_name,
4780 struct cli_state *cli,
4781 struct rpc_pipe_client *pipe_hnd,
4782 TALLOC_CTX *mem_ctx,
4783 int argc,
4784 const char **argv)
4786 int ret;
4787 bool r;
4788 ENUM_HND hnd;
4789 uint32 i;
4790 FILE *f;
4792 struct user_token *tokens = NULL;
4793 int num_tokens = 0;
4795 struct share_list share_list;
4797 if (argc == 0) {
4798 f = stdin;
4799 } else {
4800 f = fopen(argv[0], "r");
4803 if (f == NULL) {
4804 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4805 return NT_STATUS_UNSUCCESSFUL;
4808 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4810 if (f != stdin)
4811 fclose(f);
4813 if (!r) {
4814 DEBUG(0, ("Could not read users from file\n"));
4815 return NT_STATUS_UNSUCCESSFUL;
4818 for (i=0; i<num_tokens; i++)
4819 collect_alias_memberships(&tokens[i].token);
4821 init_enum_hnd(&hnd, 0);
4823 share_list.num_shares = 0;
4824 share_list.shares = NULL;
4826 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4828 if (ret == -1) {
4829 DEBUG(0, ("Error returning browse list: %s\n",
4830 cli_errstr(cli)));
4831 goto done;
4834 for (i = 0; i < share_list.num_shares; i++) {
4835 char *netname = share_list.shares[i];
4837 if (netname[strlen(netname)-1] == '$')
4838 continue;
4840 d_printf("%s\n", netname);
4842 show_userlist(pipe_hnd, mem_ctx, netname,
4843 num_tokens, tokens);
4845 done:
4846 for (i=0; i<num_tokens; i++) {
4847 free_user_token(&tokens[i].token);
4849 SAFE_FREE(tokens);
4850 SAFE_FREE(share_list.shares);
4852 return NT_STATUS_OK;
4855 static int rpc_share_allowedusers(struct net_context *c, int argc,
4856 const char **argv)
4858 int result;
4860 if (c->display_usage) {
4861 d_printf("Usage:\n"
4862 "net rpc share allowedusers\n"
4863 " List allowed users\n");
4864 return 0;
4867 result = run_rpc_command(c, NULL, PI_SAMR, 0,
4868 rpc_aliaslist_internals,
4869 argc, argv);
4870 if (result != 0)
4871 return result;
4873 result = run_rpc_command(c, NULL, PI_LSARPC, 0,
4874 rpc_aliaslist_dump,
4875 argc, argv);
4876 if (result != 0)
4877 return result;
4879 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4880 rpc_share_allowedusers_internals,
4881 argc, argv);
4884 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4886 int num_tokens = 0;
4887 struct user_token *tokens = NULL;
4888 int i;
4890 if (argc != 0) {
4891 net_usersidlist_usage(c, argc, argv);
4892 return 0;
4895 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4896 DEBUG(0, ("Could not get the user/sid list\n"));
4897 return 0;
4900 for (i=0; i<num_tokens; i++) {
4901 dump_user_token(&tokens[i]);
4902 free_user_token(&tokens[i].token);
4905 SAFE_FREE(tokens);
4906 return 1;
4909 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4911 d_printf("net usersidlist\n"
4912 "\tprints out a list of all users the running winbind knows\n"
4913 "\tabout, together with all their SIDs. This is used as\n"
4914 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4916 net_common_flags_usage(c, argc, argv);
4917 return -1;
4921 * 'net rpc share' entrypoint.
4922 * @param argc Standard main() style argc.
4923 * @param argv Standard main() style argv. Initial components are already
4924 * stripped.
4927 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4929 struct functable func[] = {
4931 "add",
4932 rpc_share_add,
4933 NET_TRANSPORT_RPC,
4934 "Add share",
4935 "net rpc share add\n"
4936 " Add share"
4939 "delete",
4940 rpc_share_delete,
4941 NET_TRANSPORT_RPC,
4942 "Remove share",
4943 "net rpc share delete\n"
4944 " Remove share"
4947 "allowedusers",
4948 rpc_share_allowedusers,
4949 NET_TRANSPORT_RPC,
4950 "Modify allowed users",
4951 "net rpc share allowedusers\n"
4952 " Modify allowed users"
4955 "migrate",
4956 rpc_share_migrate,
4957 NET_TRANSPORT_RPC,
4958 "Migrate share to local server",
4959 "net rpc share migrate\n"
4960 " Migrate share to local server"
4963 "list",
4964 rpc_share_list,
4965 NET_TRANSPORT_RPC,
4966 "List shares",
4967 "net rpc share list\n"
4968 " List shares"
4970 {NULL, NULL, 0, NULL, NULL}
4974 if (argc == 0) {
4975 if (c->display_usage) {
4976 d_printf("Usage:\n"
4977 "net rpc share\n"
4978 " List shares\n"
4979 " Alias for net rpc share list\n");
4980 net_display_usage_from_functable(func);
4981 return 0;
4984 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4985 rpc_share_list_internals,
4986 argc, argv);
4989 return net_run_function(c, argc, argv, "net rpc share", func);
4992 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4993 TALLOC_CTX *mem_ctx,
4994 struct rpc_sh_ctx *ctx,
4995 struct rpc_pipe_client *pipe_hnd,
4996 int argc, const char **argv)
4998 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4999 ctx->cli, pipe_hnd, mem_ctx,
5000 argc, argv);
5003 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5004 TALLOC_CTX *mem_ctx,
5005 struct rpc_sh_ctx *ctx,
5006 struct rpc_pipe_client *pipe_hnd,
5007 int argc, const char **argv)
5009 WERROR result;
5010 NTSTATUS status;
5011 uint32_t parm_err = 0;
5012 union srvsvc_NetShareInfo info;
5013 struct srvsvc_NetShareInfo2 info2;
5015 if ((argc < 2) || (argc > 3)) {
5016 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
5017 ctx->whoami);
5018 return NT_STATUS_INVALID_PARAMETER;
5021 info2.name = argv[0];
5022 info2.type = STYPE_DISKTREE;
5023 info2.comment = (argc == 3) ? argv[2] : "";
5024 info2.permissions = 0;
5025 info2.max_users = 0;
5026 info2.current_users = 0;
5027 info2.path = argv[1];
5028 info2.password = NULL;
5030 info.info2 = &info2;
5032 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
5033 pipe_hnd->desthost,
5035 &info,
5036 &parm_err,
5037 &result);
5039 return status;
5042 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5043 TALLOC_CTX *mem_ctx,
5044 struct rpc_sh_ctx *ctx,
5045 struct rpc_pipe_client *pipe_hnd,
5046 int argc, const char **argv)
5048 WERROR result;
5049 NTSTATUS status;
5051 if (argc != 1) {
5052 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5053 return NT_STATUS_INVALID_PARAMETER;
5056 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
5057 pipe_hnd->desthost,
5058 argv[0],
5060 &result);
5062 return status;
5065 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5066 TALLOC_CTX *mem_ctx,
5067 struct rpc_sh_ctx *ctx,
5068 struct rpc_pipe_client *pipe_hnd,
5069 int argc, const char **argv)
5071 union srvsvc_NetShareInfo info;
5072 WERROR result;
5073 NTSTATUS status;
5075 if (argc != 1) {
5076 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5077 return NT_STATUS_INVALID_PARAMETER;
5080 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
5081 pipe_hnd->desthost,
5082 argv[0],
5084 &info,
5085 &result);
5086 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5087 goto done;
5090 d_printf("Name: %s\n", info.info2->name);
5091 d_printf("Comment: %s\n", info.info2->comment);
5092 d_printf("Path: %s\n", info.info2->path);
5093 d_printf("Password: %s\n", info.info2->password);
5095 done:
5096 return werror_to_ntstatus(result);
5099 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5100 struct rpc_sh_ctx *ctx)
5102 static struct rpc_sh_cmd cmds[] = {
5104 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
5105 "List available shares" },
5107 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
5108 "Add a share" },
5110 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
5111 "Delete a share" },
5113 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
5114 "Get information about a share" },
5116 { NULL, NULL, 0, NULL, NULL }
5119 return cmds;
5122 /****************************************************************************/
5124 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5126 return net_file_usage(c, argc, argv);
5130 * Close a file on a remote RPC server.
5132 * All parameters are provided by the run_rpc_command function, except for
5133 * argc, argv which are passed through.
5135 * @param c A net_context structure.
5136 * @param domain_sid The domain sid acquired from the remote server.
5137 * @param cli A cli_state connected to the server.
5138 * @param mem_ctx Talloc context, destroyed on completion of the function.
5139 * @param argc Standard main() style argc.
5140 * @param argv Standard main() style argv. Initial components are already
5141 * stripped.
5143 * @return Normal NTSTATUS return.
5145 static NTSTATUS rpc_file_close_internals(struct net_context *c,
5146 const DOM_SID *domain_sid,
5147 const char *domain_name,
5148 struct cli_state *cli,
5149 struct rpc_pipe_client *pipe_hnd,
5150 TALLOC_CTX *mem_ctx,
5151 int argc,
5152 const char **argv)
5154 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5155 pipe_hnd->desthost,
5156 atoi(argv[0]), NULL);
5160 * Close a file on a remote RPC server.
5162 * @param argc Standard main() style argc.
5163 * @param argv Standard main() style argv. Initial components are already
5164 * stripped.
5166 * @return A shell status integer (0 for success).
5168 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5170 if (argc < 1 || c->display_usage) {
5171 return rpc_file_usage(c, argc, argv);
5174 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5175 rpc_file_close_internals,
5176 argc, argv);
5180 * Formatted print of open file info
5182 * @param r struct srvsvc_NetFileInfo3 contents
5185 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
5187 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5188 r->fid, r->user, r->permissions, r->num_locks, r->path);
5192 * List open files on a remote RPC server.
5194 * All parameters are provided by the run_rpc_command function, except for
5195 * argc, argv which are passed through.
5197 * @param c A net_context structure.
5198 * @param domain_sid The domain sid acquired from the remote server.
5199 * @param cli A cli_state connected to the server.
5200 * @param mem_ctx Talloc context, destroyed on completion of the function.
5201 * @param argc Standard main() style argc.
5202 * @param argv Standard main() style argv. Initial components are already
5203 * stripped.
5205 * @return Normal NTSTATUS return.
5208 static NTSTATUS rpc_file_list_internals(struct net_context *c,
5209 const DOM_SID *domain_sid,
5210 const char *domain_name,
5211 struct cli_state *cli,
5212 struct rpc_pipe_client *pipe_hnd,
5213 TALLOC_CTX *mem_ctx,
5214 int argc,
5215 const char **argv)
5217 struct srvsvc_NetFileInfoCtr info_ctr;
5218 struct srvsvc_NetFileCtr3 ctr3;
5219 WERROR result;
5220 NTSTATUS status;
5221 uint32 preferred_len = 0xffffffff, i;
5222 const char *username=NULL;
5223 uint32_t total_entries = 0;
5224 uint32_t resume_handle = 0;
5226 /* if argc > 0, must be user command */
5227 if (argc > 0)
5228 username = smb_xstrdup(argv[0]);
5230 ZERO_STRUCT(info_ctr);
5231 ZERO_STRUCT(ctr3);
5233 info_ctr.level = 3;
5234 info_ctr.ctr.ctr3 = &ctr3;
5236 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
5237 pipe_hnd->desthost,
5238 NULL,
5239 username,
5240 &info_ctr,
5241 preferred_len,
5242 &total_entries,
5243 &resume_handle,
5244 &result);
5246 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5247 goto done;
5249 /* Display results */
5251 d_printf(
5252 "\nEnumerating open files on remote server:\n\n"
5253 "\nFileId Opened by Perms Locks Path"
5254 "\n------ --------- ----- ----- ---- \n");
5255 for (i = 0; i < total_entries; i++)
5256 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5257 done:
5258 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5262 * List files for a user on a remote RPC server.
5264 * @param argc Standard main() style argc.
5265 * @param argv Standard main() style argv. Initial components are already
5266 * stripped.
5268 * @return A shell status integer (0 for success)..
5271 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5273 if (argc < 1 || c->display_usage) {
5274 return rpc_file_usage(c, argc, argv);
5277 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5278 rpc_file_list_internals,
5279 argc, argv);
5283 * 'net rpc file' entrypoint.
5284 * @param argc Standard main() style argc.
5285 * @param argv Standard main() style argv. Initial components are already
5286 * stripped.
5289 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5291 struct functable func[] = {
5293 "close",
5294 rpc_file_close,
5295 NET_TRANSPORT_RPC,
5296 "Close opened file",
5297 "net rpc file close\n"
5298 " Close opened file"
5301 "user",
5302 rpc_file_user,
5303 NET_TRANSPORT_RPC,
5304 "List files opened by user",
5305 "net rpc file user\n"
5306 " List files opened by user"
5308 #if 0
5310 "info",
5311 rpc_file_info,
5312 NET_TRANSPORT_RPC,
5313 "Display information about opened file",
5314 "net rpc file info\n"
5315 " Display information about opened file"
5317 #endif
5318 {NULL, NULL, 0, NULL, NULL}
5321 if (argc == 0) {
5322 if (c->display_usage) {
5323 d_printf("Usage:\n");
5324 d_printf("net rpc file\n"
5325 " List opened files\n");
5326 net_display_usage_from_functable(func);
5327 return 0;
5330 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5331 rpc_file_list_internals,
5332 argc, argv);
5335 return net_run_function(c, argc, argv, "net rpc file", func);
5339 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5341 * All parameters are provided by the run_rpc_command function, except for
5342 * argc, argv which are passed through.
5344 * @param c A net_context structure.
5345 * @param domain_sid The domain sid acquired from the remote server.
5346 * @param cli A cli_state connected to the server.
5347 * @param mem_ctx Talloc context, destroyed on completion of the function.
5348 * @param argc Standard main() style argc.
5349 * @param argv Standard main() style argv. Initial components are already
5350 * stripped.
5352 * @return Normal NTSTATUS return.
5355 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5356 const DOM_SID *domain_sid,
5357 const char *domain_name,
5358 struct cli_state *cli,
5359 struct rpc_pipe_client *pipe_hnd,
5360 TALLOC_CTX *mem_ctx,
5361 int argc,
5362 const char **argv)
5364 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5366 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5368 if (NT_STATUS_IS_OK(result)) {
5369 d_printf("\nShutdown successfully aborted\n");
5370 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5371 } else
5372 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5374 return result;
5378 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5380 * All parameters are provided by the run_rpc_command function, except for
5381 * argc, argv which are passed through.
5383 * @param c A net_context structure.
5384 * @param domain_sid The domain sid acquired from the remote server.
5385 * @param cli A cli_state connected to the server.
5386 * @param mem_ctx Talloc context, destroyed on completion of the function.
5387 * @param argc Standard main() style argc.
5388 * @param argv Standard main() style argv. Initial components are already
5389 * stripped.
5391 * @return Normal NTSTATUS return.
5394 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5395 const DOM_SID *domain_sid,
5396 const char *domain_name,
5397 struct cli_state *cli,
5398 struct rpc_pipe_client *pipe_hnd,
5399 TALLOC_CTX *mem_ctx,
5400 int argc,
5401 const char **argv)
5403 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5405 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5407 if (NT_STATUS_IS_OK(result)) {
5408 d_printf("\nShutdown successfully aborted\n");
5409 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5410 } else
5411 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5413 return result;
5417 * ABORT the shutdown of a remote RPC server.
5419 * @param argc Standard main() style argc.
5420 * @param argv Standard main() style argv. Initial components are already
5421 * stripped.
5423 * @return A shell status integer (0 for success).
5426 static int rpc_shutdown_abort(struct net_context *c, int argc,
5427 const char **argv)
5429 int rc = -1;
5431 if (c->display_usage) {
5432 d_printf("Usage:\n"
5433 "net rpc abortshutdown\n"
5434 " Abort a scheduled shutdown\n");
5435 return 0;
5438 rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5439 rpc_shutdown_abort_internals, argc, argv);
5441 if (rc == 0)
5442 return rc;
5444 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5446 return run_rpc_command(c, NULL, PI_WINREG, 0,
5447 rpc_reg_shutdown_abort_internals,
5448 argc, argv);
5452 * Shut down a remote RPC Server via initshutdown pipe.
5454 * All parameters are provided by the run_rpc_command function, except for
5455 * argc, argv which are passed through.
5457 * @param c A net_context structure.
5458 * @param domain_sid The domain sid acquired from the remote server.
5459 * @param cli A cli_state connected to the server.
5460 * @param mem_ctx Talloc context, destroyed on completion of the function.
5461 * @param argc Standard main() style argc.
5462 * @param argv Standard main() style argv. Initial components are already
5463 * stripped.
5465 * @return Normal NTSTATUS return.
5468 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5469 const DOM_SID *domain_sid,
5470 const char *domain_name,
5471 struct cli_state *cli,
5472 struct rpc_pipe_client *pipe_hnd,
5473 TALLOC_CTX *mem_ctx,
5474 int argc,
5475 const char **argv)
5477 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5478 const char *msg = "This machine will be shutdown shortly";
5479 uint32 timeout = 20;
5480 struct initshutdown_String msg_string;
5481 struct initshutdown_String_sub s;
5483 if (c->opt_comment) {
5484 msg = c->opt_comment;
5486 if (c->opt_timeout) {
5487 timeout = c->opt_timeout;
5490 s.name = msg;
5491 msg_string.name = &s;
5493 /* create an entry */
5494 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5495 &msg_string, timeout, c->opt_force, c->opt_reboot,
5496 NULL);
5498 if (NT_STATUS_IS_OK(result)) {
5499 d_printf("\nShutdown of remote machine succeeded\n");
5500 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5501 } else {
5502 DEBUG(1,("Shutdown of remote machine failed!\n"));
5504 return result;
5508 * Shut down a remote RPC Server via winreg pipe.
5510 * All parameters are provided by the run_rpc_command function, except for
5511 * argc, argv which are passed through.
5513 * @param c A net_context structure.
5514 * @param domain_sid The domain sid acquired from the remote server.
5515 * @param cli A cli_state connected to the server.
5516 * @param mem_ctx Talloc context, destroyed on completion of the function.
5517 * @param argc Standard main() style argc.
5518 * @param argv Standard main() style argv. Initial components are already
5519 * stripped.
5521 * @return Normal NTSTATUS return.
5524 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5525 const DOM_SID *domain_sid,
5526 const char *domain_name,
5527 struct cli_state *cli,
5528 struct rpc_pipe_client *pipe_hnd,
5529 TALLOC_CTX *mem_ctx,
5530 int argc,
5531 const char **argv)
5533 const char *msg = "This machine will be shutdown shortly";
5534 uint32 timeout = 20;
5535 struct initshutdown_String msg_string;
5536 struct initshutdown_String_sub s;
5537 NTSTATUS result;
5538 WERROR werr;
5540 if (c->opt_comment) {
5541 msg = c->opt_comment;
5543 s.name = msg;
5544 msg_string.name = &s;
5546 if (c->opt_timeout) {
5547 timeout = c->opt_timeout;
5550 /* create an entry */
5551 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5552 &msg_string, timeout, c->opt_force, c->opt_reboot,
5553 &werr);
5555 if (NT_STATUS_IS_OK(result)) {
5556 d_printf("\nShutdown of remote machine succeeded\n");
5557 } else {
5558 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5559 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5560 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5561 else
5562 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5565 return result;
5569 * Shut down a remote RPC server.
5571 * @param argc Standard main() style argc.
5572 * @param argv Standard main() style argv. Initial components are already
5573 * stripped.
5575 * @return A shell status integer (0 for success).
5578 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5580 int rc = -1;
5582 if (c->display_usage) {
5583 d_printf("Usage:\n"
5584 "net rpc shutdown\n"
5585 " Shut down a remote RPC server\n");
5586 return 0;
5589 rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5590 rpc_init_shutdown_internals, argc, argv);
5592 if (rc) {
5593 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5594 rc = run_rpc_command(c, NULL, PI_WINREG, 0,
5595 rpc_reg_shutdown_internals, argc, argv);
5598 return rc;
5601 /***************************************************************************
5602 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5603 ***************************************************************************/
5606 * Add interdomain trust account to the RPC server.
5607 * All parameters (except for argc and argv) are passed by run_rpc_command
5608 * function.
5610 * @param c A net_context structure.
5611 * @param domain_sid The domain sid acquired from the server.
5612 * @param cli A cli_state connected to the server.
5613 * @param mem_ctx Talloc context, destroyed on completion of the function.
5614 * @param argc Standard main() style argc.
5615 * @param argv Standard main() style argv. Initial components are already
5616 * stripped.
5618 * @return normal NTSTATUS return code.
5621 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5622 const DOM_SID *domain_sid,
5623 const char *domain_name,
5624 struct cli_state *cli,
5625 struct rpc_pipe_client *pipe_hnd,
5626 TALLOC_CTX *mem_ctx,
5627 int argc,
5628 const char **argv)
5630 POLICY_HND connect_pol, domain_pol, user_pol;
5631 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5632 char *acct_name;
5633 struct lsa_String lsa_acct_name;
5634 uint32 acb_info;
5635 uint32 acct_flags=0;
5636 uint32 user_rid;
5637 uint32_t access_granted = 0;
5638 union samr_UserInfo info;
5640 if (argc != 2) {
5641 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5642 return NT_STATUS_INVALID_PARAMETER;
5646 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5649 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5650 return NT_STATUS_NO_MEMORY;
5653 strupper_m(acct_name);
5655 init_lsa_String(&lsa_acct_name, acct_name);
5657 /* Get samr policy handle */
5658 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5659 pipe_hnd->desthost,
5660 MAXIMUM_ALLOWED_ACCESS,
5661 &connect_pol);
5662 if (!NT_STATUS_IS_OK(result)) {
5663 goto done;
5666 /* Get domain policy handle */
5667 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5668 &connect_pol,
5669 MAXIMUM_ALLOWED_ACCESS,
5670 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5671 &domain_pol);
5672 if (!NT_STATUS_IS_OK(result)) {
5673 goto done;
5676 /* Create trusting domain's account */
5677 acb_info = ACB_NORMAL;
5678 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5679 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5680 SAMR_USER_ACCESS_SET_PASSWORD |
5681 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5682 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5684 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5685 &domain_pol,
5686 &lsa_acct_name,
5687 acb_info,
5688 acct_flags,
5689 &user_pol,
5690 &access_granted,
5691 &user_rid);
5692 if (!NT_STATUS_IS_OK(result)) {
5693 goto done;
5697 NTTIME notime;
5698 struct samr_LogonHours hours;
5699 struct lsa_BinaryString parameters;
5700 const int units_per_week = 168;
5701 uchar pwbuf[516];
5703 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5705 ZERO_STRUCT(notime);
5706 ZERO_STRUCT(hours);
5707 ZERO_STRUCT(parameters);
5709 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5710 if (!hours.bits) {
5711 result = NT_STATUS_NO_MEMORY;
5712 goto done;
5714 hours.units_per_week = units_per_week;
5715 memset(hours.bits, 0xFF, units_per_week);
5717 init_samr_user_info23(&info.info23,
5718 notime, notime, notime,
5719 notime, notime, notime,
5720 NULL, NULL, NULL, NULL, NULL,
5721 NULL, NULL, NULL, NULL, &parameters,
5722 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5723 hours,
5724 0, 0, 0, 0, 0, 0, 0,
5725 pwbuf, 24);
5727 SamOEMhashBlob(info.info23.password.data, 516,
5728 &cli->user_session_key);
5730 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5731 &user_pol,
5733 &info);
5735 if (!NT_STATUS_IS_OK(result)) {
5736 DEBUG(0,("Could not set trust account password: %s\n",
5737 nt_errstr(result)));
5738 goto done;
5742 done:
5743 SAFE_FREE(acct_name);
5744 return result;
5748 * Create interdomain trust account for a remote domain.
5750 * @param argc Standard argc.
5751 * @param argv Standard argv without initial components.
5753 * @return Integer status (0 means success).
5756 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5758 if (argc > 0 && !c->display_usage) {
5759 return run_rpc_command(c, NULL, PI_SAMR, 0,
5760 rpc_trustdom_add_internals, argc, argv);
5761 } else {
5762 d_printf("Usage:\n"
5763 "net rpc trustdom add <domain>\n");
5764 return -1;
5770 * Remove interdomain trust account from the RPC server.
5771 * All parameters (except for argc and argv) are passed by run_rpc_command
5772 * function.
5774 * @param c A net_context structure.
5775 * @param domain_sid The domain sid acquired from the server.
5776 * @param cli A cli_state connected to the server.
5777 * @param mem_ctx Talloc context, destroyed on completion of the function.
5778 * @param argc Standard main() style argc.
5779 * @param argv Standard main() style argv. Initial components are already
5780 * stripped.
5782 * @return normal NTSTATUS return code.
5785 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5786 const DOM_SID *domain_sid,
5787 const char *domain_name,
5788 struct cli_state *cli,
5789 struct rpc_pipe_client *pipe_hnd,
5790 TALLOC_CTX *mem_ctx,
5791 int argc,
5792 const char **argv)
5794 POLICY_HND connect_pol, domain_pol, user_pol;
5795 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5796 char *acct_name;
5797 DOM_SID trust_acct_sid;
5798 struct samr_Ids user_rids, name_types;
5799 struct lsa_String lsa_acct_name;
5801 if (argc != 1) {
5802 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5803 return NT_STATUS_INVALID_PARAMETER;
5807 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5809 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5811 if (acct_name == NULL)
5812 return NT_STATUS_NO_MEMORY;
5814 strupper_m(acct_name);
5816 /* Get samr policy handle */
5817 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5818 pipe_hnd->desthost,
5819 MAXIMUM_ALLOWED_ACCESS,
5820 &connect_pol);
5821 if (!NT_STATUS_IS_OK(result)) {
5822 goto done;
5825 /* Get domain policy handle */
5826 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5827 &connect_pol,
5828 MAXIMUM_ALLOWED_ACCESS,
5829 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5830 &domain_pol);
5831 if (!NT_STATUS_IS_OK(result)) {
5832 goto done;
5835 init_lsa_String(&lsa_acct_name, acct_name);
5837 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5838 &domain_pol,
5840 &lsa_acct_name,
5841 &user_rids,
5842 &name_types);
5844 if (!NT_STATUS_IS_OK(result)) {
5845 goto done;
5848 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5849 &domain_pol,
5850 MAXIMUM_ALLOWED_ACCESS,
5851 user_rids.ids[0],
5852 &user_pol);
5854 if (!NT_STATUS_IS_OK(result)) {
5855 goto done;
5858 /* append the rid to the domain sid */
5859 sid_copy(&trust_acct_sid, domain_sid);
5860 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5861 goto done;
5864 /* remove the sid */
5866 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5867 &user_pol,
5868 &trust_acct_sid);
5869 if (!NT_STATUS_IS_OK(result)) {
5870 goto done;
5873 /* Delete user */
5875 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5876 &user_pol);
5878 if (!NT_STATUS_IS_OK(result)) {
5879 goto done;
5882 if (!NT_STATUS_IS_OK(result)) {
5883 DEBUG(0,("Could not set trust account password: %s\n",
5884 nt_errstr(result)));
5885 goto done;
5888 done:
5889 return result;
5893 * Delete interdomain trust account for a remote domain.
5895 * @param argc Standard argc.
5896 * @param argv Standard argv without initial components.
5898 * @return Integer status (0 means success).
5901 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5903 if (argc > 0 && !c->display_usage) {
5904 return run_rpc_command(c, NULL, PI_SAMR, 0,
5905 rpc_trustdom_del_internals, argc, argv);
5906 } else {
5907 d_printf("Usage:\n"
5908 "net rpc trustdom del <domain>\n");
5909 return -1;
5913 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5914 struct cli_state *cli,
5915 TALLOC_CTX *mem_ctx,
5916 const char *domain_name)
5918 char *dc_name = NULL;
5919 const char *buffer = NULL;
5920 struct rpc_pipe_client *netr;
5921 NTSTATUS status;
5923 /* Use NetServerEnum2 */
5925 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5926 SAFE_FREE(dc_name);
5927 return NT_STATUS_OK;
5930 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5931 for domain %s\n", domain_name));
5933 /* Try netr_GetDcName */
5935 netr = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &status);
5936 if (!netr) {
5937 return status;
5940 status = rpccli_netr_GetDcName(netr, mem_ctx,
5941 cli->desthost,
5942 domain_name,
5943 &buffer,
5944 NULL);
5945 TALLOC_FREE(netr);
5947 if (NT_STATUS_IS_OK(status)) {
5948 return status;
5951 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5952 for domain %s\n", domain_name));
5954 return status;
5958 * Establish trust relationship to a trusting domain.
5959 * Interdomain account must already be created on remote PDC.
5961 * @param c A net_context structure.
5962 * @param argc Standard argc.
5963 * @param argv Standard argv without initial components.
5965 * @return Integer status (0 means success).
5968 static int rpc_trustdom_establish(struct net_context *c, int argc,
5969 const char **argv)
5971 struct cli_state *cli = NULL;
5972 struct sockaddr_storage server_ss;
5973 struct rpc_pipe_client *pipe_hnd = NULL;
5974 POLICY_HND connect_hnd;
5975 TALLOC_CTX *mem_ctx;
5976 NTSTATUS nt_status;
5977 DOM_SID *domain_sid;
5979 char* domain_name;
5980 char* acct_name;
5981 fstring pdc_name;
5982 union lsa_PolicyInformation *info = NULL;
5985 * Connect to \\server\ipc$ as 'our domain' account with password
5988 if (argc != 1 || c->display_usage) {
5989 d_printf("Usage:\n"
5990 "net rpc trustdom establish <domain_name>\n");
5991 return -1;
5994 domain_name = smb_xstrdup(argv[0]);
5995 strupper_m(domain_name);
5997 /* account name used at first is our domain's name with '$' */
5998 asprintf(&acct_name, "%s$", lp_workgroup());
5999 strupper_m(acct_name);
6002 * opt_workgroup will be used by connection functions further,
6003 * hence it should be set to remote domain name instead of ours
6005 if (c->opt_workgroup) {
6006 c->opt_workgroup = smb_xstrdup(domain_name);
6009 c->opt_user_name = acct_name;
6011 /* find the domain controller */
6012 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6013 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6014 return -1;
6017 /* connect to ipc$ as username/password */
6018 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6019 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6021 /* Is it trusting domain account for sure ? */
6022 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6023 nt_errstr(nt_status)));
6024 return -1;
6027 /* store who we connected to */
6029 saf_store( domain_name, pdc_name );
6032 * Connect to \\server\ipc$ again (this time anonymously)
6035 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6036 (char*)pdc_name);
6038 if (NT_STATUS_IS_ERR(nt_status)) {
6039 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6040 domain_name, nt_errstr(nt_status)));
6041 return -1;
6044 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6045 "domain %s", domain_name))) {
6046 DEBUG(0, ("talloc_init() failed\n"));
6047 cli_shutdown(cli);
6048 return -1;
6051 /* Make sure we're talking to a proper server */
6053 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6054 if (!NT_STATUS_IS_OK(nt_status)) {
6055 cli_shutdown(cli);
6056 talloc_destroy(mem_ctx);
6057 return -1;
6061 * Call LsaOpenPolicy and LsaQueryInfo
6064 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6065 if (!pipe_hnd) {
6066 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6067 cli_shutdown(cli);
6068 talloc_destroy(mem_ctx);
6069 return -1;
6072 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
6073 &connect_hnd);
6074 if (NT_STATUS_IS_ERR(nt_status)) {
6075 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6076 nt_errstr(nt_status)));
6077 cli_shutdown(cli);
6078 talloc_destroy(mem_ctx);
6079 return -1;
6082 /* Querying info level 5 */
6084 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6085 &connect_hnd,
6086 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6087 &info);
6088 if (NT_STATUS_IS_ERR(nt_status)) {
6089 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6090 nt_errstr(nt_status)));
6091 cli_shutdown(cli);
6092 talloc_destroy(mem_ctx);
6093 return -1;
6096 domain_sid = info->account_domain.sid;
6098 /* There should be actually query info level 3 (following nt serv behaviour),
6099 but I still don't know if it's _really_ necessary */
6102 * Store the password in secrets db
6105 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6106 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6107 cli_shutdown(cli);
6108 talloc_destroy(mem_ctx);
6109 return -1;
6113 * Close the pipes and clean up
6116 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6117 if (NT_STATUS_IS_ERR(nt_status)) {
6118 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6119 nt_errstr(nt_status)));
6120 cli_shutdown(cli);
6121 talloc_destroy(mem_ctx);
6122 return -1;
6125 cli_shutdown(cli);
6127 talloc_destroy(mem_ctx);
6129 d_printf("Trust to domain %s established\n", domain_name);
6130 return 0;
6134 * Revoke trust relationship to the remote domain.
6136 * @param c A net_context structure.
6137 * @param argc Standard argc.
6138 * @param argv Standard argv without initial components.
6140 * @return Integer status (0 means success).
6143 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6144 const char **argv)
6146 char* domain_name;
6147 int rc = -1;
6149 if (argc < 1 || c->display_usage) {
6150 d_printf("Usage:\n"
6151 "net rpc trustdom revoke <domain_name>\n"
6152 " Revoke trust relationship\n"
6153 " domain_name\tName of domain to revoke trust\n");
6154 return -1;
6157 /* generate upper cased domain name */
6158 domain_name = smb_xstrdup(argv[0]);
6159 strupper_m(domain_name);
6161 /* delete password of the trust */
6162 if (!pdb_del_trusteddom_pw(domain_name)) {
6163 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6164 domain_name));
6165 goto done;
6168 rc = 0;
6169 done:
6170 SAFE_FREE(domain_name);
6171 return rc;
6174 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6175 const DOM_SID *domain_sid,
6176 const char *domain_name,
6177 struct cli_state *cli,
6178 struct rpc_pipe_client *pipe_hnd,
6179 TALLOC_CTX *mem_ctx,
6180 int argc,
6181 const char **argv)
6183 fstring str_sid;
6184 sid_to_fstring(str_sid, domain_sid);
6185 d_printf("%s\n", str_sid);
6186 return NT_STATUS_OK;
6189 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6191 fstring ascii_sid, padding;
6192 int pad_len, col_len = 20;
6194 /* convert sid into ascii string */
6195 sid_to_fstring(ascii_sid, dom_sid);
6197 /* calculate padding space for d_printf to look nicer */
6198 pad_len = col_len - strlen(trusted_dom_name);
6199 padding[pad_len] = 0;
6200 do padding[--pad_len] = ' '; while (pad_len);
6202 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6205 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6206 TALLOC_CTX *mem_ctx,
6207 POLICY_HND *pol,
6208 DOM_SID dom_sid,
6209 const char *trusted_dom_name)
6211 NTSTATUS nt_status;
6212 union lsa_TrustedDomainInfo *info = NULL;
6213 char *cleartextpwd = NULL;
6214 uint8_t nt_hash[16];
6215 DATA_BLOB data;
6217 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6218 pol,
6219 &dom_sid,
6220 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6221 &info);
6222 if (NT_STATUS_IS_ERR(nt_status)) {
6223 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6224 nt_errstr(nt_status)));
6225 goto done;
6228 data = data_blob(info->password.password->data,
6229 info->password.password->length);
6231 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
6232 DEBUG(0, ("Could not retrieve password hash\n"));
6233 goto done;
6236 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
6238 if (cleartextpwd == NULL) {
6239 DEBUG(0,("retrieved NULL password\n"));
6240 nt_status = NT_STATUS_UNSUCCESSFUL;
6241 goto done;
6244 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6245 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6246 nt_status = NT_STATUS_UNSUCCESSFUL;
6247 goto done;
6250 #ifdef DEBUG_PASSWORD
6251 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6252 "password: [%s]\n", trusted_dom_name,
6253 sid_string_dbg(&dom_sid), cleartextpwd));
6254 #endif
6256 done:
6257 SAFE_FREE(cleartextpwd);
6258 data_blob_free(&data);
6260 return nt_status;
6263 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6264 const char **argv)
6266 /* common variables */
6267 TALLOC_CTX* mem_ctx;
6268 struct cli_state *cli = NULL;
6269 struct rpc_pipe_client *pipe_hnd = NULL;
6270 NTSTATUS nt_status;
6271 const char *domain_name = NULL;
6272 DOM_SID *queried_dom_sid;
6273 POLICY_HND connect_hnd;
6274 union lsa_PolicyInformation *info = NULL;
6276 /* trusted domains listing variables */
6277 unsigned int enum_ctx = 0;
6278 int i;
6279 struct lsa_DomainList dom_list;
6280 fstring pdc_name;
6282 if (c->display_usage) {
6283 d_printf("Usage:\n"
6284 "net rpc trustdom vampire\n"
6285 " Vampire trust relationship from remote server\n");
6286 return 0;
6290 * Listing trusted domains (stored in secrets.tdb, if local)
6293 mem_ctx = talloc_init("trust relationships vampire");
6296 * set domain and pdc name to local samba server (default)
6297 * or to remote one given in command line
6300 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6301 domain_name = c->opt_workgroup;
6302 c->opt_target_workgroup = c->opt_workgroup;
6303 } else {
6304 fstrcpy(pdc_name, global_myname());
6305 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6306 c->opt_target_workgroup = domain_name;
6309 /* open \PIPE\lsarpc and open policy handle */
6310 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6311 if (!NT_STATUS_IS_OK(nt_status)) {
6312 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6313 nt_errstr(nt_status)));
6314 talloc_destroy(mem_ctx);
6315 return -1;
6318 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6319 if (!pipe_hnd) {
6320 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6321 nt_errstr(nt_status) ));
6322 cli_shutdown(cli);
6323 talloc_destroy(mem_ctx);
6324 return -1;
6327 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6328 &connect_hnd);
6329 if (NT_STATUS_IS_ERR(nt_status)) {
6330 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6331 nt_errstr(nt_status)));
6332 cli_shutdown(cli);
6333 talloc_destroy(mem_ctx);
6334 return -1;
6337 /* query info level 5 to obtain sid of a domain being queried */
6338 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6339 &connect_hnd,
6340 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6341 &info);
6343 if (NT_STATUS_IS_ERR(nt_status)) {
6344 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6345 nt_errstr(nt_status)));
6346 cli_shutdown(cli);
6347 talloc_destroy(mem_ctx);
6348 return -1;
6351 queried_dom_sid = info->account_domain.sid;
6354 * Keep calling LsaEnumTrustdom over opened pipe until
6355 * the end of enumeration is reached
6358 d_printf("Vampire trusted domains:\n\n");
6360 do {
6361 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6362 &connect_hnd,
6363 &enum_ctx,
6364 &dom_list,
6365 (uint32_t)-1);
6366 if (NT_STATUS_IS_ERR(nt_status)) {
6367 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6368 nt_errstr(nt_status)));
6369 cli_shutdown(cli);
6370 talloc_destroy(mem_ctx);
6371 return -1;
6374 for (i = 0; i < dom_list.count; i++) {
6376 print_trusted_domain(dom_list.domains[i].sid,
6377 dom_list.domains[i].name.string);
6379 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6380 *dom_list.domains[i].sid,
6381 dom_list.domains[i].name.string);
6382 if (!NT_STATUS_IS_OK(nt_status)) {
6383 cli_shutdown(cli);
6384 talloc_destroy(mem_ctx);
6385 return -1;
6390 * in case of no trusted domains say something rather
6391 * than just display blank line
6393 if (!dom_list.count) d_printf("none\n");
6395 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6397 /* close this connection before doing next one */
6398 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6399 if (NT_STATUS_IS_ERR(nt_status)) {
6400 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6401 nt_errstr(nt_status)));
6402 cli_shutdown(cli);
6403 talloc_destroy(mem_ctx);
6404 return -1;
6407 /* close lsarpc pipe and connection to IPC$ */
6408 cli_shutdown(cli);
6410 talloc_destroy(mem_ctx);
6411 return 0;
6414 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6416 /* common variables */
6417 TALLOC_CTX* mem_ctx;
6418 struct cli_state *cli = NULL, *remote_cli = NULL;
6419 struct rpc_pipe_client *pipe_hnd = NULL;
6420 NTSTATUS nt_status;
6421 const char *domain_name = NULL;
6422 DOM_SID *queried_dom_sid;
6423 fstring padding;
6424 int ascii_dom_name_len;
6425 POLICY_HND connect_hnd;
6426 union lsa_PolicyInformation *info = NULL;
6428 /* trusted domains listing variables */
6429 unsigned int num_domains, enum_ctx = 0;
6430 int i, pad_len, col_len = 20;
6431 struct lsa_DomainList dom_list;
6432 fstring pdc_name;
6434 /* trusting domains listing variables */
6435 POLICY_HND domain_hnd;
6436 struct samr_SamArray *trusts = NULL;
6438 if (c->display_usage) {
6439 d_printf("Usage:\n"
6440 "net rpc trustdom list\n"
6441 " List trust relationships\n");
6442 return 0;
6446 * Listing trusted domains (stored in secrets.tdb, if local)
6449 mem_ctx = talloc_init("trust relationships listing");
6452 * set domain and pdc name to local samba server (default)
6453 * or to remote one given in command line
6456 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6457 domain_name = c->opt_workgroup;
6458 c->opt_target_workgroup = c->opt_workgroup;
6459 } else {
6460 fstrcpy(pdc_name, global_myname());
6461 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6462 c->opt_target_workgroup = domain_name;
6465 /* open \PIPE\lsarpc and open policy handle */
6466 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6467 if (!NT_STATUS_IS_OK(nt_status)) {
6468 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6469 nt_errstr(nt_status)));
6470 talloc_destroy(mem_ctx);
6471 return -1;
6474 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6475 if (!pipe_hnd) {
6476 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6477 nt_errstr(nt_status) ));
6478 cli_shutdown(cli);
6479 talloc_destroy(mem_ctx);
6480 return -1;
6483 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6484 &connect_hnd);
6485 if (NT_STATUS_IS_ERR(nt_status)) {
6486 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6487 nt_errstr(nt_status)));
6488 cli_shutdown(cli);
6489 talloc_destroy(mem_ctx);
6490 return -1;
6493 /* query info level 5 to obtain sid of a domain being queried */
6494 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6495 &connect_hnd,
6496 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6497 &info);
6499 if (NT_STATUS_IS_ERR(nt_status)) {
6500 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6501 nt_errstr(nt_status)));
6502 cli_shutdown(cli);
6503 talloc_destroy(mem_ctx);
6504 return -1;
6507 queried_dom_sid = info->account_domain.sid;
6510 * Keep calling LsaEnumTrustdom over opened pipe until
6511 * the end of enumeration is reached
6514 d_printf("Trusted domains list:\n\n");
6516 do {
6517 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6518 &connect_hnd,
6519 &enum_ctx,
6520 &dom_list,
6521 (uint32_t)-1);
6522 if (NT_STATUS_IS_ERR(nt_status)) {
6523 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6524 nt_errstr(nt_status)));
6525 cli_shutdown(cli);
6526 talloc_destroy(mem_ctx);
6527 return -1;
6530 for (i = 0; i < dom_list.count; i++) {
6531 print_trusted_domain(dom_list.domains[i].sid,
6532 dom_list.domains[i].name.string);
6536 * in case of no trusted domains say something rather
6537 * than just display blank line
6539 if (!dom_list.count) d_printf("none\n");
6541 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6543 /* close this connection before doing next one */
6544 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6545 if (NT_STATUS_IS_ERR(nt_status)) {
6546 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6547 nt_errstr(nt_status)));
6548 cli_shutdown(cli);
6549 talloc_destroy(mem_ctx);
6550 return -1;
6553 TALLOC_FREE(pipe_hnd);
6556 * Listing trusting domains (stored in passdb backend, if local)
6559 d_printf("\nTrusting domains list:\n\n");
6562 * Open \PIPE\samr and get needed policy handles
6564 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6565 if (!pipe_hnd) {
6566 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6567 cli_shutdown(cli);
6568 talloc_destroy(mem_ctx);
6569 return -1;
6572 /* SamrConnect2 */
6573 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6574 pipe_hnd->desthost,
6575 SA_RIGHT_SAM_OPEN_DOMAIN,
6576 &connect_hnd);
6577 if (!NT_STATUS_IS_OK(nt_status)) {
6578 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6579 nt_errstr(nt_status)));
6580 cli_shutdown(cli);
6581 talloc_destroy(mem_ctx);
6582 return -1;
6585 /* SamrOpenDomain - we have to open domain policy handle in order to be
6586 able to enumerate accounts*/
6587 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6588 &connect_hnd,
6589 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6590 queried_dom_sid,
6591 &domain_hnd);
6592 if (!NT_STATUS_IS_OK(nt_status)) {
6593 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6594 nt_errstr(nt_status)));
6595 cli_shutdown(cli);
6596 talloc_destroy(mem_ctx);
6597 return -1;
6601 * perform actual enumeration
6604 enum_ctx = 0; /* reset enumeration context from last enumeration */
6605 do {
6607 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6608 &domain_hnd,
6609 &enum_ctx,
6610 ACB_DOMTRUST,
6611 &trusts,
6612 0xffff,
6613 &num_domains);
6614 if (NT_STATUS_IS_ERR(nt_status)) {
6615 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6616 nt_errstr(nt_status)));
6617 cli_shutdown(cli);
6618 talloc_destroy(mem_ctx);
6619 return -1;
6622 for (i = 0; i < num_domains; i++) {
6624 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6627 * get each single domain's sid (do we _really_ need this ?):
6628 * 1) connect to domain's pdc
6629 * 2) query the pdc for domain's sid
6632 /* get rid of '$' tail */
6633 ascii_dom_name_len = strlen(str);
6634 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6635 str[ascii_dom_name_len - 1] = '\0';
6637 /* calculate padding space for d_printf to look nicer */
6638 pad_len = col_len - strlen(str);
6639 padding[pad_len] = 0;
6640 do padding[--pad_len] = ' '; while (pad_len);
6642 /* set opt_* variables to remote domain */
6643 strupper_m(str);
6644 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6645 c->opt_target_workgroup = c->opt_workgroup;
6647 d_printf("%s%s", str, padding);
6649 /* connect to remote domain controller */
6650 nt_status = net_make_ipc_connection(c,
6651 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6652 &remote_cli);
6653 if (NT_STATUS_IS_OK(nt_status)) {
6654 /* query for domain's sid */
6655 if (run_rpc_command(c, remote_cli, PI_LSARPC, 0,
6656 rpc_query_domain_sid, argc,
6657 argv))
6658 d_fprintf(stderr, "couldn't get domain's sid\n");
6660 cli_shutdown(remote_cli);
6662 } else {
6663 d_fprintf(stderr, "domain controller is not "
6664 "responding: %s\n",
6665 nt_errstr(nt_status));
6669 if (!num_domains) d_printf("none\n");
6671 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6673 /* close opened samr and domain policy handles */
6674 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6675 if (!NT_STATUS_IS_OK(nt_status)) {
6676 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6679 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6680 if (!NT_STATUS_IS_OK(nt_status)) {
6681 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6684 /* close samr pipe and connection to IPC$ */
6685 cli_shutdown(cli);
6687 talloc_destroy(mem_ctx);
6688 return 0;
6692 * Entrypoint for 'net rpc trustdom' code.
6694 * @param argc Standard argc.
6695 * @param argv Standard argv without initial components.
6697 * @return Integer status (0 means success).
6700 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6702 struct functable func[] = {
6704 "add",
6705 rpc_trustdom_add,
6706 NET_TRANSPORT_RPC,
6707 "Add trusted domain's account",
6708 "net rpc trustdom add\n"
6709 " Add trusted domain's account"
6712 "del",
6713 rpc_trustdom_del,
6714 NET_TRANSPORT_RPC,
6715 "Remove trusted domain's account",
6716 "net rpc trustdom del\n"
6717 " Remove trusted domain's account"
6720 "establish",
6721 rpc_trustdom_establish,
6722 NET_TRANSPORT_RPC,
6723 "Establish trust relationship",
6724 "net rpc trustdom establish\n"
6725 " Establish trust relationship"
6728 "revoke",
6729 rpc_trustdom_revoke,
6730 NET_TRANSPORT_RPC,
6731 "Revoke trust relationship",
6732 "net rpc trustdom revoke\n"
6733 " Revoke trust relationship"
6736 "list",
6737 rpc_trustdom_list,
6738 NET_TRANSPORT_RPC,
6739 "List domain trusts",
6740 "net rpc trustdom list\n"
6741 " List domain trusts"
6744 "vampire",
6745 rpc_trustdom_vampire,
6746 NET_TRANSPORT_RPC,
6747 "Vampire trusts from remote server",
6748 "net rpc trustdom vampire\n"
6749 " Vampire trusts from remote server"
6751 {NULL, NULL, 0, NULL, NULL}
6754 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6758 * Check if a server will take rpc commands
6759 * @param flags Type of server to connect to (PDC, DMB, localhost)
6760 * if the host is not explicitly specified
6761 * @return bool (true means rpc supported)
6763 bool net_rpc_check(struct net_context *c, unsigned flags)
6765 struct cli_state *cli;
6766 bool ret = false;
6767 struct sockaddr_storage server_ss;
6768 char *server_name = NULL;
6769 NTSTATUS status;
6771 /* flags (i.e. server type) may depend on command */
6772 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6773 return false;
6775 if ((cli = cli_initialise()) == NULL) {
6776 return false;
6779 status = cli_connect(cli, server_name, &server_ss);
6780 if (!NT_STATUS_IS_OK(status))
6781 goto done;
6782 if (!attempt_netbios_session_request(&cli, global_myname(),
6783 server_name, &server_ss))
6784 goto done;
6785 if (!cli_negprot(cli))
6786 goto done;
6787 if (cli->protocol < PROTOCOL_NT1)
6788 goto done;
6790 ret = true;
6791 done:
6792 cli_shutdown(cli);
6793 return ret;
6796 /* dump sam database via samsync rpc calls */
6797 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6798 if (c->display_usage) {
6799 d_printf("Usage:\n"
6800 "net rpc samdump\n"
6801 " Dump remote SAM database\n");
6802 return 0;
6805 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6806 rpc_samdump_internals, argc, argv);
6809 /* syncronise sam database via samsync rpc calls */
6810 static int rpc_vampire(struct net_context *c, int argc, const char **argv) {
6811 if (c->display_usage) {
6812 d_printf("Usage:\n"
6813 "net rpc vampire\n"
6814 " Vampire remote SAM database\n");
6815 return 0;
6818 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6819 rpc_vampire_internals, argc, argv);
6823 * Migrate everything from a print server.
6825 * @param c A net_context structure.
6826 * @param argc Standard main() style argc.
6827 * @param argv Standard main() style argv. Initial components are already
6828 * stripped.
6830 * @return A shell status integer (0 for success).
6832 * The order is important !
6833 * To successfully add drivers the print queues have to exist !
6834 * Applying ACLs should be the last step, because you're easily locked out.
6837 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6838 const char **argv)
6840 int ret;
6842 if (c->display_usage) {
6843 d_printf("Usage:\n"
6844 "net rpc printer migrate all\n"
6845 " Migrate everything from a print server\n");
6846 return 0;
6849 if (!c->opt_host) {
6850 d_printf("no server to migrate\n");
6851 return -1;
6854 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6855 rpc_printer_migrate_printers_internals, argc,
6856 argv);
6857 if (ret)
6858 return ret;
6860 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6861 rpc_printer_migrate_drivers_internals, argc,
6862 argv);
6863 if (ret)
6864 return ret;
6866 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6867 rpc_printer_migrate_forms_internals, argc, argv);
6868 if (ret)
6869 return ret;
6871 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6872 rpc_printer_migrate_settings_internals, argc,
6873 argv);
6874 if (ret)
6875 return ret;
6877 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6878 rpc_printer_migrate_security_internals, argc,
6879 argv);
6884 * Migrate print drivers from a print server.
6886 * @param c A net_context structure.
6887 * @param argc Standard main() style argc.
6888 * @param argv Standard main() style argv. Initial components are already
6889 * stripped.
6891 * @return A shell status integer (0 for success).
6893 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6894 const char **argv)
6896 if (c->display_usage) {
6897 d_printf("Usage:\n"
6898 "net rpc printer migrate drivers\n"
6899 " Migrate print-drivers from a print-server\n");
6900 return 0;
6903 if (!c->opt_host) {
6904 d_printf("no server to migrate\n");
6905 return -1;
6908 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6909 rpc_printer_migrate_drivers_internals,
6910 argc, argv);
6914 * Migrate print-forms from a print-server.
6916 * @param c A net_context structure.
6917 * @param argc Standard main() style argc.
6918 * @param argv Standard main() style argv. Initial components are already
6919 * stripped.
6921 * @return A shell status integer (0 for success).
6923 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6924 const char **argv)
6926 if (c->display_usage) {
6927 d_printf("Usage:\n"
6928 "net rpc printer migrate forms\n"
6929 " Migrate print-forms from a print-server\n");
6930 return 0;
6933 if (!c->opt_host) {
6934 d_printf("no server to migrate\n");
6935 return -1;
6938 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6939 rpc_printer_migrate_forms_internals,
6940 argc, argv);
6944 * Migrate printers from a print-server.
6946 * @param c A net_context structure.
6947 * @param argc Standard main() style argc.
6948 * @param argv Standard main() style argv. Initial components are already
6949 * stripped.
6951 * @return A shell status integer (0 for success).
6953 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6954 const char **argv)
6956 if (c->display_usage) {
6957 d_printf("Usage:\n"
6958 "net rpc printer migrate printers\n"
6959 " Migrate printers from a print-server\n");
6960 return 0;
6963 if (!c->opt_host) {
6964 d_printf("no server to migrate\n");
6965 return -1;
6968 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6969 rpc_printer_migrate_printers_internals,
6970 argc, argv);
6974 * Migrate printer-ACLs from a print-server
6976 * @param c A net_context structure.
6977 * @param argc Standard main() style argc.
6978 * @param argv Standard main() style argv. Initial components are already
6979 * stripped.
6981 * @return A shell status integer (0 for success).
6983 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6984 const char **argv)
6986 if (c->display_usage) {
6987 d_printf("Usage:\n"
6988 "net rpc printer migrate security\n"
6989 " Migrate printer-ACLs from a print-server\n");
6990 return 0;
6993 if (!c->opt_host) {
6994 d_printf("no server to migrate\n");
6995 return -1;
6998 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6999 rpc_printer_migrate_security_internals,
7000 argc, argv);
7004 * Migrate printer-settings from a print-server.
7006 * @param c A net_context structure.
7007 * @param argc Standard main() style argc.
7008 * @param argv Standard main() style argv. Initial components are already
7009 * stripped.
7011 * @return A shell status integer (0 for success).
7013 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7014 const char **argv)
7016 if (c->display_usage) {
7017 d_printf("Usage:\n"
7018 "net rpc printer migrate settings\n"
7019 " Migrate printer-settings from a print-server\n");
7020 return 0;
7023 if (!c->opt_host) {
7024 d_printf("no server to migrate\n");
7025 return -1;
7028 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7029 rpc_printer_migrate_settings_internals,
7030 argc, argv);
7034 * 'net rpc printer' entrypoint.
7036 * @param c A net_context structure.
7037 * @param argc Standard main() style argc.
7038 * @param argv Standard main() style argv. Initial components are already
7039 * stripped.
7042 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7045 /* ouch: when addriver and setdriver are called from within
7046 rpc_printer_migrate_drivers_internals, the printer-queue already
7047 *has* to exist */
7049 struct functable func[] = {
7051 "all",
7052 rpc_printer_migrate_all,
7053 NET_TRANSPORT_RPC,
7054 "Migrate all from remote to local print server",
7055 "net rpc printer migrate all\n"
7056 " Migrate all from remote to local print server"
7059 "drivers",
7060 rpc_printer_migrate_drivers,
7061 NET_TRANSPORT_RPC,
7062 "Migrate drivers to local server",
7063 "net rpc printer migrate drivers\n"
7064 " Migrate drivers to local server"
7067 "forms",
7068 rpc_printer_migrate_forms,
7069 NET_TRANSPORT_RPC,
7070 "Migrate froms to local server",
7071 "net rpc printer migrate forms\n"
7072 " Migrate froms to local server"
7075 "printers",
7076 rpc_printer_migrate_printers,
7077 NET_TRANSPORT_RPC,
7078 "Migrate printers to local server",
7079 "net rpc printer migrate printers\n"
7080 " Migrate printers to local server"
7083 "security",
7084 rpc_printer_migrate_security,
7085 NET_TRANSPORT_RPC,
7086 "Mirgate printer ACLs to local server",
7087 "net rpc printer migrate security\n"
7088 " Mirgate printer ACLs to local server"
7091 "settings",
7092 rpc_printer_migrate_settings,
7093 NET_TRANSPORT_RPC,
7094 "Migrate printer settings to local server",
7095 "net rpc printer migrate settings\n"
7096 " Migrate printer settings to local server"
7098 {NULL, NULL, 0, NULL, NULL}
7101 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7106 * List printers on a remote RPC server.
7108 * @param c A net_context structure.
7109 * @param argc Standard main() style argc.
7110 * @param argv Standard main() style argv. Initial components are already
7111 * stripped.
7113 * @return A shell status integer (0 for success).
7115 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7117 if (c->display_usage) {
7118 d_printf("Usage:\n"
7119 "net rpc printer list\n"
7120 " List printers on a remote RPC server\n");
7121 return 0;
7124 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7125 rpc_printer_list_internals,
7126 argc, argv);
7130 * List printer-drivers on a remote RPC server.
7132 * @param c A net_context structure.
7133 * @param argc Standard main() style argc.
7134 * @param argv Standard main() style argv. Initial components are already
7135 * stripped.
7137 * @return A shell status integer (0 for success).
7139 static int rpc_printer_driver_list(struct net_context *c, int argc,
7140 const char **argv)
7142 if (c->display_usage) {
7143 d_printf("Usage:\n"
7144 "net rpc printer driver\n"
7145 " List printer-drivers on a remote RPC server\n");
7146 return 0;
7149 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7150 rpc_printer_driver_list_internals,
7151 argc, argv);
7155 * Publish printer in ADS via MSRPC.
7157 * @param c A net_context structure.
7158 * @param argc Standard main() style argc.
7159 * @param argv Standard main() style argv. Initial components are already
7160 * stripped.
7162 * @return A shell status integer (0 for success).
7164 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7165 const char **argv)
7167 if (c->display_usage) {
7168 d_printf("Usage:\n"
7169 "net rpc printer publish publish\n"
7170 " Publish printer in ADS via MSRPC\n");
7171 return 0;
7174 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7175 rpc_printer_publish_publish_internals,
7176 argc, argv);
7180 * Update printer in ADS via MSRPC.
7182 * @param c A net_context structure.
7183 * @param argc Standard main() style argc.
7184 * @param argv Standard main() style argv. Initial components are already
7185 * stripped.
7187 * @return A shell status integer (0 for success).
7189 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7191 if (c->display_usage) {
7192 d_printf("Usage:\n"
7193 "net rpc printer publish update\n"
7194 " Update printer in ADS via MSRPC\n");
7195 return 0;
7198 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7199 rpc_printer_publish_update_internals,
7200 argc, argv);
7204 * UnPublish printer in ADS via MSRPC.
7206 * @param c A net_context structure.
7207 * @param argc Standard main() style argc.
7208 * @param argv Standard main() style argv. Initial components are already
7209 * stripped.
7211 * @return A shell status integer (0 for success).
7213 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7214 const char **argv)
7216 if (c->display_usage) {
7217 d_printf("Usage:\n"
7218 "net rpc printer publish unpublish\n"
7219 " UnPublish printer in ADS via MSRPC\n");
7220 return 0;
7223 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7224 rpc_printer_publish_unpublish_internals,
7225 argc, argv);
7229 * List published printers via MSRPC.
7231 * @param c A net_context structure.
7232 * @param argc Standard main() style argc.
7233 * @param argv Standard main() style argv. Initial components are already
7234 * stripped.
7236 * @return A shell status integer (0 for success).
7238 static int rpc_printer_publish_list(struct net_context *c, int argc,
7239 const char **argv)
7241 if (c->display_usage) {
7242 d_printf("Usage:\n"
7243 "net rpc printer publish list\n"
7244 " List published printers via MSRPC\n");
7245 return 0;
7248 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7249 rpc_printer_publish_list_internals,
7250 argc, argv);
7255 * Publish printer in ADS.
7257 * @param c A net_context structure.
7258 * @param argc Standard main() style argc.
7259 * @param argv Standard main() style argv. Initial components are already
7260 * stripped.
7262 * @return A shell status integer (0 for success).
7264 static int rpc_printer_publish(struct net_context *c, int argc,
7265 const char **argv)
7268 struct functable func[] = {
7270 "publish",
7271 rpc_printer_publish_publish,
7272 NET_TRANSPORT_RPC,
7273 "Publish printer in AD",
7274 "net rpc printer publish publish\n"
7275 " Publish printer in AD"
7278 "update",
7279 rpc_printer_publish_update,
7280 NET_TRANSPORT_RPC,
7281 "Update printer in AD",
7282 "net rpc printer publish update\n"
7283 " Update printer in AD"
7286 "unpublish",
7287 rpc_printer_publish_unpublish,
7288 NET_TRANSPORT_RPC,
7289 "Unpublish printer",
7290 "net rpc printer publish unpublish\n"
7291 " Unpublish printer"
7294 "list",
7295 rpc_printer_publish_list,
7296 NET_TRANSPORT_RPC,
7297 "List published printers",
7298 "net rpc printer publish list\n"
7299 " List published printers"
7301 {NULL, NULL, 0, NULL, NULL}
7304 if (argc == 0) {
7305 if (c->display_usage) {
7306 d_printf("Usage:\n");
7307 d_printf("net rpc printer publish\n"
7308 " List published printers\n"
7309 " Alias of net rpc printer publish list\n");
7310 net_display_usage_from_functable(func);
7311 return 0;
7313 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7314 rpc_printer_publish_list_internals,
7315 argc, argv);
7318 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7324 * Display rpc printer help page.
7326 * @param c A net_context structure.
7327 * @param argc Standard main() style argc.
7328 * @param argv Standard main() style argv. Initial components are already
7329 * stripped.
7331 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7333 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
7334 "\tlists all printers on print-server\n\n");
7335 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7336 "\tlists all printer-drivers on print-server\n\n");
7337 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7338 "\tpublishes printer settings in Active Directory\n"
7339 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
7340 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7341 "\n\tmigrates printers from remote to local server\n\n");
7342 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7343 "\n\tmigrates printer-settings from remote to local server\n\n");
7344 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7345 "\n\tmigrates printer-drivers from remote to local server\n\n");
7346 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7347 "\n\tmigrates printer-forms from remote to local server\n\n");
7348 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7349 "\n\tmigrates printer-ACLs from remote to local server\n\n");
7350 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7351 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7352 "\tremote to local print-server\n\n");
7353 net_common_methods_usage(c, argc, argv);
7354 net_common_flags_usage(c, argc, argv);
7355 d_printf(
7356 "\t-v or --verbose\t\t\tgive verbose output\n"
7357 "\t --destination\t\tmigration target server (default: localhost)\n");
7359 return -1;
7363 * 'net rpc printer' entrypoint.
7365 * @param c A net_context structure.
7366 * @param argc Standard main() style argc.
7367 * @param argv Standard main() style argv. Initial components are already
7368 * stripped.
7370 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7372 struct functable func[] = {
7374 "list",
7375 rpc_printer_list,
7376 NET_TRANSPORT_RPC,
7377 "List all printers on print server",
7378 "net rpc printer list\n"
7379 " List all printers on print server"
7382 "migrate",
7383 rpc_printer_migrate,
7384 NET_TRANSPORT_RPC,
7385 "Migrate printer to local server",
7386 "net rpc printer migrate\n"
7387 " Migrate printer to local server"
7390 "driver",
7391 rpc_printer_driver_list,
7392 NET_TRANSPORT_RPC,
7393 "List printer drivers",
7394 "net rpc printer driver\n"
7395 " List printer drivers"
7398 "publish",
7399 rpc_printer_publish,
7400 NET_TRANSPORT_RPC,
7401 "Publish printer in AD",
7402 "net rpc printer publish\n"
7403 " Publish printer in AD"
7405 {NULL, NULL, 0, NULL, NULL}
7408 if (argc == 0) {
7409 if (c->display_usage) {
7410 d_printf("Usage:\n");
7411 d_printf("net rpc printer\n"
7412 " List printers\n");
7413 net_display_usage_from_functable(func);
7414 return 0;
7416 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7417 rpc_printer_list_internals,
7418 argc, argv);
7421 return net_run_function(c, argc, argv, "net rpc printer", func);
7425 * 'net rpc' entrypoint.
7427 * @param c A net_context structure.
7428 * @param argc Standard main() style argc.
7429 * @param argv Standard main() style argv. Initial components are already
7430 * stripped.
7433 int net_rpc(struct net_context *c, int argc, const char **argv)
7435 struct functable func[] = {
7437 "audit",
7438 net_rpc_audit,
7439 NET_TRANSPORT_RPC,
7440 "Modify global audit settings",
7441 "net rpc audit\n"
7442 " Modify global audit settings"
7445 "info",
7446 net_rpc_info,
7447 NET_TRANSPORT_RPC,
7448 "Show basic info about a domain",
7449 "net rpc info\n"
7450 " Show basic info about a domain"
7453 "join",
7454 net_rpc_join,
7455 NET_TRANSPORT_RPC,
7456 "Join a domain",
7457 "net rpc join\n"
7458 " Join a domain"
7461 "oldjoin",
7462 net_rpc_oldjoin,
7463 NET_TRANSPORT_RPC,
7464 "Join a domain created in server manager",
7465 "net rpc oldjoin\n"
7466 " Join a domain created in server manager"
7469 "testjoin",
7470 net_rpc_testjoin,
7471 NET_TRANSPORT_RPC,
7472 "Test that a join is valid",
7473 "net rpc testjoin\n"
7474 " Test that a join is valid"
7477 "user",
7478 net_rpc_user,
7479 NET_TRANSPORT_RPC,
7480 "List/modify users",
7481 "net rpc user\n"
7482 " List/modify users"
7485 "password",
7486 rpc_user_password,
7487 NET_TRANSPORT_RPC,
7488 "Change a user password",
7489 "net rpc password\n"
7490 " Change a user password\n"
7491 " Alias for net rpc user password"
7494 "group",
7495 net_rpc_group,
7496 NET_TRANSPORT_RPC,
7497 "List/modify groups",
7498 "net rpc group\n"
7499 " List/modify groups"
7502 "share",
7503 net_rpc_share,
7504 NET_TRANSPORT_RPC,
7505 "List/modify shares",
7506 "net rpc share\n"
7507 " List/modify shares"
7510 "file",
7511 net_rpc_file,
7512 NET_TRANSPORT_RPC,
7513 "List open files",
7514 "net rpc file\n"
7515 " List open files"
7518 "printer",
7519 net_rpc_printer,
7520 NET_TRANSPORT_RPC,
7521 "List/modify printers",
7522 "net rpc printer\n"
7523 " List/modify printers"
7526 "changetrustpw",
7527 net_rpc_changetrustpw,
7528 NET_TRANSPORT_RPC,
7529 "Change trust account password",
7530 "net rpc changetrustpw\n"
7531 " Change trust account password"
7534 "trustdom",
7535 rpc_trustdom,
7536 NET_TRANSPORT_RPC,
7537 "Modify domain trusts",
7538 "net rpc trustdom\n"
7539 " Modify domain trusts"
7542 "abortshutdown",
7543 rpc_shutdown_abort,
7544 NET_TRANSPORT_RPC,
7545 "Abort a remote shutdown",
7546 "net rpc abortshutdown\n"
7547 " Abort a remote shutdown"
7550 "shutdown",
7551 rpc_shutdown,
7552 NET_TRANSPORT_RPC,
7553 "Shutdown a remote server",
7554 "net rpc shutdown\n"
7555 " Shutdown a remote server"
7558 "samdump",
7559 rpc_samdump,
7560 NET_TRANSPORT_RPC,
7561 "Dump SAM data of remote NT PDC",
7562 "net rpc samdump\n"
7563 " Dump SAM data of remote NT PDC"
7566 "vampire",
7567 rpc_vampire,
7568 NET_TRANSPORT_RPC,
7569 "Sync a remote NT PDC's data into local passdb",
7570 "net rpc vampire\n"
7571 " Sync a remote NT PDC's data into local passdb"
7574 "getsid",
7575 net_rpc_getsid,
7576 NET_TRANSPORT_RPC,
7577 "Fetch the domain sid into local secrets.tdb",
7578 "net rpc getsid\n"
7579 " Fetch the domain sid into local secrets.tdb"
7582 "rights",
7583 net_rpc_rights,
7584 NET_TRANSPORT_RPC,
7585 "Manage privileges assigned to SID",
7586 "net rpc rights\n"
7587 " Manage privileges assigned to SID"
7590 "service",
7591 net_rpc_service,
7592 NET_TRANSPORT_RPC,
7593 "Start/stop/query remote services",
7594 "net rpc service\n"
7595 " Start/stop/query remote services"
7598 "registry",
7599 net_rpc_registry,
7600 NET_TRANSPORT_RPC,
7601 "Manage registry hives",
7602 "net rpc registry\n"
7603 " Manage registry hives"
7606 "shell",
7607 net_rpc_shell,
7608 NET_TRANSPORT_RPC,
7609 "Open interactive shell on remote server",
7610 "net rpc shell\n"
7611 " Open interactive shell on remote server"
7613 {NULL, NULL, 0, NULL, NULL}
7615 return net_run_function(c, argc, argv, "net rpc", func);