WHATSNEW: Update changes since 3.4.4.
[Samba/gbeck.git] / source3 / utils / net_rpc.c
blob75726b0d61793c66ca02bd79ce0368942f833a2c
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 = NULL;
58 struct policy_handle pol;
59 NTSTATUS result = NT_STATUS_OK;
60 union lsa_PolicyInformation *info = NULL;
62 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
63 &lsa_pipe);
64 if (!NT_STATUS_IS_OK(result)) {
65 d_fprintf(stderr, "Could not initialise lsa pipe\n");
66 return result;
69 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
70 SEC_FLAG_MAXIMUM_ALLOWED,
71 &pol);
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, "open_policy failed: %s\n",
74 nt_errstr(result));
75 return result;
78 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
79 &pol,
80 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
81 &info);
82 if (!NT_STATUS_IS_OK(result)) {
83 d_fprintf(stderr, "lsaquery failed: %s\n",
84 nt_errstr(result));
85 return result;
88 *domain_name = info->account_domain.name.string;
89 *domain_sid = info->account_domain.sid;
91 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
92 TALLOC_FREE(lsa_pipe);
94 return NT_STATUS_OK;
97 /**
98 * Run a single RPC command, from start to finish.
100 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
101 * @param conn_flag a NET_FLAG_ combination. Passed to
102 * net_make_ipc_connection.
103 * @param argc Standard main() style argc.
104 * @param argv Standard main() style argv. Initial components are already
105 * stripped.
106 * @return A shell status integer (0 for success).
109 int run_rpc_command(struct net_context *c,
110 struct cli_state *cli_arg,
111 const struct ndr_syntax_id *interface,
112 int conn_flags,
113 rpc_command_fn fn,
114 int argc,
115 const char **argv)
117 struct cli_state *cli = NULL;
118 struct rpc_pipe_client *pipe_hnd = NULL;
119 TALLOC_CTX *mem_ctx;
120 NTSTATUS nt_status;
121 DOM_SID *domain_sid;
122 const char *domain_name;
123 int ret = -1;
125 /* make use of cli_state handed over as an argument, if possible */
126 if (!cli_arg) {
127 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
128 if (!NT_STATUS_IS_OK(nt_status)) {
129 DEBUG(1, ("failed to make ipc connection: %s\n",
130 nt_errstr(nt_status)));
131 return -1;
133 } else {
134 cli = cli_arg;
137 if (!cli) {
138 return -1;
141 /* Create mem_ctx */
143 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
144 DEBUG(0, ("talloc_init() failed\n"));
145 goto fail;
148 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
149 &domain_name);
150 if (!NT_STATUS_IS_OK(nt_status)) {
151 goto fail;
154 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
155 if (lp_client_schannel()
156 && (ndr_syntax_id_equal(interface,
157 &ndr_table_netlogon.syntax_id))) {
158 /* Always try and create an schannel netlogon pipe. */
159 nt_status = cli_rpc_pipe_open_schannel(
160 cli, interface, NCACN_NP,
161 PIPE_AUTH_LEVEL_PRIVACY, domain_name,
162 &pipe_hnd);
163 if (!NT_STATUS_IS_OK(nt_status)) {
164 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
165 nt_errstr(nt_status) ));
166 goto fail;
168 } else {
169 if (conn_flags & NET_FLAGS_SEAL) {
170 nt_status = cli_rpc_pipe_open_ntlmssp(
171 cli, interface,
172 (conn_flags & NET_FLAGS_TCP) ?
173 NCACN_IP_TCP : NCACN_NP,
174 PIPE_AUTH_LEVEL_PRIVACY,
175 lp_workgroup(), c->opt_user_name,
176 c->opt_password, &pipe_hnd);
177 } else {
178 nt_status = cli_rpc_pipe_open_noauth(
179 cli, interface,
180 &pipe_hnd);
182 if (!NT_STATUS_IS_OK(nt_status)) {
183 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
184 get_pipe_name_from_iface(interface),
185 nt_errstr(nt_status) ));
186 goto fail;
191 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
193 if (!NT_STATUS_IS_OK(nt_status)) {
194 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
195 } else {
196 ret = 0;
197 DEBUG(5, ("rpc command function succedded\n"));
200 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
201 if (pipe_hnd) {
202 TALLOC_FREE(pipe_hnd);
206 fail:
207 /* close the connection only if it was opened here */
208 if (!cli_arg) {
209 cli_shutdown(cli);
212 talloc_destroy(mem_ctx);
213 return ret;
217 * Force a change of the trust acccount password.
219 * All parameters are provided by the run_rpc_command function, except for
220 * argc, argv which are passed through.
222 * @param domain_sid The domain sid acquired from the remote server.
223 * @param cli A cli_state connected to the server.
224 * @param mem_ctx Talloc context, destroyed on completion of the function.
225 * @param argc Standard main() style argc.
226 * @param argv Standard main() style argv. Initial components are already
227 * stripped.
229 * @return Normal NTSTATUS return.
232 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
233 const DOM_SID *domain_sid,
234 const char *domain_name,
235 struct cli_state *cli,
236 struct rpc_pipe_client *pipe_hnd,
237 TALLOC_CTX *mem_ctx,
238 int argc,
239 const char **argv)
242 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
246 * Force a change of the trust acccount password.
248 * @param argc Standard main() style argc.
249 * @param argv Standard main() style argv. Initial components are already
250 * stripped.
252 * @return A shell status integer (0 for success).
255 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
257 if (c->display_usage) {
258 d_printf("Usage:\n"
259 "net rpc changetrustpw\n"
260 " Change the machine trust password\n");
261 return 0;
264 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
265 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
266 rpc_changetrustpw_internals,
267 argc, argv);
271 * Join a domain, the old way.
273 * This uses 'machinename' as the inital password, and changes it.
275 * The password should be created with 'server manager' or equiv first.
277 * All parameters are provided by the run_rpc_command function, except for
278 * argc, argv which are passed through.
280 * @param domain_sid The domain sid acquired from the remote server.
281 * @param cli A cli_state connected to the server.
282 * @param mem_ctx Talloc context, destroyed on completion of the function.
283 * @param argc Standard main() style argc.
284 * @param argv Standard main() style argv. Initial components are already
285 * stripped.
287 * @return Normal NTSTATUS return.
290 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
291 const DOM_SID *domain_sid,
292 const char *domain_name,
293 struct cli_state *cli,
294 struct rpc_pipe_client *pipe_hnd,
295 TALLOC_CTX *mem_ctx,
296 int argc,
297 const char **argv)
300 fstring trust_passwd;
301 unsigned char orig_trust_passwd_hash[16];
302 NTSTATUS result;
303 uint32 sec_channel_type;
305 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
306 &pipe_hnd);
307 if (!NT_STATUS_IS_OK(result)) {
308 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
309 "error was %s\n",
310 cli->desthost,
311 nt_errstr(result) ));
312 return result;
316 check what type of join - if the user want's to join as
317 a BDC, the server must agree that we are a BDC.
319 if (argc >= 0) {
320 sec_channel_type = get_sec_channel_type(argv[0]);
321 } else {
322 sec_channel_type = get_sec_channel_type(NULL);
325 fstrcpy(trust_passwd, global_myname());
326 strlower_m(trust_passwd);
329 * Machine names can be 15 characters, but the max length on
330 * a password is 14. --jerry
333 trust_passwd[14] = '\0';
335 E_md4hash(trust_passwd, orig_trust_passwd_hash);
337 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
338 orig_trust_passwd_hash,
339 sec_channel_type);
341 if (NT_STATUS_IS_OK(result))
342 printf("Joined domain %s.\n", c->opt_target_workgroup);
345 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
346 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
347 result = NT_STATUS_UNSUCCESSFUL;
350 return result;
354 * Join a domain, the old way.
356 * @param argc Standard main() style argc.
357 * @param argv Standard main() style argv. Initial components are already
358 * stripped.
360 * @return A shell status integer (0 for success).
363 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
365 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
366 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
367 rpc_oldjoin_internals,
368 argc, argv);
372 * Join a domain, the old way. This function exists to allow
373 * the message to be displayed when oldjoin was explicitly
374 * requested, but not when it was implied by "net rpc join".
376 * @param argc Standard main() style argc.
377 * @param argv Standard main() style argv. Initial components are already
378 * stripped.
380 * @return A shell status integer (0 for success).
383 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
385 int rc = -1;
387 if (c->display_usage) {
388 d_printf("Usage:\n"
389 "net rpc oldjoin\n"
390 " Join a domain the old way\n");
391 return 0;
394 rc = net_rpc_perform_oldjoin(c, argc, argv);
396 if (rc) {
397 d_fprintf(stderr, "Failed to join domain\n");
400 return rc;
404 * 'net rpc join' entrypoint.
405 * @param argc Standard main() style argc.
406 * @param argv Standard main() style argv. Initial components are already
407 * stripped
409 * Main 'net_rpc_join()' (where the admin username/password is used) is
410 * in net_rpc_join.c.
411 * Try to just change the password, but if that doesn't work, use/prompt
412 * for a username/password.
415 int net_rpc_join(struct net_context *c, int argc, const char **argv)
417 if (c->display_usage) {
418 d_printf("Usage:\n"
419 "net rpc join -U <username>[%%password] <type>\n"
420 " Join a domain\n"
421 " username\tName of the admin user"
422 " password\tPassword of the admin user, will "
423 "prompt if not specified\n"
424 " type\tCan be one of the following:\n"
425 "\t\tMEMBER\tJoin as member server (default)\n"
426 "\t\tBDC\tJoin as BDC\n"
427 "\t\tPDC\tJoin as PDC\n");
428 return 0;
431 if (lp_server_role() == ROLE_STANDALONE) {
432 d_printf("cannot join as standalone machine\n");
433 return -1;
436 if (strlen(global_myname()) > 15) {
437 d_printf("Our netbios name can be at most 15 chars long, "
438 "\"%s\" is %u chars long\n",
439 global_myname(), (unsigned int)strlen(global_myname()));
440 return -1;
443 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
444 return 0;
446 return net_rpc_join_newstyle(c, argc, argv);
450 * display info about a rpc domain
452 * All parameters are provided by the run_rpc_command function, except for
453 * argc, argv which are passed through.
455 * @param domain_sid The domain sid acquired from the remote server
456 * @param cli A cli_state connected to the server.
457 * @param mem_ctx Talloc context, destroyed on completion of the function.
458 * @param argc Standard main() style argc.
459 * @param argv Standard main() style argv. Initial components are already
460 * stripped.
462 * @return Normal NTSTATUS return.
465 NTSTATUS rpc_info_internals(struct net_context *c,
466 const DOM_SID *domain_sid,
467 const char *domain_name,
468 struct cli_state *cli,
469 struct rpc_pipe_client *pipe_hnd,
470 TALLOC_CTX *mem_ctx,
471 int argc,
472 const char **argv)
474 struct policy_handle connect_pol, domain_pol;
475 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
476 union samr_DomainInfo *info = NULL;
477 fstring sid_str;
479 sid_to_fstring(sid_str, domain_sid);
481 /* Get sam policy handle */
482 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
483 pipe_hnd->desthost,
484 MAXIMUM_ALLOWED_ACCESS,
485 &connect_pol);
486 if (!NT_STATUS_IS_OK(result)) {
487 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
488 goto done;
491 /* Get domain policy handle */
492 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
493 &connect_pol,
494 MAXIMUM_ALLOWED_ACCESS,
495 CONST_DISCARD(struct dom_sid2 *, domain_sid),
496 &domain_pol);
497 if (!NT_STATUS_IS_OK(result)) {
498 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
499 goto done;
502 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
503 &domain_pol,
505 &info);
506 if (NT_STATUS_IS_OK(result)) {
507 d_printf("Domain Name: %s\n", info->general.domain_name.string);
508 d_printf("Domain SID: %s\n", sid_str);
509 d_printf("Sequence number: %llu\n",
510 (unsigned long long)info->general.sequence_num);
511 d_printf("Num users: %u\n", info->general.num_users);
512 d_printf("Num domain groups: %u\n", info->general.num_groups);
513 d_printf("Num local groups: %u\n", info->general.num_aliases);
516 done:
517 return result;
521 * 'net rpc info' entrypoint.
522 * @param argc Standard main() style argc.
523 * @param argv Standard main() style argv. Initial components are already
524 * stripped.
527 int net_rpc_info(struct net_context *c, int argc, const char **argv)
529 if (c->display_usage) {
530 d_printf("Usage:\n"
531 "net rpc info\n"
532 " Display information about the domain\n");
533 return 0;
536 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
537 NET_FLAGS_PDC, rpc_info_internals,
538 argc, argv);
542 * Fetch domain SID into the local secrets.tdb.
544 * All parameters are provided by the run_rpc_command function, except for
545 * argc, argv which are passed through.
547 * @param domain_sid The domain sid acquired from the remote server.
548 * @param cli A cli_state connected to the server.
549 * @param mem_ctx Talloc context, destroyed on completion of the function.
550 * @param argc Standard main() style argc.
551 * @param argv Standard main() style argv. Initial components are already
552 * stripped.
554 * @return Normal NTSTATUS return.
557 static NTSTATUS rpc_getsid_internals(struct net_context *c,
558 const DOM_SID *domain_sid,
559 const char *domain_name,
560 struct cli_state *cli,
561 struct rpc_pipe_client *pipe_hnd,
562 TALLOC_CTX *mem_ctx,
563 int argc,
564 const char **argv)
566 fstring sid_str;
568 sid_to_fstring(sid_str, domain_sid);
569 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
570 sid_str, domain_name);
572 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
573 DEBUG(0,("Can't store domain SID\n"));
574 return NT_STATUS_UNSUCCESSFUL;
577 return NT_STATUS_OK;
581 * 'net rpc getsid' entrypoint.
582 * @param argc Standard main() style argc.
583 * @param argv Standard main() style argv. Initial components are already
584 * stripped.
587 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
589 int conn_flags = NET_FLAGS_PDC;
591 if (!c->opt_user_specified) {
592 conn_flags |= NET_FLAGS_ANONYMOUS;
595 if (c->display_usage) {
596 d_printf("Usage:\n"
597 "net rpc getsid\n"
598 " Fetch domain SID into local secrets.tdb\n");
599 return 0;
602 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
603 conn_flags,
604 rpc_getsid_internals,
605 argc, argv);
608 /****************************************************************************/
611 * Basic usage function for 'net rpc user'.
612 * @param argc Standard main() style argc.
613 * @param argv Standard main() style argv. Initial components are already
614 * stripped.
617 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
619 return net_user_usage(c, argc, argv);
623 * Add a new user to a remote RPC server.
625 * @param argc Standard main() style argc.
626 * @param argv Standard main() style argv. Initial components are already
627 * stripped.
629 * @return A shell status integer (0 for success).
632 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
634 NET_API_STATUS status;
635 struct USER_INFO_1 info1;
636 uint32_t parm_error = 0;
638 if (argc < 1 || c->display_usage) {
639 rpc_user_usage(c, argc, argv);
640 return 0;
643 ZERO_STRUCT(info1);
645 info1.usri1_name = argv[0];
646 if (argc == 2) {
647 info1.usri1_password = argv[1];
650 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
652 if (status != 0) {
653 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
654 argv[0], libnetapi_get_error_string(c->netapi_ctx,
655 status));
656 return -1;
657 } else {
658 d_printf("Added user '%s'.\n", argv[0]);
661 return 0;
665 * Rename a user on a remote RPC server.
667 * @param argc Standard main() style argc.
668 * @param argv Standard main() style argv. Initial components are already
669 * stripped.
671 * @return A shell status integer (0 for success).
674 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
676 NET_API_STATUS status;
677 struct USER_INFO_0 u0;
678 uint32_t parm_err = 0;
680 if (argc != 2 || c->display_usage) {
681 rpc_user_usage(c, argc, argv);
682 return 0;
685 u0.usri0_name = argv[1];
687 status = NetUserSetInfo(c->opt_host, argv[0],
688 0, (uint8_t *)&u0, &parm_err);
689 if (status) {
690 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n",
691 argv[0], argv[1],
692 libnetapi_get_error_string(c->netapi_ctx, status));
693 } else {
694 d_printf("Renamed user from %s to %s\n", argv[0], argv[1]);
697 return status;
701 * Delete a user from a remote RPC server.
703 * @param argc Standard main() style argc.
704 * @param argv Standard main() style argv. Initial components are already
705 * stripped.
707 * @return A shell status integer (0 for success).
710 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
712 NET_API_STATUS status;
714 if (argc < 1 || c->display_usage) {
715 rpc_user_usage(c, argc, argv);
716 return 0;
719 status = NetUserDel(c->opt_host, argv[0]);
721 if (status != 0) {
722 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
723 argv[0],
724 libnetapi_get_error_string(c->netapi_ctx, status));
725 return -1;
726 } else {
727 d_printf("Deleted user '%s'.\n", argv[0]);
730 return 0;
734 * Set a user's password on a remote RPC server.
736 * @param argc Standard main() style argc.
737 * @param argv Standard main() style argv. Initial components are already
738 * stripped.
740 * @return A shell status integer (0 for success).
743 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
745 NET_API_STATUS status;
746 char *prompt = NULL;
747 struct USER_INFO_1003 u1003;
748 uint32_t parm_err = 0;
750 if (argc < 1 || c->display_usage) {
751 rpc_user_usage(c, argc, argv);
752 return 0;
755 if (argv[1]) {
756 u1003.usri1003_password = argv[1];
757 } else {
758 if (asprintf(&prompt, "Enter new password for %s:", argv[0]) == -1) {
759 return -1;
761 u1003.usri1003_password = getpass(prompt);
762 SAFE_FREE(prompt);
765 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
767 /* Display results */
768 if (status != 0) {
769 d_fprintf(stderr, "Failed to set password for '%s' with: %s.\n",
770 argv[0], libnetapi_get_error_string(c->netapi_ctx,
771 status));
772 return -1;
775 return 0;
779 * List a user's groups from a remote RPC server.
781 * @param argc Standard main() style argc.
782 * @param argv Standard main() style argv. Initial components are already
783 * stripped.
785 * @return A shell status integer (0 for success)
788 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
791 NET_API_STATUS status;
792 struct GROUP_USERS_INFO_0 *u0 = NULL;
793 uint32_t entries_read = 0;
794 uint32_t total_entries = 0;
795 int i;
798 if (argc < 1 || c->display_usage) {
799 rpc_user_usage(c, argc, argv);
800 return 0;
803 status = NetUserGetGroups(c->opt_host,
804 argv[0],
806 (uint8_t **)(void *)&u0,
807 (uint32_t)-1,
808 &entries_read,
809 &total_entries);
810 if (status != 0) {
811 d_fprintf(stderr, "Failed to get groups for '%s' with: %s.\n",
812 argv[0], libnetapi_get_error_string(c->netapi_ctx,
813 status));
814 return -1;
817 for (i=0; i < entries_read; i++) {
818 printf("%s\n", u0->grui0_name);
819 u0++;
822 return 0;
826 * List users on a remote RPC server.
828 * All parameters are provided by the run_rpc_command function, except for
829 * argc, argv which are passed through.
831 * @param domain_sid The domain sid acquired from the remote server.
832 * @param cli A cli_state connected to the server.
833 * @param mem_ctx Talloc context, destroyed on completion of the function.
834 * @param argc Standard main() style argc.
835 * @param argv Standard main() style argv. Initial components are already
836 * stripped.
838 * @return Normal NTSTATUS return.
841 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
843 NET_API_STATUS status;
844 uint32_t start_idx=0, num_entries, i, loop_count = 0;
845 struct NET_DISPLAY_USER *info = NULL;
846 void *buffer = NULL;
848 /* Query domain users */
849 if (c->opt_long_list_entries)
850 d_printf("\nUser name Comment"
851 "\n-----------------------------\n");
852 do {
853 uint32_t max_entries, max_size;
855 get_query_dispinfo_params(
856 loop_count, &max_entries, &max_size);
858 status = NetQueryDisplayInformation(c->opt_host,
860 start_idx,
861 max_entries,
862 max_size,
863 &num_entries,
864 &buffer);
865 if (status != 0 && status != ERROR_MORE_DATA) {
866 return status;
869 info = (struct NET_DISPLAY_USER *)buffer;
871 for (i = 0; i < num_entries; i++) {
873 if (c->opt_long_list_entries)
874 printf("%-21.21s %s\n", info->usri1_name,
875 info->usri1_comment);
876 else
877 printf("%s\n", info->usri1_name);
878 info++;
881 NetApiBufferFree(buffer);
883 loop_count++;
884 start_idx += num_entries;
886 } while (status == ERROR_MORE_DATA);
888 return status;
892 * 'net rpc user' entrypoint.
893 * @param argc Standard main() style argc.
894 * @param argv Standard main() style argv. Initial components are already
895 * stripped.
898 int net_rpc_user(struct net_context *c, int argc, const char **argv)
900 NET_API_STATUS status;
902 struct functable func[] = {
904 "add",
905 rpc_user_add,
906 NET_TRANSPORT_RPC,
907 "Add specified user",
908 "net rpc user add\n"
909 " Add specified user"
912 "info",
913 rpc_user_info,
914 NET_TRANSPORT_RPC,
915 "List domain groups of user",
916 "net rpc user info\n"
917 " Lis domain groups of user"
920 "delete",
921 rpc_user_delete,
922 NET_TRANSPORT_RPC,
923 "Remove specified user",
924 "net rpc user delete\n"
925 " Remove specified user"
928 "password",
929 rpc_user_password,
930 NET_TRANSPORT_RPC,
931 "Change user password",
932 "net rpc user password\n"
933 " Change user password"
936 "rename",
937 rpc_user_rename,
938 NET_TRANSPORT_RPC,
939 "Rename specified user",
940 "net rpc user rename\n"
941 " Rename specified user"
943 {NULL, NULL, 0, NULL, NULL}
946 status = libnetapi_init(&c->netapi_ctx);
947 if (status != 0) {
948 return -1;
950 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
951 libnetapi_set_password(c->netapi_ctx, c->opt_password);
952 if (c->opt_kerberos) {
953 libnetapi_set_use_kerberos(c->netapi_ctx);
956 if (argc == 0) {
957 if (c->display_usage) {
958 d_printf("Usage:\n");
959 d_printf("net rpc user\n"
960 " List all users\n");
961 net_display_usage_from_functable(func);
962 return 0;
965 return rpc_user_list(c, argc, argv);
968 return net_run_function(c, argc, argv, "net rpc user", func);
971 static NTSTATUS rpc_sh_user_list(struct net_context *c,
972 TALLOC_CTX *mem_ctx,
973 struct rpc_sh_ctx *ctx,
974 struct rpc_pipe_client *pipe_hnd,
975 int argc, const char **argv)
977 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
980 static NTSTATUS rpc_sh_user_info(struct net_context *c,
981 TALLOC_CTX *mem_ctx,
982 struct rpc_sh_ctx *ctx,
983 struct rpc_pipe_client *pipe_hnd,
984 int argc, const char **argv)
986 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
989 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
990 TALLOC_CTX *mem_ctx,
991 struct rpc_sh_ctx *ctx,
992 struct rpc_pipe_client *pipe_hnd,
993 int argc, const char **argv,
994 NTSTATUS (*fn)(
995 struct net_context *c,
996 TALLOC_CTX *mem_ctx,
997 struct rpc_sh_ctx *ctx,
998 struct rpc_pipe_client *pipe_hnd,
999 struct policy_handle *user_hnd,
1000 int argc, const char **argv))
1002 struct policy_handle connect_pol, domain_pol, user_pol;
1003 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1004 DOM_SID sid;
1005 uint32 rid;
1006 enum lsa_SidType type;
1008 if (argc == 0) {
1009 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1010 return NT_STATUS_INVALID_PARAMETER;
1013 ZERO_STRUCT(connect_pol);
1014 ZERO_STRUCT(domain_pol);
1015 ZERO_STRUCT(user_pol);
1017 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1018 argv[0], NULL, NULL, &sid, &type);
1019 if (!NT_STATUS_IS_OK(result)) {
1020 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1021 nt_errstr(result));
1022 goto done;
1025 if (type != SID_NAME_USER) {
1026 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1027 sid_type_lookup(type));
1028 result = NT_STATUS_NO_SUCH_USER;
1029 goto done;
1032 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1033 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1034 result = NT_STATUS_NO_SUCH_USER;
1035 goto done;
1038 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1039 pipe_hnd->desthost,
1040 MAXIMUM_ALLOWED_ACCESS,
1041 &connect_pol);
1042 if (!NT_STATUS_IS_OK(result)) {
1043 goto done;
1046 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1047 &connect_pol,
1048 MAXIMUM_ALLOWED_ACCESS,
1049 ctx->domain_sid,
1050 &domain_pol);
1051 if (!NT_STATUS_IS_OK(result)) {
1052 goto done;
1055 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1056 &domain_pol,
1057 MAXIMUM_ALLOWED_ACCESS,
1058 rid,
1059 &user_pol);
1060 if (!NT_STATUS_IS_OK(result)) {
1061 goto done;
1064 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1066 done:
1067 if (is_valid_policy_hnd(&user_pol)) {
1068 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1070 if (is_valid_policy_hnd(&domain_pol)) {
1071 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1073 if (is_valid_policy_hnd(&connect_pol)) {
1074 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1076 return result;
1079 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1080 TALLOC_CTX *mem_ctx,
1081 struct rpc_sh_ctx *ctx,
1082 struct rpc_pipe_client *pipe_hnd,
1083 struct policy_handle *user_hnd,
1084 int argc, const char **argv)
1086 NTSTATUS result;
1087 union samr_UserInfo *info = NULL;
1089 if (argc != 0) {
1090 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1091 return NT_STATUS_INVALID_PARAMETER;
1094 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1095 user_hnd,
1097 &info);
1098 if (!NT_STATUS_IS_OK(result)) {
1099 return result;
1102 d_printf("user rid: %d, group rid: %d\n",
1103 info->info21.rid,
1104 info->info21.primary_gid);
1106 return result;
1109 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1110 TALLOC_CTX *mem_ctx,
1111 struct rpc_sh_ctx *ctx,
1112 struct rpc_pipe_client *pipe_hnd,
1113 int argc, const char **argv)
1115 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1116 rpc_sh_user_show_internals);
1119 #define FETCHSTR(name, rec) \
1120 do { if (strequal(ctx->thiscmd, name)) { \
1121 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1122 } while (0);
1124 #define SETSTR(name, rec, flag) \
1125 do { if (strequal(ctx->thiscmd, name)) { \
1126 init_lsa_String(&(info->info21.rec), argv[0]); \
1127 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1128 } while (0);
1130 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1131 TALLOC_CTX *mem_ctx,
1132 struct rpc_sh_ctx *ctx,
1133 struct rpc_pipe_client *pipe_hnd,
1134 struct policy_handle *user_hnd,
1135 int argc, const char **argv)
1137 NTSTATUS result;
1138 const char *username;
1139 const char *oldval = "";
1140 union samr_UserInfo *info = NULL;
1142 if (argc > 1) {
1143 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1144 ctx->whoami);
1145 return NT_STATUS_INVALID_PARAMETER;
1148 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1149 user_hnd,
1151 &info);
1152 if (!NT_STATUS_IS_OK(result)) {
1153 return result;
1156 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1158 FETCHSTR("fullname", full_name);
1159 FETCHSTR("homedir", home_directory);
1160 FETCHSTR("homedrive", home_drive);
1161 FETCHSTR("logonscript", logon_script);
1162 FETCHSTR("profilepath", profile_path);
1163 FETCHSTR("description", description);
1165 if (argc == 0) {
1166 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1167 goto done;
1170 if (strcmp(argv[0], "NULL") == 0) {
1171 argv[0] = "";
1174 ZERO_STRUCT(info->info21);
1176 SETSTR("fullname", full_name, FULL_NAME);
1177 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1178 SETSTR("homedrive", home_drive, HOME_DRIVE);
1179 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1180 SETSTR("profilepath", profile_path, PROFILE_PATH);
1181 SETSTR("description", description, DESCRIPTION);
1183 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1184 user_hnd,
1186 info);
1188 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1189 ctx->thiscmd, oldval, argv[0]);
1191 done:
1193 return result;
1196 #define HANDLEFLG(name, rec) \
1197 do { if (strequal(ctx->thiscmd, name)) { \
1198 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1199 if (newval) { \
1200 newflags = oldflags | ACB_##rec; \
1201 } else { \
1202 newflags = oldflags & ~ACB_##rec; \
1203 } } } while (0);
1205 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1206 TALLOC_CTX *mem_ctx,
1207 struct rpc_sh_ctx *ctx,
1208 struct rpc_pipe_client *pipe_hnd,
1209 int argc, const char **argv)
1211 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1212 rpc_sh_user_str_edit_internals);
1215 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1216 TALLOC_CTX *mem_ctx,
1217 struct rpc_sh_ctx *ctx,
1218 struct rpc_pipe_client *pipe_hnd,
1219 struct policy_handle *user_hnd,
1220 int argc, const char **argv)
1222 NTSTATUS result;
1223 const char *username;
1224 const char *oldval = "unknown";
1225 uint32 oldflags, newflags;
1226 bool newval;
1227 union samr_UserInfo *info = NULL;
1229 if ((argc > 1) ||
1230 ((argc == 1) && !strequal(argv[0], "yes") &&
1231 !strequal(argv[0], "no"))) {
1232 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1233 ctx->whoami);
1234 return NT_STATUS_INVALID_PARAMETER;
1237 newval = strequal(argv[0], "yes");
1239 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1240 user_hnd,
1242 &info);
1243 if (!NT_STATUS_IS_OK(result)) {
1244 return result;
1247 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1248 oldflags = info->info21.acct_flags;
1249 newflags = info->info21.acct_flags;
1251 HANDLEFLG("disabled", DISABLED);
1252 HANDLEFLG("pwnotreq", PWNOTREQ);
1253 HANDLEFLG("autolock", AUTOLOCK);
1254 HANDLEFLG("pwnoexp", PWNOEXP);
1256 if (argc == 0) {
1257 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1258 goto done;
1261 ZERO_STRUCT(info->info21);
1263 info->info21.acct_flags = newflags;
1264 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1266 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1267 user_hnd,
1269 info);
1271 if (NT_STATUS_IS_OK(result)) {
1272 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1273 ctx->thiscmd, oldval, argv[0]);
1276 done:
1278 return result;
1281 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1282 TALLOC_CTX *mem_ctx,
1283 struct rpc_sh_ctx *ctx,
1284 struct rpc_pipe_client *pipe_hnd,
1285 int argc, const char **argv)
1287 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1288 rpc_sh_user_flag_edit_internals);
1291 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1292 TALLOC_CTX *mem_ctx,
1293 struct rpc_sh_ctx *ctx)
1295 static struct rpc_sh_cmd cmds[] = {
1297 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1298 "Show/Set a user's full name" },
1300 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1301 "Show/Set a user's home directory" },
1303 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1304 "Show/Set a user's home drive" },
1306 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1307 "Show/Set a user's logon script" },
1309 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1310 "Show/Set a user's profile path" },
1312 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1313 "Show/Set a user's description" },
1315 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1316 "Show/Set whether a user is disabled" },
1318 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1319 "Show/Set whether a user locked out" },
1321 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1322 "Show/Set whether a user does not need a password" },
1324 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1325 "Show/Set whether a user's password does not expire" },
1327 { NULL, NULL, 0, NULL, NULL }
1330 return cmds;
1333 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1334 TALLOC_CTX *mem_ctx,
1335 struct rpc_sh_ctx *ctx)
1337 static struct rpc_sh_cmd cmds[] = {
1339 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1340 "List available users" },
1342 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1343 "List the domain groups a user is member of" },
1345 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1346 "Show info about a user" },
1348 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1349 "Show/Modify a user's fields" },
1351 { NULL, NULL, 0, NULL, NULL }
1354 return cmds;
1357 /****************************************************************************/
1360 * Basic usage function for 'net rpc group'.
1361 * @param argc Standard main() style argc.
1362 * @param argv Standard main() style argv. Initial components are already
1363 * stripped.
1366 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1368 return net_group_usage(c, argc, argv);
1372 * Delete group on a remote RPC server.
1374 * All parameters are provided by the run_rpc_command function, except for
1375 * argc, argv which are passed through.
1377 * @param domain_sid The domain sid acquired from the remote server.
1378 * @param cli A cli_state connected to the server.
1379 * @param mem_ctx Talloc context, destroyed on completion of the function.
1380 * @param argc Standard main() style argc.
1381 * @param argv Standard main() style argv. Initial components are already
1382 * stripped.
1384 * @return Normal NTSTATUS return.
1387 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1388 const DOM_SID *domain_sid,
1389 const char *domain_name,
1390 struct cli_state *cli,
1391 struct rpc_pipe_client *pipe_hnd,
1392 TALLOC_CTX *mem_ctx,
1393 int argc,
1394 const char **argv)
1396 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1397 bool group_is_primary = false;
1398 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1399 uint32_t group_rid;
1400 struct samr_RidTypeArray *rids = NULL;
1401 /* char **names; */
1402 int i;
1403 /* struct samr_RidWithAttribute *user_gids; */
1405 struct samr_Ids group_rids, name_types;
1406 struct lsa_String lsa_acct_name;
1407 union samr_UserInfo *info = NULL;
1409 if (argc < 1 || c->display_usage) {
1410 rpc_group_usage(c, argc,argv);
1411 return NT_STATUS_OK; /* ok? */
1414 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1415 pipe_hnd->desthost,
1416 MAXIMUM_ALLOWED_ACCESS,
1417 &connect_pol);
1419 if (!NT_STATUS_IS_OK(result)) {
1420 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1421 goto done;
1424 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1425 &connect_pol,
1426 MAXIMUM_ALLOWED_ACCESS,
1427 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1428 &domain_pol);
1430 if (!NT_STATUS_IS_OK(result)) {
1431 d_fprintf(stderr, "Request open_domain failed\n");
1432 goto done;
1435 init_lsa_String(&lsa_acct_name, argv[0]);
1437 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1438 &domain_pol,
1440 &lsa_acct_name,
1441 &group_rids,
1442 &name_types);
1443 if (!NT_STATUS_IS_OK(result)) {
1444 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1445 goto done;
1448 switch (name_types.ids[0])
1450 case SID_NAME_DOM_GRP:
1451 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1452 &domain_pol,
1453 MAXIMUM_ALLOWED_ACCESS,
1454 group_rids.ids[0],
1455 &group_pol);
1456 if (!NT_STATUS_IS_OK(result)) {
1457 d_fprintf(stderr, "Request open_group failed");
1458 goto done;
1461 group_rid = group_rids.ids[0];
1463 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1464 &group_pol,
1465 &rids);
1467 if (!NT_STATUS_IS_OK(result)) {
1468 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1469 goto done;
1472 if (c->opt_verbose) {
1473 d_printf("Domain Group %s (rid: %d) has %d members\n",
1474 argv[0],group_rid, rids->count);
1477 /* Check if group is anyone's primary group */
1478 for (i = 0; i < rids->count; i++)
1480 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1481 &domain_pol,
1482 MAXIMUM_ALLOWED_ACCESS,
1483 rids->rids[i],
1484 &user_pol);
1486 if (!NT_STATUS_IS_OK(result)) {
1487 d_fprintf(stderr, "Unable to open group member %d\n",
1488 rids->rids[i]);
1489 goto done;
1492 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1493 &user_pol,
1495 &info);
1497 if (!NT_STATUS_IS_OK(result)) {
1498 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1499 rids->rids[i]);
1500 goto done;
1503 if (info->info21.primary_gid == group_rid) {
1504 if (c->opt_verbose) {
1505 d_printf("Group is primary group of %s\n",
1506 info->info21.account_name.string);
1508 group_is_primary = true;
1511 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1514 if (group_is_primary) {
1515 d_fprintf(stderr, "Unable to delete group because some "
1516 "of it's members have it as primary group\n");
1517 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1518 goto done;
1521 /* remove all group members */
1522 for (i = 0; i < rids->count; i++)
1524 if (c->opt_verbose)
1525 d_printf("Remove group member %d...",
1526 rids->rids[i]);
1527 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1528 &group_pol,
1529 rids->rids[i]);
1531 if (NT_STATUS_IS_OK(result)) {
1532 if (c->opt_verbose)
1533 d_printf("ok\n");
1534 } else {
1535 if (c->opt_verbose)
1536 d_printf("failed\n");
1537 goto done;
1541 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1542 &group_pol);
1544 break;
1545 /* removing a local group is easier... */
1546 case SID_NAME_ALIAS:
1547 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1548 &domain_pol,
1549 MAXIMUM_ALLOWED_ACCESS,
1550 group_rids.ids[0],
1551 &group_pol);
1553 if (!NT_STATUS_IS_OK(result)) {
1554 d_fprintf(stderr, "Request open_alias failed\n");
1555 goto done;
1558 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1559 &group_pol);
1560 break;
1561 default:
1562 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1563 argv[0],sid_type_lookup(name_types.ids[0]));
1564 result = NT_STATUS_UNSUCCESSFUL;
1565 goto done;
1568 if (NT_STATUS_IS_OK(result)) {
1569 if (c->opt_verbose)
1570 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1571 } else {
1572 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1573 get_friendly_nt_error_msg(result));
1576 done:
1577 return result;
1581 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1583 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1584 rpc_group_delete_internals, argc,argv);
1587 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1589 NET_API_STATUS status;
1590 struct GROUP_INFO_1 info1;
1591 uint32_t parm_error = 0;
1593 if (argc != 1 || c->display_usage) {
1594 rpc_group_usage(c, argc, argv);
1595 return 0;
1598 ZERO_STRUCT(info1);
1600 info1.grpi1_name = argv[0];
1601 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1602 info1.grpi1_comment = c->opt_comment;
1605 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1607 if (status != 0) {
1608 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1609 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1610 status));
1611 return -1;
1612 } else {
1613 d_printf("Added group '%s'.\n", argv[0]);
1616 return 0;
1619 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1621 NET_API_STATUS status;
1622 struct LOCALGROUP_INFO_1 info1;
1623 uint32_t parm_error = 0;
1625 if (argc != 1 || c->display_usage) {
1626 rpc_group_usage(c, argc, argv);
1627 return 0;
1630 ZERO_STRUCT(info1);
1632 info1.lgrpi1_name = argv[0];
1633 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1634 info1.lgrpi1_comment = c->opt_comment;
1637 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1639 if (status != 0) {
1640 d_fprintf(stderr, "Failed to add alias '%s' with: %s.\n",
1641 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1642 status));
1643 return -1;
1644 } else {
1645 d_printf("Added alias '%s'.\n", argv[0]);
1648 return 0;
1651 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1653 if (c->opt_localgroup)
1654 return rpc_alias_add_internals(c, argc, argv);
1656 return rpc_group_add_internals(c, argc, argv);
1659 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1660 TALLOC_CTX *mem_ctx,
1661 const char *name,
1662 DOM_SID *sid,
1663 enum lsa_SidType *type)
1665 DOM_SID *sids = NULL;
1666 enum lsa_SidType *types = NULL;
1667 struct rpc_pipe_client *pipe_hnd = NULL;
1668 struct policy_handle lsa_pol;
1669 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1671 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1672 &pipe_hnd);
1673 if (!NT_STATUS_IS_OK(result)) {
1674 goto done;
1677 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1678 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1680 if (!NT_STATUS_IS_OK(result)) {
1681 goto done;
1684 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1685 &name, NULL, 1, &sids, &types);
1687 if (NT_STATUS_IS_OK(result)) {
1688 sid_copy(sid, &sids[0]);
1689 *type = types[0];
1692 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1694 done:
1695 if (pipe_hnd) {
1696 TALLOC_FREE(pipe_hnd);
1699 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1701 /* Try as S-1-5-whatever */
1703 DOM_SID tmp_sid;
1705 if (string_to_sid(&tmp_sid, name)) {
1706 sid_copy(sid, &tmp_sid);
1707 *type = SID_NAME_UNKNOWN;
1708 result = NT_STATUS_OK;
1712 return result;
1715 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1716 TALLOC_CTX *mem_ctx,
1717 const DOM_SID *group_sid,
1718 const char *member)
1720 struct policy_handle connect_pol, domain_pol;
1721 NTSTATUS result;
1722 uint32 group_rid;
1723 struct policy_handle group_pol;
1725 struct samr_Ids rids, rid_types;
1726 struct lsa_String lsa_acct_name;
1728 DOM_SID sid;
1730 sid_copy(&sid, group_sid);
1732 if (!sid_split_rid(&sid, &group_rid)) {
1733 return NT_STATUS_UNSUCCESSFUL;
1736 /* Get sam policy handle */
1737 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1738 pipe_hnd->desthost,
1739 MAXIMUM_ALLOWED_ACCESS,
1740 &connect_pol);
1741 if (!NT_STATUS_IS_OK(result)) {
1742 return result;
1745 /* Get domain policy handle */
1746 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1747 &connect_pol,
1748 MAXIMUM_ALLOWED_ACCESS,
1749 &sid,
1750 &domain_pol);
1751 if (!NT_STATUS_IS_OK(result)) {
1752 return result;
1755 init_lsa_String(&lsa_acct_name, member);
1757 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1758 &domain_pol,
1760 &lsa_acct_name,
1761 &rids,
1762 &rid_types);
1764 if (!NT_STATUS_IS_OK(result)) {
1765 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1766 goto done;
1769 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1770 &domain_pol,
1771 MAXIMUM_ALLOWED_ACCESS,
1772 group_rid,
1773 &group_pol);
1775 if (!NT_STATUS_IS_OK(result)) {
1776 goto done;
1779 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1780 &group_pol,
1781 rids.ids[0],
1782 0x0005); /* unknown flags */
1784 done:
1785 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1786 return result;
1789 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1790 TALLOC_CTX *mem_ctx,
1791 const DOM_SID *alias_sid,
1792 const char *member)
1794 struct policy_handle connect_pol, domain_pol;
1795 NTSTATUS result;
1796 uint32 alias_rid;
1797 struct policy_handle alias_pol;
1799 DOM_SID member_sid;
1800 enum lsa_SidType member_type;
1802 DOM_SID sid;
1804 sid_copy(&sid, alias_sid);
1806 if (!sid_split_rid(&sid, &alias_rid)) {
1807 return NT_STATUS_UNSUCCESSFUL;
1810 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1811 member, &member_sid, &member_type);
1813 if (!NT_STATUS_IS_OK(result)) {
1814 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1815 return result;
1818 /* Get sam policy handle */
1819 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1820 pipe_hnd->desthost,
1821 MAXIMUM_ALLOWED_ACCESS,
1822 &connect_pol);
1823 if (!NT_STATUS_IS_OK(result)) {
1824 goto done;
1827 /* Get domain policy handle */
1828 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1829 &connect_pol,
1830 MAXIMUM_ALLOWED_ACCESS,
1831 &sid,
1832 &domain_pol);
1833 if (!NT_STATUS_IS_OK(result)) {
1834 goto done;
1837 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1838 &domain_pol,
1839 MAXIMUM_ALLOWED_ACCESS,
1840 alias_rid,
1841 &alias_pol);
1843 if (!NT_STATUS_IS_OK(result)) {
1844 return result;
1847 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1848 &alias_pol,
1849 &member_sid);
1851 if (!NT_STATUS_IS_OK(result)) {
1852 return result;
1855 done:
1856 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1857 return result;
1860 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1861 const DOM_SID *domain_sid,
1862 const char *domain_name,
1863 struct cli_state *cli,
1864 struct rpc_pipe_client *pipe_hnd,
1865 TALLOC_CTX *mem_ctx,
1866 int argc,
1867 const char **argv)
1869 DOM_SID group_sid;
1870 enum lsa_SidType group_type;
1872 if (argc != 2 || c->display_usage) {
1873 d_printf("Usage:\n"
1874 "net rpc group addmem <group> <member>\n"
1875 " Add a member to a group\n"
1876 " group\tGroup to add member to\n"
1877 " member\tMember to add to group\n");
1878 return NT_STATUS_UNSUCCESSFUL;
1881 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1882 &group_sid, &group_type))) {
1883 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
1884 return NT_STATUS_UNSUCCESSFUL;
1887 if (group_type == SID_NAME_DOM_GRP) {
1888 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1889 &group_sid, argv[1]);
1891 if (!NT_STATUS_IS_OK(result)) {
1892 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1893 argv[1], argv[0], nt_errstr(result));
1895 return result;
1898 if (group_type == SID_NAME_ALIAS) {
1899 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1900 &group_sid, argv[1]);
1902 if (!NT_STATUS_IS_OK(result)) {
1903 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1904 argv[1], argv[0], nt_errstr(result));
1906 return result;
1909 d_fprintf(stderr, "Can only add members to global or local groups "
1910 "which %s is not\n", argv[0]);
1912 return NT_STATUS_UNSUCCESSFUL;
1915 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1917 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1918 rpc_group_addmem_internals,
1919 argc, argv);
1922 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1923 struct rpc_pipe_client *pipe_hnd,
1924 TALLOC_CTX *mem_ctx,
1925 const DOM_SID *group_sid,
1926 const char *member)
1928 struct policy_handle connect_pol, domain_pol;
1929 NTSTATUS result;
1930 uint32 group_rid;
1931 struct policy_handle group_pol;
1933 struct samr_Ids rids, rid_types;
1934 struct lsa_String lsa_acct_name;
1936 DOM_SID sid;
1938 sid_copy(&sid, group_sid);
1940 if (!sid_split_rid(&sid, &group_rid))
1941 return NT_STATUS_UNSUCCESSFUL;
1943 /* Get sam policy handle */
1944 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1945 pipe_hnd->desthost,
1946 MAXIMUM_ALLOWED_ACCESS,
1947 &connect_pol);
1948 if (!NT_STATUS_IS_OK(result))
1949 return result;
1951 /* Get domain policy handle */
1952 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1953 &connect_pol,
1954 MAXIMUM_ALLOWED_ACCESS,
1955 &sid,
1956 &domain_pol);
1957 if (!NT_STATUS_IS_OK(result))
1958 return result;
1960 init_lsa_String(&lsa_acct_name, member);
1962 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1963 &domain_pol,
1965 &lsa_acct_name,
1966 &rids,
1967 &rid_types);
1968 if (!NT_STATUS_IS_OK(result)) {
1969 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1970 goto done;
1973 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1974 &domain_pol,
1975 MAXIMUM_ALLOWED_ACCESS,
1976 group_rid,
1977 &group_pol);
1979 if (!NT_STATUS_IS_OK(result))
1980 goto done;
1982 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1983 &group_pol,
1984 rids.ids[0]);
1986 done:
1987 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1988 return result;
1991 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
1992 TALLOC_CTX *mem_ctx,
1993 const DOM_SID *alias_sid,
1994 const char *member)
1996 struct policy_handle connect_pol, domain_pol;
1997 NTSTATUS result;
1998 uint32 alias_rid;
1999 struct policy_handle alias_pol;
2001 DOM_SID member_sid;
2002 enum lsa_SidType member_type;
2004 DOM_SID sid;
2006 sid_copy(&sid, alias_sid);
2008 if (!sid_split_rid(&sid, &alias_rid))
2009 return NT_STATUS_UNSUCCESSFUL;
2011 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2012 member, &member_sid, &member_type);
2014 if (!NT_STATUS_IS_OK(result)) {
2015 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2016 return result;
2019 /* Get sam policy handle */
2020 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2021 pipe_hnd->desthost,
2022 MAXIMUM_ALLOWED_ACCESS,
2023 &connect_pol);
2024 if (!NT_STATUS_IS_OK(result)) {
2025 goto done;
2028 /* Get domain policy handle */
2029 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2030 &connect_pol,
2031 MAXIMUM_ALLOWED_ACCESS,
2032 &sid,
2033 &domain_pol);
2034 if (!NT_STATUS_IS_OK(result)) {
2035 goto done;
2038 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2039 &domain_pol,
2040 MAXIMUM_ALLOWED_ACCESS,
2041 alias_rid,
2042 &alias_pol);
2044 if (!NT_STATUS_IS_OK(result))
2045 return result;
2047 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2048 &alias_pol,
2049 &member_sid);
2051 if (!NT_STATUS_IS_OK(result))
2052 return result;
2054 done:
2055 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2056 return result;
2059 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2060 const DOM_SID *domain_sid,
2061 const char *domain_name,
2062 struct cli_state *cli,
2063 struct rpc_pipe_client *pipe_hnd,
2064 TALLOC_CTX *mem_ctx,
2065 int argc,
2066 const char **argv)
2068 DOM_SID group_sid;
2069 enum lsa_SidType group_type;
2071 if (argc != 2 || c->display_usage) {
2072 d_printf("Usage:\n"
2073 "net rpc group delmem <group> <member>\n"
2074 " Delete a member from a group\n"
2075 " group\tGroup to delete member from\n"
2076 " member\tMember to delete from group\n");
2077 return NT_STATUS_UNSUCCESSFUL;
2080 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2081 &group_sid, &group_type))) {
2082 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2083 return NT_STATUS_UNSUCCESSFUL;
2086 if (group_type == SID_NAME_DOM_GRP) {
2087 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2088 &group_sid, argv[1]);
2090 if (!NT_STATUS_IS_OK(result)) {
2091 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2092 argv[1], argv[0], nt_errstr(result));
2094 return result;
2097 if (group_type == SID_NAME_ALIAS) {
2098 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2099 &group_sid, argv[1]);
2101 if (!NT_STATUS_IS_OK(result)) {
2102 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2103 argv[1], argv[0], nt_errstr(result));
2105 return result;
2108 d_fprintf(stderr, "Can only delete members from global or local groups "
2109 "which %s is not\n", argv[0]);
2111 return NT_STATUS_UNSUCCESSFUL;
2114 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2116 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2117 rpc_group_delmem_internals,
2118 argc, argv);
2122 * List groups on a remote RPC server.
2124 * All parameters are provided by the run_rpc_command function, except for
2125 * argc, argv which are passes through.
2127 * @param domain_sid The domain sid acquired from the remote server.
2128 * @param cli A cli_state connected to the server.
2129 * @param mem_ctx Talloc context, destroyed on completion of the function.
2130 * @param argc Standard main() style argc.
2131 * @param argv Standard main() style argv. Initial components are already
2132 * stripped.
2134 * @return Normal NTSTATUS return.
2137 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2138 const DOM_SID *domain_sid,
2139 const char *domain_name,
2140 struct cli_state *cli,
2141 struct rpc_pipe_client *pipe_hnd,
2142 TALLOC_CTX *mem_ctx,
2143 int argc,
2144 const char **argv)
2146 struct policy_handle connect_pol, domain_pol;
2147 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2148 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2149 struct samr_SamArray *groups = NULL;
2150 bool global = false;
2151 bool local = false;
2152 bool builtin = false;
2154 if (c->display_usage) {
2155 d_printf("Usage:\n"
2156 "net rpc group list [global] [local] [builtin]\n"
2157 " List groups on RPC server\n"
2158 " global\tList global groups\n"
2159 " local\tList local groups\n"
2160 " builtin\tList builtin groups\n"
2161 " If none of global, local or builtin is "
2162 "specified, all three options are considered set\n");
2163 return NT_STATUS_OK;
2166 if (argc == 0) {
2167 global = true;
2168 local = true;
2169 builtin = true;
2172 for (i=0; i<argc; i++) {
2173 if (strequal(argv[i], "global"))
2174 global = true;
2176 if (strequal(argv[i], "local"))
2177 local = true;
2179 if (strequal(argv[i], "builtin"))
2180 builtin = true;
2183 /* Get sam policy handle */
2185 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2186 pipe_hnd->desthost,
2187 MAXIMUM_ALLOWED_ACCESS,
2188 &connect_pol);
2189 if (!NT_STATUS_IS_OK(result)) {
2190 goto done;
2193 /* Get domain policy handle */
2195 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2196 &connect_pol,
2197 MAXIMUM_ALLOWED_ACCESS,
2198 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2199 &domain_pol);
2200 if (!NT_STATUS_IS_OK(result)) {
2201 goto done;
2204 /* Query domain groups */
2205 if (c->opt_long_list_entries)
2206 d_printf("\nGroup name Comment"
2207 "\n-----------------------------\n");
2208 do {
2209 uint32_t max_size, total_size, returned_size;
2210 union samr_DispInfo info;
2212 if (!global) break;
2214 get_query_dispinfo_params(
2215 loop_count, &max_entries, &max_size);
2217 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2218 &domain_pol,
2220 start_idx,
2221 max_entries,
2222 max_size,
2223 &total_size,
2224 &returned_size,
2225 &info);
2226 num_entries = info.info3.count;
2227 start_idx += info.info3.count;
2229 if (!NT_STATUS_IS_OK(result) &&
2230 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2231 break;
2233 for (i = 0; i < num_entries; i++) {
2235 const char *group = NULL;
2236 const char *desc = NULL;
2238 group = info.info3.entries[i].account_name.string;
2239 desc = info.info3.entries[i].description.string;
2241 if (c->opt_long_list_entries)
2242 printf("%-21.21s %-50.50s\n",
2243 group, desc);
2244 else
2245 printf("%s\n", group);
2247 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2248 /* query domain aliases */
2249 start_idx = 0;
2250 do {
2251 if (!local) break;
2253 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2254 &domain_pol,
2255 &start_idx,
2256 &groups,
2257 0xffff,
2258 &num_entries);
2259 if (!NT_STATUS_IS_OK(result) &&
2260 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2261 break;
2263 for (i = 0; i < num_entries; i++) {
2265 const char *description = NULL;
2267 if (c->opt_long_list_entries) {
2269 struct policy_handle alias_pol;
2270 union samr_AliasInfo *info = NULL;
2272 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2273 &domain_pol,
2274 0x8,
2275 groups->entries[i].idx,
2276 &alias_pol))) &&
2277 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2278 &alias_pol,
2280 &info))) &&
2281 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2282 &alias_pol)))) {
2283 description = info->description.string;
2287 if (description != NULL) {
2288 printf("%-21.21s %-50.50s\n",
2289 groups->entries[i].name.string,
2290 description);
2291 } else {
2292 printf("%s\n", groups->entries[i].name.string);
2295 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2296 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2297 /* Get builtin policy handle */
2299 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2300 &connect_pol,
2301 MAXIMUM_ALLOWED_ACCESS,
2302 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2303 &domain_pol);
2304 if (!NT_STATUS_IS_OK(result)) {
2305 goto done;
2307 /* query builtin aliases */
2308 start_idx = 0;
2309 do {
2310 if (!builtin) break;
2312 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2313 &domain_pol,
2314 &start_idx,
2315 &groups,
2316 max_entries,
2317 &num_entries);
2318 if (!NT_STATUS_IS_OK(result) &&
2319 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2320 break;
2322 for (i = 0; i < num_entries; i++) {
2324 const char *description = NULL;
2326 if (c->opt_long_list_entries) {
2328 struct policy_handle alias_pol;
2329 union samr_AliasInfo *info = NULL;
2331 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2332 &domain_pol,
2333 0x8,
2334 groups->entries[i].idx,
2335 &alias_pol))) &&
2336 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2337 &alias_pol,
2339 &info))) &&
2340 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2341 &alias_pol)))) {
2342 description = info->description.string;
2346 if (description != NULL) {
2347 printf("%-21.21s %-50.50s\n",
2348 groups->entries[i].name.string,
2349 description);
2350 } else {
2351 printf("%s\n", groups->entries[i].name.string);
2354 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2356 done:
2357 return result;
2360 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2362 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2363 rpc_group_list_internals,
2364 argc, argv);
2367 static NTSTATUS rpc_list_group_members(struct net_context *c,
2368 struct rpc_pipe_client *pipe_hnd,
2369 TALLOC_CTX *mem_ctx,
2370 const char *domain_name,
2371 const DOM_SID *domain_sid,
2372 struct policy_handle *domain_pol,
2373 uint32 rid)
2375 NTSTATUS result;
2376 struct policy_handle group_pol;
2377 uint32 num_members, *group_rids;
2378 int i;
2379 struct samr_RidTypeArray *rids = NULL;
2380 struct lsa_Strings names;
2381 struct samr_Ids types;
2383 fstring sid_str;
2384 sid_to_fstring(sid_str, domain_sid);
2386 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2387 domain_pol,
2388 MAXIMUM_ALLOWED_ACCESS,
2389 rid,
2390 &group_pol);
2392 if (!NT_STATUS_IS_OK(result))
2393 return result;
2395 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2396 &group_pol,
2397 &rids);
2399 if (!NT_STATUS_IS_OK(result))
2400 return result;
2402 num_members = rids->count;
2403 group_rids = rids->rids;
2405 while (num_members > 0) {
2406 int this_time = 512;
2408 if (num_members < this_time)
2409 this_time = num_members;
2411 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2412 domain_pol,
2413 this_time,
2414 group_rids,
2415 &names,
2416 &types);
2418 if (!NT_STATUS_IS_OK(result))
2419 return result;
2421 /* We only have users as members, but make the output
2422 the same as the output of alias members */
2424 for (i = 0; i < this_time; i++) {
2426 if (c->opt_long_list_entries) {
2427 printf("%s-%d %s\\%s %d\n", sid_str,
2428 group_rids[i], domain_name,
2429 names.names[i].string,
2430 SID_NAME_USER);
2431 } else {
2432 printf("%s\\%s\n", domain_name,
2433 names.names[i].string);
2437 num_members -= this_time;
2438 group_rids += 512;
2441 return NT_STATUS_OK;
2444 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2445 struct rpc_pipe_client *pipe_hnd,
2446 TALLOC_CTX *mem_ctx,
2447 struct policy_handle *domain_pol,
2448 uint32 rid)
2450 NTSTATUS result;
2451 struct rpc_pipe_client *lsa_pipe;
2452 struct policy_handle alias_pol, lsa_pol;
2453 uint32 num_members;
2454 DOM_SID *alias_sids;
2455 char **domains;
2456 char **names;
2457 enum lsa_SidType *types;
2458 int i;
2459 struct lsa_SidArray sid_array;
2461 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2462 domain_pol,
2463 MAXIMUM_ALLOWED_ACCESS,
2464 rid,
2465 &alias_pol);
2467 if (!NT_STATUS_IS_OK(result))
2468 return result;
2470 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2471 &alias_pol,
2472 &sid_array);
2474 if (!NT_STATUS_IS_OK(result)) {
2475 d_fprintf(stderr, "Couldn't list alias members\n");
2476 return result;
2479 num_members = sid_array.num_sids;
2481 if (num_members == 0) {
2482 return NT_STATUS_OK;
2485 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2486 &ndr_table_lsarpc.syntax_id,
2487 &lsa_pipe);
2488 if (!NT_STATUS_IS_OK(result)) {
2489 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2490 nt_errstr(result) );
2491 return result;
2494 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2495 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2497 if (!NT_STATUS_IS_OK(result)) {
2498 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2499 TALLOC_FREE(lsa_pipe);
2500 return result;
2503 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2504 if (!alias_sids) {
2505 d_fprintf(stderr, "Out of memory\n");
2506 TALLOC_FREE(lsa_pipe);
2507 return NT_STATUS_NO_MEMORY;
2510 for (i=0; i<num_members; i++) {
2511 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2514 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2515 num_members, alias_sids,
2516 &domains, &names, &types);
2518 if (!NT_STATUS_IS_OK(result) &&
2519 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2520 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2521 TALLOC_FREE(lsa_pipe);
2522 return result;
2525 for (i = 0; i < num_members; i++) {
2526 fstring sid_str;
2527 sid_to_fstring(sid_str, &alias_sids[i]);
2529 if (c->opt_long_list_entries) {
2530 printf("%s %s\\%s %d\n", sid_str,
2531 domains[i] ? domains[i] : "*unknown*",
2532 names[i] ? names[i] : "*unknown*", types[i]);
2533 } else {
2534 if (domains[i])
2535 printf("%s\\%s\n", domains[i], names[i]);
2536 else
2537 printf("%s\n", sid_str);
2541 TALLOC_FREE(lsa_pipe);
2542 return NT_STATUS_OK;
2545 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2546 const DOM_SID *domain_sid,
2547 const char *domain_name,
2548 struct cli_state *cli,
2549 struct rpc_pipe_client *pipe_hnd,
2550 TALLOC_CTX *mem_ctx,
2551 int argc,
2552 const char **argv)
2554 NTSTATUS result;
2555 struct policy_handle connect_pol, domain_pol;
2556 struct samr_Ids rids, rid_types;
2557 struct lsa_String lsa_acct_name;
2559 /* Get sam policy handle */
2561 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2562 pipe_hnd->desthost,
2563 MAXIMUM_ALLOWED_ACCESS,
2564 &connect_pol);
2566 if (!NT_STATUS_IS_OK(result))
2567 return result;
2569 /* Get domain policy handle */
2571 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2572 &connect_pol,
2573 MAXIMUM_ALLOWED_ACCESS,
2574 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2575 &domain_pol);
2577 if (!NT_STATUS_IS_OK(result))
2578 return result;
2580 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2582 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2583 &domain_pol,
2585 &lsa_acct_name,
2586 &rids,
2587 &rid_types);
2589 if (!NT_STATUS_IS_OK(result)) {
2591 /* Ok, did not find it in the global sam, try with builtin */
2593 DOM_SID sid_Builtin;
2595 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2597 sid_copy(&sid_Builtin, &global_sid_Builtin);
2599 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2600 &connect_pol,
2601 MAXIMUM_ALLOWED_ACCESS,
2602 &sid_Builtin,
2603 &domain_pol);
2605 if (!NT_STATUS_IS_OK(result)) {
2606 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2607 return result;
2610 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2611 &domain_pol,
2613 &lsa_acct_name,
2614 &rids,
2615 &rid_types);
2617 if (!NT_STATUS_IS_OK(result)) {
2618 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2619 return result;
2623 if (rids.count != 1) {
2624 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2625 return result;
2628 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2629 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2630 domain_sid, &domain_pol,
2631 rids.ids[0]);
2634 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2635 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2636 rids.ids[0]);
2639 return NT_STATUS_NO_SUCH_GROUP;
2642 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2644 if (argc != 1 || c->display_usage) {
2645 return rpc_group_usage(c, argc, argv);
2648 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2649 rpc_group_members_internals,
2650 argc, argv);
2653 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2655 NET_API_STATUS status;
2656 struct GROUP_INFO_0 g0;
2657 uint32_t parm_err;
2659 if (argc != 2) {
2660 d_printf("Usage: 'net rpc group rename group newname'\n");
2661 return -1;
2664 g0.grpi0_name = argv[1];
2666 status = NetGroupSetInfo(c->opt_host,
2667 argv[0],
2669 (uint8_t *)&g0,
2670 &parm_err);
2672 if (status != 0) {
2673 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2674 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2675 status));
2676 return -1;
2679 return 0;
2682 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2684 if (argc != 2 || c->display_usage) {
2685 return rpc_group_usage(c, argc, argv);
2688 return rpc_group_rename_internals(c, argc, argv);
2692 * 'net rpc group' entrypoint.
2693 * @param argc Standard main() style argc.
2694 * @param argv Standard main() style argv. Initial components are already
2695 * stripped.
2698 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2700 NET_API_STATUS status;
2702 struct functable func[] = {
2704 "add",
2705 rpc_group_add,
2706 NET_TRANSPORT_RPC,
2707 "Create specified group",
2708 "net rpc group add\n"
2709 " Create specified group"
2712 "delete",
2713 rpc_group_delete,
2714 NET_TRANSPORT_RPC,
2715 "Delete specified group",
2716 "net rpc group delete\n"
2717 " Delete specified group"
2720 "addmem",
2721 rpc_group_addmem,
2722 NET_TRANSPORT_RPC,
2723 "Add member to group",
2724 "net rpc group addmem\n"
2725 " Add member to group"
2728 "delmem",
2729 rpc_group_delmem,
2730 NET_TRANSPORT_RPC,
2731 "Remove member from group",
2732 "net rpc group delmem\n"
2733 " Remove member from group"
2736 "list",
2737 rpc_group_list,
2738 NET_TRANSPORT_RPC,
2739 "List groups",
2740 "net rpc group list\n"
2741 " List groups"
2744 "members",
2745 rpc_group_members,
2746 NET_TRANSPORT_RPC,
2747 "List group members",
2748 "net rpc group members\n"
2749 " List group members"
2752 "rename",
2753 rpc_group_rename,
2754 NET_TRANSPORT_RPC,
2755 "Rename group",
2756 "net rpc group rename\n"
2757 " Rename group"
2759 {NULL, NULL, 0, NULL, NULL}
2762 status = libnetapi_init(&c->netapi_ctx);
2763 if (status != 0) {
2764 return -1;
2766 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2767 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2768 if (c->opt_kerberos) {
2769 libnetapi_set_use_kerberos(c->netapi_ctx);
2772 if (argc == 0) {
2773 if (c->display_usage) {
2774 d_printf("Usage:\n");
2775 d_printf("net rpc group\n"
2776 " Alias for net rpc group list global local "
2777 "builtin\n");
2778 net_display_usage_from_functable(func);
2779 return 0;
2782 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2783 rpc_group_list_internals,
2784 argc, argv);
2787 return net_run_function(c, argc, argv, "net rpc group", func);
2790 /****************************************************************************/
2792 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2794 return net_share_usage(c, argc, argv);
2798 * Add a share on a remote RPC server.
2800 * @param argc Standard main() style argc.
2801 * @param argv Standard main() style argv. Initial components are already
2802 * stripped.
2804 * @return A shell status integer (0 for success).
2807 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2809 NET_API_STATUS status;
2810 char *sharename;
2811 char *path;
2812 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2813 uint32 num_users=0, perms=0;
2814 char *password=NULL; /* don't allow a share password */
2815 struct SHARE_INFO_2 i2;
2816 uint32_t parm_error = 0;
2818 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2819 return rpc_share_usage(c, argc, argv);
2822 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2823 return -1;
2826 path = strchr(sharename, '=');
2827 if (!path) {
2828 return -1;
2831 *path++ = '\0';
2833 i2.shi2_netname = sharename;
2834 i2.shi2_type = type;
2835 i2.shi2_remark = c->opt_comment;
2836 i2.shi2_permissions = perms;
2837 i2.shi2_max_uses = c->opt_maxusers;
2838 i2.shi2_current_uses = num_users;
2839 i2.shi2_path = path;
2840 i2.shi2_passwd = password;
2842 status = NetShareAdd(c->opt_host,
2844 (uint8_t *)&i2,
2845 &parm_error);
2846 if (status != 0) {
2847 printf("NetShareAdd failed with: %s\n",
2848 libnetapi_get_error_string(c->netapi_ctx, status));
2851 return status;
2855 * Delete a share on a remote RPC server.
2857 * @param domain_sid The domain sid acquired from the remote server.
2858 * @param argc Standard main() style argc.
2859 * @param argv Standard main() style argv. Initial components are already
2860 * stripped.
2862 * @return A shell status integer (0 for success).
2864 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2866 if (argc < 1 || c->display_usage) {
2867 return rpc_share_usage(c, argc, argv);
2870 return NetShareDel(c->opt_host, argv[0], 0);
2874 * Formatted print of share info
2876 * @param r pointer to SHARE_INFO_1 to format
2879 static void display_share_info_1(struct net_context *c,
2880 struct SHARE_INFO_1 *r)
2882 if (c->opt_long_list_entries) {
2883 d_printf("%-12s %-8.8s %-50s\n",
2884 r->shi1_netname,
2885 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2886 r->shi1_remark);
2887 } else {
2888 d_printf("%s\n", r->shi1_netname);
2892 static WERROR get_share_info(struct net_context *c,
2893 struct rpc_pipe_client *pipe_hnd,
2894 TALLOC_CTX *mem_ctx,
2895 uint32 level,
2896 int argc,
2897 const char **argv,
2898 struct srvsvc_NetShareInfoCtr *info_ctr)
2900 WERROR result;
2901 NTSTATUS status;
2902 union srvsvc_NetShareInfo info;
2904 /* no specific share requested, enumerate all */
2905 if (argc == 0) {
2907 uint32_t preferred_len = 0xffffffff;
2908 uint32_t total_entries = 0;
2909 uint32_t resume_handle = 0;
2911 info_ctr->level = level;
2913 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2914 pipe_hnd->desthost,
2915 info_ctr,
2916 preferred_len,
2917 &total_entries,
2918 &resume_handle,
2919 &result);
2920 return result;
2923 /* request just one share */
2924 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2925 pipe_hnd->desthost,
2926 argv[0],
2927 level,
2928 &info,
2929 &result);
2931 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2932 goto done;
2935 /* construct ctr */
2936 ZERO_STRUCTP(info_ctr);
2938 info_ctr->level = level;
2940 switch (level) {
2941 case 1:
2943 struct srvsvc_NetShareCtr1 *ctr1;
2945 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2946 W_ERROR_HAVE_NO_MEMORY(ctr1);
2948 ctr1->count = 1;
2949 ctr1->array = info.info1;
2951 info_ctr->ctr.ctr1 = ctr1;
2953 case 2:
2955 struct srvsvc_NetShareCtr2 *ctr2;
2957 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2958 W_ERROR_HAVE_NO_MEMORY(ctr2);
2960 ctr2->count = 1;
2961 ctr2->array = info.info2;
2963 info_ctr->ctr.ctr2 = ctr2;
2965 case 502:
2967 struct srvsvc_NetShareCtr502 *ctr502;
2969 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
2970 W_ERROR_HAVE_NO_MEMORY(ctr502);
2972 ctr502->count = 1;
2973 ctr502->array = info.info502;
2975 info_ctr->ctr.ctr502 = ctr502;
2977 } /* switch */
2978 done:
2979 return result;
2982 /***
2983 * 'net rpc share list' entrypoint.
2984 * @param argc Standard main() style argc.
2985 * @param argv Standard main() style argv. Initial components are already
2986 * stripped.
2988 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
2990 NET_API_STATUS status;
2991 struct SHARE_INFO_1 *i1 = NULL;
2992 uint32_t entries_read = 0;
2993 uint32_t total_entries = 0;
2994 uint32_t resume_handle = 0;
2995 uint32_t i, level = 1;
2997 if (c->display_usage) {
2998 d_printf("Usage\n"
2999 "net rpc share list\n"
3000 " List shares on remote server\n");
3001 return 0;
3004 status = NetShareEnum(c->opt_host,
3005 level,
3006 (uint8_t **)(void *)&i1,
3007 (uint32_t)-1,
3008 &entries_read,
3009 &total_entries,
3010 &resume_handle);
3011 if (status != 0) {
3012 goto done;
3015 /* Display results */
3017 if (c->opt_long_list_entries) {
3018 d_printf(
3019 "\nEnumerating shared resources (exports) on remote server:\n\n"
3020 "\nShare name Type Description\n"
3021 "---------- ---- -----------\n");
3023 for (i = 0; i < entries_read; i++)
3024 display_share_info_1(c, &i1[i]);
3025 done:
3026 return status;
3029 static bool check_share_availability(struct cli_state *cli, const char *netname)
3031 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3032 d_printf("skipping [%s]: not a file share.\n", netname);
3033 return false;
3036 if (!cli_tdis(cli))
3037 return false;
3039 return true;
3042 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3043 const char *netname, uint32 type)
3045 /* only support disk shares */
3046 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3047 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3048 return false;
3051 /* skip builtin shares */
3052 /* FIXME: should print$ be added too ? */
3053 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3054 strequal(netname,"global"))
3055 return false;
3057 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3058 printf("excluding [%s]\n", netname);
3059 return false;
3062 return check_share_availability(cli, netname);
3066 * Migrate shares from a remote RPC server to the local RPC server.
3068 * All parameters are provided by the run_rpc_command function, except for
3069 * argc, argv which are passed through.
3071 * @param domain_sid The domain sid acquired from the remote server.
3072 * @param cli A cli_state connected to the server.
3073 * @param mem_ctx Talloc context, destroyed on completion of the function.
3074 * @param argc Standard main() style argc.
3075 * @param argv Standard main() style argv. Initial components are already
3076 * stripped.
3078 * @return Normal NTSTATUS return.
3081 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3082 const DOM_SID *domain_sid,
3083 const char *domain_name,
3084 struct cli_state *cli,
3085 struct rpc_pipe_client *pipe_hnd,
3086 TALLOC_CTX *mem_ctx,
3087 int argc,
3088 const char **argv)
3090 WERROR result;
3091 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3092 struct srvsvc_NetShareInfoCtr ctr_src;
3093 uint32 i;
3094 struct rpc_pipe_client *srvsvc_pipe = NULL;
3095 struct cli_state *cli_dst = NULL;
3096 uint32 level = 502; /* includes secdesc */
3097 uint32_t parm_error = 0;
3099 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3100 &ctr_src);
3101 if (!W_ERROR_IS_OK(result))
3102 goto done;
3104 /* connect destination PI_SRVSVC */
3105 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3106 &ndr_table_srvsvc.syntax_id);
3107 if (!NT_STATUS_IS_OK(nt_status))
3108 return nt_status;
3111 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3113 union srvsvc_NetShareInfo info;
3114 struct srvsvc_NetShareInfo502 info502 =
3115 ctr_src.ctr.ctr502->array[i];
3117 /* reset error-code */
3118 nt_status = NT_STATUS_UNSUCCESSFUL;
3120 if (!check_share_sanity(c, cli, info502.name, info502.type))
3121 continue;
3123 /* finally add the share on the dst server */
3125 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3126 info502.name, info502.path, info502.comment);
3128 info.info502 = &info502;
3130 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3131 srvsvc_pipe->desthost,
3132 502,
3133 &info,
3134 &parm_error,
3135 &result);
3137 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3138 printf(" [%s] does already exist\n",
3139 info502.name);
3140 continue;
3143 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3144 printf("cannot add share: %s\n", win_errstr(result));
3145 goto done;
3150 nt_status = NT_STATUS_OK;
3152 done:
3153 if (cli_dst) {
3154 cli_shutdown(cli_dst);
3157 return nt_status;
3162 * Migrate shares from a RPC server to another.
3164 * @param argc Standard main() style argc.
3165 * @param argv Standard main() style argv. Initial components are already
3166 * stripped.
3168 * @return A shell status integer (0 for success).
3170 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3171 const char **argv)
3173 if (c->display_usage) {
3174 d_printf("Usage:\n"
3175 "net rpc share migrate shares\n"
3176 " Migrate shares to local server\n");
3177 return 0;
3180 if (!c->opt_host) {
3181 printf("no server to migrate\n");
3182 return -1;
3185 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3186 rpc_share_migrate_shares_internals,
3187 argc, argv);
3191 * Copy a file/dir
3193 * @param f file_info
3194 * @param mask current search mask
3195 * @param state arg-pointer
3198 static void copy_fn(const char *mnt, file_info *f,
3199 const char *mask, void *state)
3201 static NTSTATUS nt_status;
3202 static struct copy_clistate *local_state;
3203 static fstring filename, new_mask;
3204 fstring dir;
3205 char *old_dir;
3206 struct net_context *c;
3208 local_state = (struct copy_clistate *)state;
3209 nt_status = NT_STATUS_UNSUCCESSFUL;
3211 c = local_state->c;
3213 if (strequal(f->name, ".") || strequal(f->name, ".."))
3214 return;
3216 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3218 /* DIRECTORY */
3219 if (f->mode & aDIR) {
3221 DEBUG(3,("got dir: %s\n", f->name));
3223 fstrcpy(dir, local_state->cwd);
3224 fstrcat(dir, "\\");
3225 fstrcat(dir, f->name);
3227 switch (net_mode_share)
3229 case NET_MODE_SHARE_MIGRATE:
3230 /* create that directory */
3231 nt_status = net_copy_file(c, local_state->mem_ctx,
3232 local_state->cli_share_src,
3233 local_state->cli_share_dst,
3234 dir, dir,
3235 c->opt_acls? true : false,
3236 c->opt_attrs? true : false,
3237 c->opt_timestamps? true:false,
3238 false);
3239 break;
3240 default:
3241 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3242 return;
3245 if (!NT_STATUS_IS_OK(nt_status))
3246 printf("could not handle dir %s: %s\n",
3247 dir, nt_errstr(nt_status));
3249 /* search below that directory */
3250 fstrcpy(new_mask, dir);
3251 fstrcat(new_mask, "\\*");
3253 old_dir = local_state->cwd;
3254 local_state->cwd = dir;
3255 if (!sync_files(local_state, new_mask))
3256 printf("could not handle files\n");
3257 local_state->cwd = old_dir;
3259 return;
3263 /* FILE */
3264 fstrcpy(filename, local_state->cwd);
3265 fstrcat(filename, "\\");
3266 fstrcat(filename, f->name);
3268 DEBUG(3,("got file: %s\n", filename));
3270 switch (net_mode_share)
3272 case NET_MODE_SHARE_MIGRATE:
3273 nt_status = net_copy_file(c, local_state->mem_ctx,
3274 local_state->cli_share_src,
3275 local_state->cli_share_dst,
3276 filename, filename,
3277 c->opt_acls? true : false,
3278 c->opt_attrs? true : false,
3279 c->opt_timestamps? true: false,
3280 true);
3281 break;
3282 default:
3283 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3284 return;
3287 if (!NT_STATUS_IS_OK(nt_status))
3288 printf("could not handle file %s: %s\n",
3289 filename, nt_errstr(nt_status));
3294 * sync files, can be called recursivly to list files
3295 * and then call copy_fn for each file
3297 * @param cp_clistate pointer to the copy_clistate we work with
3298 * @param mask the current search mask
3300 * @return Boolean result
3302 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3304 struct cli_state *targetcli;
3305 char *targetpath = NULL;
3307 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3309 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3310 mask, &targetcli, &targetpath ) ) {
3311 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3312 mask, cli_errstr(cp_clistate->cli_share_src));
3313 return false;
3316 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3317 d_fprintf(stderr, "listing %s failed with error: %s\n",
3318 mask, cli_errstr(targetcli));
3319 return false;
3322 return true;
3327 * Set the top level directory permissions before we do any further copies.
3328 * Should set up ACL inheritance.
3331 bool copy_top_level_perms(struct net_context *c,
3332 struct copy_clistate *cp_clistate,
3333 const char *sharename)
3335 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3337 switch (net_mode_share) {
3338 case NET_MODE_SHARE_MIGRATE:
3339 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3340 nt_status = net_copy_fileattr(c,
3341 cp_clistate->mem_ctx,
3342 cp_clistate->cli_share_src,
3343 cp_clistate->cli_share_dst,
3344 "\\", "\\",
3345 c->opt_acls? true : false,
3346 c->opt_attrs? true : false,
3347 c->opt_timestamps? true: false,
3348 false);
3349 break;
3350 default:
3351 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3352 break;
3355 if (!NT_STATUS_IS_OK(nt_status)) {
3356 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3357 sharename, nt_errstr(nt_status));
3358 return false;
3361 return true;
3365 * Sync all files inside a remote share to another share (over smb).
3367 * All parameters are provided by the run_rpc_command function, except for
3368 * argc, argv which are passed through.
3370 * @param domain_sid The domain sid acquired from the remote server.
3371 * @param cli A cli_state connected to the server.
3372 * @param mem_ctx Talloc context, destroyed on completion of the function.
3373 * @param argc Standard main() style argc.
3374 * @param argv Standard main() style argv. Initial components are already
3375 * stripped.
3377 * @return Normal NTSTATUS return.
3380 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3381 const DOM_SID *domain_sid,
3382 const char *domain_name,
3383 struct cli_state *cli,
3384 struct rpc_pipe_client *pipe_hnd,
3385 TALLOC_CTX *mem_ctx,
3386 int argc,
3387 const char **argv)
3389 WERROR result;
3390 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3391 struct srvsvc_NetShareInfoCtr ctr_src;
3392 uint32 i;
3393 uint32 level = 502;
3394 struct copy_clistate cp_clistate;
3395 bool got_src_share = false;
3396 bool got_dst_share = false;
3397 const char *mask = "\\*";
3398 char *dst = NULL;
3400 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3401 if (dst == NULL) {
3402 nt_status = NT_STATUS_NO_MEMORY;
3403 goto done;
3406 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3407 &ctr_src);
3409 if (!W_ERROR_IS_OK(result))
3410 goto done;
3412 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3414 struct srvsvc_NetShareInfo502 info502 =
3415 ctr_src.ctr.ctr502->array[i];
3417 if (!check_share_sanity(c, cli, info502.name, info502.type))
3418 continue;
3420 /* one might not want to mirror whole discs :) */
3421 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3422 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3423 continue;
3426 switch (net_mode_share)
3428 case NET_MODE_SHARE_MIGRATE:
3429 printf("syncing");
3430 break;
3431 default:
3432 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3433 break;
3435 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3436 info502.name,
3437 c->opt_acls ? "including" : "without",
3438 c->opt_attrs ? "including" : "without",
3439 c->opt_timestamps ? "(preserving timestamps)" : "");
3441 cp_clistate.mem_ctx = mem_ctx;
3442 cp_clistate.cli_share_src = NULL;
3443 cp_clistate.cli_share_dst = NULL;
3444 cp_clistate.cwd = NULL;
3445 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3446 cp_clistate.c = c;
3448 /* open share source */
3449 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3450 &cli->dest_ss, cli->desthost,
3451 info502.name, "A:");
3452 if (!NT_STATUS_IS_OK(nt_status))
3453 goto done;
3455 got_src_share = true;
3457 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3458 /* open share destination */
3459 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3460 NULL, dst, info502.name, "A:");
3461 if (!NT_STATUS_IS_OK(nt_status))
3462 goto done;
3464 got_dst_share = true;
3467 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3468 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3469 nt_status = NT_STATUS_UNSUCCESSFUL;
3470 goto done;
3473 if (!sync_files(&cp_clistate, mask)) {
3474 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3475 nt_status = NT_STATUS_UNSUCCESSFUL;
3476 goto done;
3480 nt_status = NT_STATUS_OK;
3482 done:
3484 if (got_src_share)
3485 cli_shutdown(cp_clistate.cli_share_src);
3487 if (got_dst_share)
3488 cli_shutdown(cp_clistate.cli_share_dst);
3490 SAFE_FREE(dst);
3491 return nt_status;
3495 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3497 if (c->display_usage) {
3498 d_printf("Usage:\n"
3499 "net share migrate files\n"
3500 " Migrate files to local server\n");
3501 return 0;
3504 if (!c->opt_host) {
3505 d_printf("no server to migrate\n");
3506 return -1;
3509 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3510 rpc_share_migrate_files_internals,
3511 argc, argv);
3515 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3517 * All parameters are provided by the run_rpc_command function, except for
3518 * argc, argv which are passed through.
3520 * @param domain_sid The domain sid acquired from the remote server.
3521 * @param cli A cli_state connected to the server.
3522 * @param mem_ctx Talloc context, destroyed on completion of the function.
3523 * @param argc Standard main() style argc.
3524 * @param argv Standard main() style argv. Initial components are already
3525 * stripped.
3527 * @return Normal NTSTATUS return.
3530 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3531 const DOM_SID *domain_sid,
3532 const char *domain_name,
3533 struct cli_state *cli,
3534 struct rpc_pipe_client *pipe_hnd,
3535 TALLOC_CTX *mem_ctx,
3536 int argc,
3537 const char **argv)
3539 WERROR result;
3540 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3541 struct srvsvc_NetShareInfoCtr ctr_src;
3542 union srvsvc_NetShareInfo info;
3543 uint32 i;
3544 struct rpc_pipe_client *srvsvc_pipe = NULL;
3545 struct cli_state *cli_dst = NULL;
3546 uint32 level = 502; /* includes secdesc */
3547 uint32_t parm_error = 0;
3549 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3550 &ctr_src);
3552 if (!W_ERROR_IS_OK(result))
3553 goto done;
3555 /* connect destination PI_SRVSVC */
3556 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3557 &ndr_table_srvsvc.syntax_id);
3558 if (!NT_STATUS_IS_OK(nt_status))
3559 return nt_status;
3562 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3564 struct srvsvc_NetShareInfo502 info502 =
3565 ctr_src.ctr.ctr502->array[i];
3567 /* reset error-code */
3568 nt_status = NT_STATUS_UNSUCCESSFUL;
3570 if (!check_share_sanity(c, cli, info502.name, info502.type))
3571 continue;
3573 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3574 info502.name, info502.path, info502.comment);
3576 if (c->opt_verbose)
3577 display_sec_desc(info502.sd_buf.sd);
3579 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3580 info.info502 = &info502;
3582 /* finally modify the share on the dst server */
3583 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3584 srvsvc_pipe->desthost,
3585 info502.name,
3586 level,
3587 &info,
3588 &parm_error,
3589 &result);
3590 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3591 printf("cannot set share-acl: %s\n", win_errstr(result));
3592 goto done;
3597 nt_status = NT_STATUS_OK;
3599 done:
3600 if (cli_dst) {
3601 cli_shutdown(cli_dst);
3604 return nt_status;
3609 * Migrate share-acls from a RPC server to another.
3611 * @param argc Standard main() style argc.
3612 * @param argv Standard main() style argv. Initial components are already
3613 * stripped.
3615 * @return A shell status integer (0 for success).
3617 static int rpc_share_migrate_security(struct net_context *c, int argc,
3618 const char **argv)
3620 if (c->display_usage) {
3621 d_printf("Usage:\n"
3622 "net rpc share migrate security\n"
3623 " Migrate share-acls to local server\n");
3624 return 0;
3627 if (!c->opt_host) {
3628 d_printf("no server to migrate\n");
3629 return -1;
3632 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3633 rpc_share_migrate_security_internals,
3634 argc, argv);
3638 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3639 * from one server to another.
3641 * @param argc Standard main() style argc.
3642 * @param argv Standard main() style argv. Initial components are already
3643 * stripped.
3645 * @return A shell status integer (0 for success).
3648 static int rpc_share_migrate_all(struct net_context *c, int argc,
3649 const char **argv)
3651 int ret;
3653 if (c->display_usage) {
3654 d_printf("Usage:\n"
3655 "net rpc share migrate all\n"
3656 " Migrates shares including all share settings\n");
3657 return 0;
3660 if (!c->opt_host) {
3661 d_printf("no server to migrate\n");
3662 return -1;
3665 /* order is important. we don't want to be locked out by the share-acl
3666 * before copying files - gd */
3668 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3669 rpc_share_migrate_shares_internals, argc, argv);
3670 if (ret)
3671 return ret;
3673 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3674 rpc_share_migrate_files_internals, argc, argv);
3675 if (ret)
3676 return ret;
3678 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3679 rpc_share_migrate_security_internals, argc,
3680 argv);
3685 * 'net rpc share migrate' entrypoint.
3686 * @param argc Standard main() style argc.
3687 * @param argv Standard main() style argv. Initial components are already
3688 * stripped.
3690 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3693 struct functable func[] = {
3695 "all",
3696 rpc_share_migrate_all,
3697 NET_TRANSPORT_RPC,
3698 "Migrate shares from remote to local server",
3699 "net rpc share migrate all\n"
3700 " Migrate shares from remote to local server"
3703 "files",
3704 rpc_share_migrate_files,
3705 NET_TRANSPORT_RPC,
3706 "Migrate files from remote to local server",
3707 "net rpc share migrate files\n"
3708 " Migrate files from remote to local server"
3711 "security",
3712 rpc_share_migrate_security,
3713 NET_TRANSPORT_RPC,
3714 "Migrate share-ACLs from remote to local server",
3715 "net rpc share migrate security\n"
3716 " Migrate share-ACLs from remote to local server"
3719 "shares",
3720 rpc_share_migrate_shares,
3721 NET_TRANSPORT_RPC,
3722 "Migrate shares from remote to local server",
3723 "net rpc share migrate shares\n"
3724 " Migrate shares from remote to local server"
3726 {NULL, NULL, 0, NULL, NULL}
3729 net_mode_share = NET_MODE_SHARE_MIGRATE;
3731 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3734 struct full_alias {
3735 DOM_SID sid;
3736 uint32 num_members;
3737 DOM_SID *members;
3740 static int num_server_aliases;
3741 static struct full_alias *server_aliases;
3744 * Add an alias to the static list.
3746 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3748 if (server_aliases == NULL)
3749 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3751 server_aliases[num_server_aliases] = *alias;
3752 num_server_aliases += 1;
3756 * For a specific domain on the server, fetch all the aliases
3757 * and their members. Add all of them to the server_aliases.
3760 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3761 TALLOC_CTX *mem_ctx,
3762 struct policy_handle *connect_pol,
3763 const DOM_SID *domain_sid)
3765 uint32 start_idx, max_entries, num_entries, i;
3766 struct samr_SamArray *groups = NULL;
3767 NTSTATUS result;
3768 struct policy_handle domain_pol;
3770 /* Get domain policy handle */
3772 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3773 connect_pol,
3774 MAXIMUM_ALLOWED_ACCESS,
3775 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3776 &domain_pol);
3777 if (!NT_STATUS_IS_OK(result))
3778 return result;
3780 start_idx = 0;
3781 max_entries = 250;
3783 do {
3784 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3785 &domain_pol,
3786 &start_idx,
3787 &groups,
3788 max_entries,
3789 &num_entries);
3790 for (i = 0; i < num_entries; i++) {
3792 struct policy_handle alias_pol;
3793 struct full_alias alias;
3794 struct lsa_SidArray sid_array;
3795 int j;
3797 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3798 &domain_pol,
3799 MAXIMUM_ALLOWED_ACCESS,
3800 groups->entries[i].idx,
3801 &alias_pol);
3802 if (!NT_STATUS_IS_OK(result))
3803 goto done;
3805 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3806 &alias_pol,
3807 &sid_array);
3808 if (!NT_STATUS_IS_OK(result))
3809 goto done;
3811 alias.num_members = sid_array.num_sids;
3813 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3814 if (!NT_STATUS_IS_OK(result))
3815 goto done;
3817 alias.members = NULL;
3819 if (alias.num_members > 0) {
3820 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3822 for (j = 0; j < alias.num_members; j++)
3823 sid_copy(&alias.members[j],
3824 sid_array.sids[j].sid);
3827 sid_copy(&alias.sid, domain_sid);
3828 sid_append_rid(&alias.sid, groups->entries[i].idx);
3830 push_alias(mem_ctx, &alias);
3832 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3834 result = NT_STATUS_OK;
3836 done:
3837 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3839 return result;
3843 * Dump server_aliases as names for debugging purposes.
3846 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3847 const DOM_SID *domain_sid,
3848 const char *domain_name,
3849 struct cli_state *cli,
3850 struct rpc_pipe_client *pipe_hnd,
3851 TALLOC_CTX *mem_ctx,
3852 int argc,
3853 const char **argv)
3855 int i;
3856 NTSTATUS result;
3857 struct policy_handle lsa_pol;
3859 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3860 SEC_FLAG_MAXIMUM_ALLOWED,
3861 &lsa_pol);
3862 if (!NT_STATUS_IS_OK(result))
3863 return result;
3865 for (i=0; i<num_server_aliases; i++) {
3866 char **names;
3867 char **domains;
3868 enum lsa_SidType *types;
3869 int j;
3871 struct full_alias *alias = &server_aliases[i];
3873 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3874 &alias->sid,
3875 &domains, &names, &types);
3876 if (!NT_STATUS_IS_OK(result))
3877 continue;
3879 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3881 if (alias->num_members == 0) {
3882 DEBUG(1, ("\n"));
3883 continue;
3886 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3887 alias->num_members,
3888 alias->members,
3889 &domains, &names, &types);
3891 if (!NT_STATUS_IS_OK(result) &&
3892 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3893 continue;
3895 for (j=0; j<alias->num_members; j++)
3896 DEBUG(1, ("%s\\%s (%d); ",
3897 domains[j] ? domains[j] : "*unknown*",
3898 names[j] ? names[j] : "*unknown*",types[j]));
3899 DEBUG(1, ("\n"));
3902 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3904 return NT_STATUS_OK;
3908 * Fetch a list of all server aliases and their members into
3909 * server_aliases.
3912 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3913 const DOM_SID *domain_sid,
3914 const char *domain_name,
3915 struct cli_state *cli,
3916 struct rpc_pipe_client *pipe_hnd,
3917 TALLOC_CTX *mem_ctx,
3918 int argc,
3919 const char **argv)
3921 NTSTATUS result;
3922 struct policy_handle connect_pol;
3924 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3925 pipe_hnd->desthost,
3926 MAXIMUM_ALLOWED_ACCESS,
3927 &connect_pol);
3929 if (!NT_STATUS_IS_OK(result))
3930 goto done;
3932 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3933 &global_sid_Builtin);
3935 if (!NT_STATUS_IS_OK(result))
3936 goto done;
3938 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3939 domain_sid);
3941 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3942 done:
3943 return result;
3946 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3948 token->num_sids = 4;
3950 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
3951 d_fprintf(stderr, "malloc failed\n");
3952 token->num_sids = 0;
3953 return;
3956 token->user_sids[0] = *user_sid;
3957 sid_copy(&token->user_sids[1], &global_sid_World);
3958 sid_copy(&token->user_sids[2], &global_sid_Network);
3959 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3962 static void free_user_token(NT_USER_TOKEN *token)
3964 SAFE_FREE(token->user_sids);
3967 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3969 if (is_sid_in_token(token, sid))
3970 return;
3972 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3973 if (!token->user_sids) {
3974 return;
3977 sid_copy(&token->user_sids[token->num_sids], sid);
3979 token->num_sids += 1;
3982 struct user_token {
3983 fstring name;
3984 NT_USER_TOKEN token;
3987 static void dump_user_token(struct user_token *token)
3989 int i;
3991 d_printf("%s\n", token->name);
3993 for (i=0; i<token->token.num_sids; i++) {
3994 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
3998 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4000 int i;
4002 for (i=0; i<alias->num_members; i++) {
4003 if (sid_compare(sid, &alias->members[i]) == 0)
4004 return true;
4007 return false;
4010 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4012 int i;
4014 for (i=0; i<num_server_aliases; i++) {
4015 if (is_alias_member(&sid, &server_aliases[i]))
4016 add_sid_to_token(token, &server_aliases[i].sid);
4021 * We got a user token with all the SIDs we can know about without asking the
4022 * server directly. These are the user and domain group sids. All of these can
4023 * be members of aliases. So scan the list of aliases for each of the SIDs and
4024 * add them to the token.
4027 static void collect_alias_memberships(NT_USER_TOKEN *token)
4029 int num_global_sids = token->num_sids;
4030 int i;
4032 for (i=0; i<num_global_sids; i++) {
4033 collect_sid_memberships(token, token->user_sids[i]);
4037 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4039 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4040 enum wbcSidType type;
4041 fstring full_name;
4042 struct wbcDomainSid wsid;
4043 char *sid_str = NULL;
4044 DOM_SID user_sid;
4045 uint32_t num_groups;
4046 gid_t *groups = NULL;
4047 uint32_t i;
4049 fstr_sprintf(full_name, "%s%c%s",
4050 domain, *lp_winbind_separator(), user);
4052 /* First let's find out the user sid */
4054 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4056 if (!WBC_ERROR_IS_OK(wbc_status)) {
4057 DEBUG(1, ("winbind could not find %s: %s\n",
4058 full_name, wbcErrorString(wbc_status)));
4059 return false;
4062 wbc_status = wbcSidToString(&wsid, &sid_str);
4063 if (!WBC_ERROR_IS_OK(wbc_status)) {
4064 return false;
4067 if (type != SID_NAME_USER) {
4068 wbcFreeMemory(sid_str);
4069 DEBUG(1, ("%s is not a user\n", full_name));
4070 return false;
4073 if (!string_to_sid(&user_sid, sid_str)) {
4074 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4075 return false;
4078 wbcFreeMemory(sid_str);
4079 sid_str = NULL;
4081 init_user_token(token, &user_sid);
4083 /* And now the groups winbind knows about */
4085 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4086 if (!WBC_ERROR_IS_OK(wbc_status)) {
4087 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4088 full_name, wbcErrorString(wbc_status)));
4089 return false;
4092 for (i = 0; i < num_groups; i++) {
4093 gid_t gid = groups[i];
4094 DOM_SID sid;
4096 wbc_status = wbcGidToSid(gid, &wsid);
4097 if (!WBC_ERROR_IS_OK(wbc_status)) {
4098 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4099 (unsigned int)gid, wbcErrorString(wbc_status)));
4100 wbcFreeMemory(groups);
4101 return false;
4104 wbc_status = wbcSidToString(&wsid, &sid_str);
4105 if (!WBC_ERROR_IS_OK(wbc_status)) {
4106 wbcFreeMemory(groups);
4107 return false;
4110 DEBUG(3, (" %s\n", sid_str));
4112 string_to_sid(&sid, sid_str);
4113 wbcFreeMemory(sid_str);
4114 sid_str = NULL;
4116 add_sid_to_token(token, &sid);
4118 wbcFreeMemory(groups);
4120 return true;
4124 * Get a list of all user tokens we want to look at
4127 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4128 struct user_token **user_tokens)
4130 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4131 uint32_t i, num_users;
4132 const char **users;
4133 struct user_token *result;
4134 TALLOC_CTX *frame = NULL;
4136 if (lp_winbind_use_default_domain() &&
4137 (c->opt_target_workgroup == NULL)) {
4138 d_fprintf(stderr, "winbind use default domain = yes set, "
4139 "please specify a workgroup\n");
4140 return false;
4143 /* Send request to winbind daemon */
4145 wbc_status = wbcListUsers(NULL, &num_users, &users);
4146 if (!WBC_ERROR_IS_OK(wbc_status)) {
4147 DEBUG(1, ("winbind could not list users: %s\n",
4148 wbcErrorString(wbc_status)));
4149 return false;
4152 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4154 if (result == NULL) {
4155 DEBUG(1, ("Could not malloc sid array\n"));
4156 wbcFreeMemory(users);
4157 return false;
4160 frame = talloc_stackframe();
4161 for (i=0; i < num_users; i++) {
4162 fstring domain, user;
4163 char *p;
4165 fstrcpy(result[i].name, users[i]);
4167 p = strchr(users[i], *lp_winbind_separator());
4169 DEBUG(3, ("%s\n", users[i]));
4171 if (p == NULL) {
4172 fstrcpy(domain, c->opt_target_workgroup);
4173 fstrcpy(user, users[i]);
4174 } else {
4175 *p++ = '\0';
4176 fstrcpy(domain, users[i]);
4177 strupper_m(domain);
4178 fstrcpy(user, p);
4181 get_user_sids(domain, user, &(result[i].token));
4182 i+=1;
4184 TALLOC_FREE(frame);
4185 wbcFreeMemory(users);
4187 *num_tokens = num_users;
4188 *user_tokens = result;
4190 return true;
4193 static bool get_user_tokens_from_file(FILE *f,
4194 int *num_tokens,
4195 struct user_token **tokens)
4197 struct user_token *token = NULL;
4199 while (!feof(f)) {
4200 fstring line;
4202 if (fgets(line, sizeof(line)-1, f) == NULL) {
4203 return true;
4206 if (line[strlen(line)-1] == '\n')
4207 line[strlen(line)-1] = '\0';
4209 if (line[0] == ' ') {
4210 /* We have a SID */
4212 DOM_SID sid;
4213 if(!string_to_sid(&sid, &line[1])) {
4214 DEBUG(1,("get_user_tokens_from_file: Could "
4215 "not convert sid %s \n",&line[1]));
4216 return false;
4219 if (token == NULL) {
4220 DEBUG(0, ("File does not begin with username"));
4221 return false;
4224 add_sid_to_token(&token->token, &sid);
4225 continue;
4228 /* And a new user... */
4230 *num_tokens += 1;
4231 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4232 if (*tokens == NULL) {
4233 DEBUG(0, ("Could not realloc tokens\n"));
4234 return false;
4237 token = &((*tokens)[*num_tokens-1]);
4239 fstrcpy(token->name, line);
4240 token->token.num_sids = 0;
4241 token->token.user_sids = NULL;
4242 continue;
4245 return false;
4250 * Show the list of all users that have access to a share
4253 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4254 TALLOC_CTX *mem_ctx,
4255 const char *netname,
4256 int num_tokens,
4257 struct user_token *tokens)
4259 int fnum;
4260 SEC_DESC *share_sd = NULL;
4261 SEC_DESC *root_sd = NULL;
4262 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4263 int i;
4264 union srvsvc_NetShareInfo info;
4265 WERROR result;
4266 NTSTATUS status;
4267 uint16 cnum;
4269 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4270 pipe_hnd->desthost,
4271 netname,
4272 502,
4273 &info,
4274 &result);
4276 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4277 DEBUG(1, ("Coult not query secdesc for share %s\n",
4278 netname));
4279 return;
4282 share_sd = info.info502->sd_buf.sd;
4283 if (share_sd == NULL) {
4284 DEBUG(1, ("Got no secdesc for share %s\n",
4285 netname));
4288 cnum = cli->cnum;
4290 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4291 return;
4294 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4296 if (fnum != -1) {
4297 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4300 for (i=0; i<num_tokens; i++) {
4301 uint32 acc_granted;
4303 if (share_sd != NULL) {
4304 status = se_access_check(share_sd, &tokens[i].token,
4305 1, &acc_granted);
4307 if (!NT_STATUS_IS_OK(status)) {
4308 DEBUG(1, ("Could not check share_sd for "
4309 "user %s\n",
4310 tokens[i].name));
4311 continue;
4315 if (root_sd == NULL) {
4316 d_printf(" %s\n", tokens[i].name);
4317 continue;
4320 status = se_access_check(root_sd, &tokens[i].token,
4321 1, &acc_granted);
4322 if (!NT_STATUS_IS_OK(status)) {
4323 DEBUG(1, ("Could not check root_sd for user %s\n",
4324 tokens[i].name));
4325 continue;
4327 d_printf(" %s\n", tokens[i].name);
4330 if (fnum != -1)
4331 cli_close(cli, fnum);
4332 cli_tdis(cli);
4333 cli->cnum = cnum;
4335 return;
4338 struct share_list {
4339 int num_shares;
4340 char **shares;
4343 static void collect_share(const char *name, uint32 m,
4344 const char *comment, void *state)
4346 struct share_list *share_list = (struct share_list *)state;
4348 if (m != STYPE_DISKTREE)
4349 return;
4351 share_list->num_shares += 1;
4352 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4353 if (!share_list->shares) {
4354 share_list->num_shares = 0;
4355 return;
4357 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4361 * List shares on a remote RPC server, including the security descriptors.
4363 * All parameters are provided by the run_rpc_command function, except for
4364 * argc, argv which are passed through.
4366 * @param domain_sid The domain sid acquired from the remote server.
4367 * @param cli A cli_state connected to the server.
4368 * @param mem_ctx Talloc context, destroyed on completion of the function.
4369 * @param argc Standard main() style argc.
4370 * @param argv Standard main() style argv. Initial components are already
4371 * stripped.
4373 * @return Normal NTSTATUS return.
4376 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4377 const DOM_SID *domain_sid,
4378 const char *domain_name,
4379 struct cli_state *cli,
4380 struct rpc_pipe_client *pipe_hnd,
4381 TALLOC_CTX *mem_ctx,
4382 int argc,
4383 const char **argv)
4385 int ret;
4386 bool r;
4387 uint32 i;
4388 FILE *f;
4390 struct user_token *tokens = NULL;
4391 int num_tokens = 0;
4393 struct share_list share_list;
4395 if (argc == 0) {
4396 f = stdin;
4397 } else {
4398 f = fopen(argv[0], "r");
4401 if (f == NULL) {
4402 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4403 return NT_STATUS_UNSUCCESSFUL;
4406 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4408 if (f != stdin)
4409 fclose(f);
4411 if (!r) {
4412 DEBUG(0, ("Could not read users from file\n"));
4413 return NT_STATUS_UNSUCCESSFUL;
4416 for (i=0; i<num_tokens; i++)
4417 collect_alias_memberships(&tokens[i].token);
4419 share_list.num_shares = 0;
4420 share_list.shares = NULL;
4422 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4424 if (ret == -1) {
4425 DEBUG(0, ("Error returning browse list: %s\n",
4426 cli_errstr(cli)));
4427 goto done;
4430 for (i = 0; i < share_list.num_shares; i++) {
4431 char *netname = share_list.shares[i];
4433 if (netname[strlen(netname)-1] == '$')
4434 continue;
4436 d_printf("%s\n", netname);
4438 show_userlist(pipe_hnd, mem_ctx, netname,
4439 num_tokens, tokens);
4441 done:
4442 for (i=0; i<num_tokens; i++) {
4443 free_user_token(&tokens[i].token);
4445 SAFE_FREE(tokens);
4446 SAFE_FREE(share_list.shares);
4448 return NT_STATUS_OK;
4451 static int rpc_share_allowedusers(struct net_context *c, int argc,
4452 const char **argv)
4454 int result;
4456 if (c->display_usage) {
4457 d_printf("Usage:\n"
4458 "net rpc share allowedusers\n"
4459 " List allowed users\n");
4460 return 0;
4463 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4464 rpc_aliaslist_internals,
4465 argc, argv);
4466 if (result != 0)
4467 return result;
4469 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4470 rpc_aliaslist_dump,
4471 argc, argv);
4472 if (result != 0)
4473 return result;
4475 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4476 rpc_share_allowedusers_internals,
4477 argc, argv);
4480 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4482 int num_tokens = 0;
4483 struct user_token *tokens = NULL;
4484 int i;
4486 if (argc != 0) {
4487 net_usersidlist_usage(c, argc, argv);
4488 return 0;
4491 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4492 DEBUG(0, ("Could not get the user/sid list\n"));
4493 return 0;
4496 for (i=0; i<num_tokens; i++) {
4497 dump_user_token(&tokens[i]);
4498 free_user_token(&tokens[i].token);
4501 SAFE_FREE(tokens);
4502 return 1;
4505 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4507 d_printf("net usersidlist\n"
4508 "\tprints out a list of all users the running winbind knows\n"
4509 "\tabout, together with all their SIDs. This is used as\n"
4510 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4512 net_common_flags_usage(c, argc, argv);
4513 return -1;
4517 * 'net rpc share' entrypoint.
4518 * @param argc Standard main() style argc.
4519 * @param argv Standard main() style argv. Initial components are already
4520 * stripped.
4523 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4525 NET_API_STATUS status;
4527 struct functable func[] = {
4529 "add",
4530 rpc_share_add,
4531 NET_TRANSPORT_RPC,
4532 "Add share",
4533 "net rpc share add\n"
4534 " Add share"
4537 "delete",
4538 rpc_share_delete,
4539 NET_TRANSPORT_RPC,
4540 "Remove share",
4541 "net rpc share delete\n"
4542 " Remove share"
4545 "allowedusers",
4546 rpc_share_allowedusers,
4547 NET_TRANSPORT_RPC,
4548 "Modify allowed users",
4549 "net rpc share allowedusers\n"
4550 " Modify allowed users"
4553 "migrate",
4554 rpc_share_migrate,
4555 NET_TRANSPORT_RPC,
4556 "Migrate share to local server",
4557 "net rpc share migrate\n"
4558 " Migrate share to local server"
4561 "list",
4562 rpc_share_list,
4563 NET_TRANSPORT_RPC,
4564 "List shares",
4565 "net rpc share list\n"
4566 " List shares"
4568 {NULL, NULL, 0, NULL, NULL}
4571 status = libnetapi_init(&c->netapi_ctx);
4572 if (status != 0) {
4573 return -1;
4575 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4576 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4577 if (c->opt_kerberos) {
4578 libnetapi_set_use_kerberos(c->netapi_ctx);
4581 if (argc == 0) {
4582 if (c->display_usage) {
4583 d_printf("Usage:\n"
4584 "net rpc share\n"
4585 " List shares\n"
4586 " Alias for net rpc share list\n");
4587 net_display_usage_from_functable(func);
4588 return 0;
4591 return rpc_share_list(c, argc, argv);
4594 return net_run_function(c, argc, argv, "net rpc share", func);
4597 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4598 TALLOC_CTX *mem_ctx,
4599 struct rpc_sh_ctx *ctx,
4600 struct rpc_pipe_client *pipe_hnd,
4601 int argc, const char **argv)
4604 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4607 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4608 TALLOC_CTX *mem_ctx,
4609 struct rpc_sh_ctx *ctx,
4610 struct rpc_pipe_client *pipe_hnd,
4611 int argc, const char **argv)
4613 NET_API_STATUS status;
4614 uint32_t parm_err = 0;
4615 struct SHARE_INFO_2 i2;
4617 if ((argc < 2) || (argc > 3)) {
4618 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4619 ctx->whoami);
4620 return NT_STATUS_INVALID_PARAMETER;
4623 i2.shi2_netname = argv[0];
4624 i2.shi2_type = STYPE_DISKTREE;
4625 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4626 i2.shi2_permissions = 0;
4627 i2.shi2_max_uses = 0;
4628 i2.shi2_current_uses = 0;
4629 i2.shi2_path = argv[1];
4630 i2.shi2_passwd = NULL;
4632 status = NetShareAdd(pipe_hnd->desthost,
4634 (uint8_t *)&i2,
4635 &parm_err);
4637 return werror_to_ntstatus(W_ERROR(status));
4640 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4641 TALLOC_CTX *mem_ctx,
4642 struct rpc_sh_ctx *ctx,
4643 struct rpc_pipe_client *pipe_hnd,
4644 int argc, const char **argv)
4646 if (argc != 1) {
4647 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4648 return NT_STATUS_INVALID_PARAMETER;
4651 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4654 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4655 TALLOC_CTX *mem_ctx,
4656 struct rpc_sh_ctx *ctx,
4657 struct rpc_pipe_client *pipe_hnd,
4658 int argc, const char **argv)
4660 union srvsvc_NetShareInfo info;
4661 WERROR result;
4662 NTSTATUS status;
4664 if (argc != 1) {
4665 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4666 return NT_STATUS_INVALID_PARAMETER;
4669 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4670 pipe_hnd->desthost,
4671 argv[0],
4673 &info,
4674 &result);
4675 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4676 goto done;
4679 d_printf("Name: %s\n", info.info2->name);
4680 d_printf("Comment: %s\n", info.info2->comment);
4681 d_printf("Path: %s\n", info.info2->path);
4682 d_printf("Password: %s\n", info.info2->password);
4684 done:
4685 return werror_to_ntstatus(result);
4688 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4689 struct rpc_sh_ctx *ctx)
4691 static struct rpc_sh_cmd cmds[] = {
4693 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4694 "List available shares" },
4696 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4697 "Add a share" },
4699 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4700 "Delete a share" },
4702 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4703 "Get information about a share" },
4705 { NULL, NULL, 0, NULL, NULL }
4708 return cmds;
4711 /****************************************************************************/
4713 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4715 return net_file_usage(c, argc, argv);
4719 * Close a file on a remote RPC server.
4721 * @param argc Standard main() style argc.
4722 * @param argv Standard main() style argv. Initial components are already
4723 * stripped.
4725 * @return A shell status integer (0 for success).
4727 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4729 if (argc < 1 || c->display_usage) {
4730 return rpc_file_usage(c, argc, argv);
4733 return NetFileClose(c->opt_host, atoi(argv[0]));
4737 * Formatted print of open file info
4739 * @param r struct FILE_INFO_3 contents
4742 static void display_file_info_3(struct FILE_INFO_3 *r)
4744 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4745 r->fi3_id, r->fi3_username, r->fi3_permissions,
4746 r->fi3_num_locks, r->fi3_pathname);
4750 * List files for a user on a remote RPC server.
4752 * @param argc Standard main() style argc.
4753 * @param argv Standard main() style argv. Initial components are already
4754 * stripped.
4756 * @return A shell status integer (0 for success)..
4759 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4761 NET_API_STATUS status;
4762 uint32 preferred_len = 0xffffffff, i;
4763 const char *username=NULL;
4764 uint32_t total_entries = 0;
4765 uint32_t entries_read = 0;
4766 uint32_t resume_handle = 0;
4767 struct FILE_INFO_3 *i3 = NULL;
4769 if (c->display_usage) {
4770 return rpc_file_usage(c, argc, argv);
4773 /* if argc > 0, must be user command */
4774 if (argc > 0) {
4775 username = smb_xstrdup(argv[0]);
4778 status = NetFileEnum(c->opt_host,
4779 NULL,
4780 username,
4782 (uint8_t **)(void *)&i3,
4783 preferred_len,
4784 &entries_read,
4785 &total_entries,
4786 &resume_handle);
4788 if (status != 0) {
4789 goto done;
4792 /* Display results */
4794 d_printf(
4795 "\nEnumerating open files on remote server:\n\n"
4796 "\nFileId Opened by Perms Locks Path"
4797 "\n------ --------- ----- ----- ---- \n");
4798 for (i = 0; i < entries_read; i++) {
4799 display_file_info_3(&i3[i]);
4801 done:
4802 return status;
4806 * 'net rpc file' entrypoint.
4807 * @param argc Standard main() style argc.
4808 * @param argv Standard main() style argv. Initial components are already
4809 * stripped.
4812 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4814 NET_API_STATUS status;
4816 struct functable func[] = {
4818 "close",
4819 rpc_file_close,
4820 NET_TRANSPORT_RPC,
4821 "Close opened file",
4822 "net rpc file close\n"
4823 " Close opened file"
4826 "user",
4827 rpc_file_user,
4828 NET_TRANSPORT_RPC,
4829 "List files opened by user",
4830 "net rpc file user\n"
4831 " List files opened by user"
4833 #if 0
4835 "info",
4836 rpc_file_info,
4837 NET_TRANSPORT_RPC,
4838 "Display information about opened file",
4839 "net rpc file info\n"
4840 " Display information about opened file"
4842 #endif
4843 {NULL, NULL, 0, NULL, NULL}
4846 status = libnetapi_init(&c->netapi_ctx);
4847 if (status != 0) {
4848 return -1;
4850 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4851 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4852 if (c->opt_kerberos) {
4853 libnetapi_set_use_kerberos(c->netapi_ctx);
4856 if (argc == 0) {
4857 if (c->display_usage) {
4858 d_printf("Usage:\n");
4859 d_printf("net rpc file\n"
4860 " List opened files\n");
4861 net_display_usage_from_functable(func);
4862 return 0;
4865 return rpc_file_user(c, argc, argv);
4868 return net_run_function(c, argc, argv, "net rpc file", func);
4872 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4874 * All parameters are provided by the run_rpc_command function, except for
4875 * argc, argv which are passed through.
4877 * @param c A net_context structure.
4878 * @param domain_sid The domain sid acquired from the remote server.
4879 * @param cli A cli_state connected to the server.
4880 * @param mem_ctx Talloc context, destroyed on completion of the function.
4881 * @param argc Standard main() style argc.
4882 * @param argv Standard main() style argv. Initial components are already
4883 * stripped.
4885 * @return Normal NTSTATUS return.
4888 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4889 const DOM_SID *domain_sid,
4890 const char *domain_name,
4891 struct cli_state *cli,
4892 struct rpc_pipe_client *pipe_hnd,
4893 TALLOC_CTX *mem_ctx,
4894 int argc,
4895 const char **argv)
4897 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4899 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4901 if (NT_STATUS_IS_OK(result)) {
4902 d_printf("\nShutdown successfully aborted\n");
4903 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4904 } else
4905 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4907 return result;
4911 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4913 * All parameters are provided by the run_rpc_command function, except for
4914 * argc, argv which are passed through.
4916 * @param c A net_context structure.
4917 * @param domain_sid The domain sid acquired from the remote server.
4918 * @param cli A cli_state connected to the server.
4919 * @param mem_ctx Talloc context, destroyed on completion of the function.
4920 * @param argc Standard main() style argc.
4921 * @param argv Standard main() style argv. Initial components are already
4922 * stripped.
4924 * @return Normal NTSTATUS return.
4927 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4928 const DOM_SID *domain_sid,
4929 const char *domain_name,
4930 struct cli_state *cli,
4931 struct rpc_pipe_client *pipe_hnd,
4932 TALLOC_CTX *mem_ctx,
4933 int argc,
4934 const char **argv)
4936 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4938 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
4940 if (NT_STATUS_IS_OK(result)) {
4941 d_printf("\nShutdown successfully aborted\n");
4942 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4943 } else
4944 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4946 return result;
4950 * ABORT the shutdown of a remote RPC server.
4952 * @param argc Standard main() style argc.
4953 * @param argv Standard main() style argv. Initial components are already
4954 * stripped.
4956 * @return A shell status integer (0 for success).
4959 static int rpc_shutdown_abort(struct net_context *c, int argc,
4960 const char **argv)
4962 int rc = -1;
4964 if (c->display_usage) {
4965 d_printf("Usage:\n"
4966 "net rpc abortshutdown\n"
4967 " Abort a scheduled shutdown\n");
4968 return 0;
4971 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
4972 rpc_shutdown_abort_internals, argc, argv);
4974 if (rc == 0)
4975 return rc;
4977 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4979 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
4980 rpc_reg_shutdown_abort_internals,
4981 argc, argv);
4985 * Shut down a remote RPC Server via initshutdown pipe.
4987 * All parameters are provided by the run_rpc_command function, except for
4988 * argc, argv which are passed through.
4990 * @param c A net_context structure.
4991 * @param domain_sid The domain sid acquired from the remote server.
4992 * @param cli A cli_state connected to the server.
4993 * @param mem_ctx Talloc context, destroyed on completion of the function.
4994 * @param argc Standard main() style argc.
4995 * @param argv Standard main() style argv. Initial components are already
4996 * stripped.
4998 * @return Normal NTSTATUS return.
5001 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5002 const DOM_SID *domain_sid,
5003 const char *domain_name,
5004 struct cli_state *cli,
5005 struct rpc_pipe_client *pipe_hnd,
5006 TALLOC_CTX *mem_ctx,
5007 int argc,
5008 const char **argv)
5010 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5011 const char *msg = "This machine will be shutdown shortly";
5012 uint32 timeout = 20;
5013 struct lsa_StringLarge msg_string;
5015 if (c->opt_comment) {
5016 msg = c->opt_comment;
5018 if (c->opt_timeout) {
5019 timeout = c->opt_timeout;
5022 msg_string.string = msg;
5024 /* create an entry */
5025 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5026 &msg_string, timeout, c->opt_force, c->opt_reboot,
5027 NULL);
5029 if (NT_STATUS_IS_OK(result)) {
5030 d_printf("\nShutdown of remote machine succeeded\n");
5031 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5032 } else {
5033 DEBUG(1,("Shutdown of remote machine failed!\n"));
5035 return result;
5039 * Shut down a remote RPC Server via winreg pipe.
5041 * All parameters are provided by the run_rpc_command function, except for
5042 * argc, argv which are passed through.
5044 * @param c A net_context structure.
5045 * @param domain_sid The domain sid acquired from the remote server.
5046 * @param cli A cli_state connected to the server.
5047 * @param mem_ctx Talloc context, destroyed on completion of the function.
5048 * @param argc Standard main() style argc.
5049 * @param argv Standard main() style argv. Initial components are already
5050 * stripped.
5052 * @return Normal NTSTATUS return.
5055 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5056 const DOM_SID *domain_sid,
5057 const char *domain_name,
5058 struct cli_state *cli,
5059 struct rpc_pipe_client *pipe_hnd,
5060 TALLOC_CTX *mem_ctx,
5061 int argc,
5062 const char **argv)
5064 const char *msg = "This machine will be shutdown shortly";
5065 uint32 timeout = 20;
5066 struct lsa_StringLarge msg_string;
5067 NTSTATUS result;
5068 WERROR werr;
5070 if (c->opt_comment) {
5071 msg = c->opt_comment;
5073 msg_string.string = msg;
5075 if (c->opt_timeout) {
5076 timeout = c->opt_timeout;
5079 /* create an entry */
5080 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5081 &msg_string, timeout, c->opt_force, c->opt_reboot,
5082 &werr);
5084 if (NT_STATUS_IS_OK(result)) {
5085 d_printf("\nShutdown of remote machine succeeded\n");
5086 } else {
5087 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5088 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5089 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5090 else
5091 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5094 return result;
5098 * Shut down a remote RPC server.
5100 * @param argc Standard main() style argc.
5101 * @param argv Standard main() style argv. Initial components are already
5102 * stripped.
5104 * @return A shell status integer (0 for success).
5107 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5109 int rc = -1;
5111 if (c->display_usage) {
5112 d_printf("Usage:\n"
5113 "net rpc shutdown\n"
5114 " Shut down a remote RPC server\n");
5115 return 0;
5118 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5119 rpc_init_shutdown_internals, argc, argv);
5121 if (rc) {
5122 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5123 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5124 rpc_reg_shutdown_internals, argc, argv);
5127 return rc;
5130 /***************************************************************************
5131 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5132 ***************************************************************************/
5135 * Add interdomain trust account to the RPC server.
5136 * All parameters (except for argc and argv) are passed by run_rpc_command
5137 * function.
5139 * @param c A net_context structure.
5140 * @param domain_sid The domain sid acquired from the server.
5141 * @param cli A cli_state connected to the server.
5142 * @param mem_ctx Talloc context, destroyed on completion of the function.
5143 * @param argc Standard main() style argc.
5144 * @param argv Standard main() style argv. Initial components are already
5145 * stripped.
5147 * @return normal NTSTATUS return code.
5150 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5151 const DOM_SID *domain_sid,
5152 const char *domain_name,
5153 struct cli_state *cli,
5154 struct rpc_pipe_client *pipe_hnd,
5155 TALLOC_CTX *mem_ctx,
5156 int argc,
5157 const char **argv)
5159 struct policy_handle connect_pol, domain_pol, user_pol;
5160 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5161 char *acct_name;
5162 struct lsa_String lsa_acct_name;
5163 uint32 acb_info;
5164 uint32 acct_flags=0;
5165 uint32 user_rid;
5166 uint32_t access_granted = 0;
5167 union samr_UserInfo info;
5168 unsigned int orig_timeout;
5170 if (argc != 2) {
5171 d_printf("Usage: net rpc trustdom add <domain_name> "
5172 "<trust password>\n");
5173 return NT_STATUS_INVALID_PARAMETER;
5177 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5180 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5181 return NT_STATUS_NO_MEMORY;
5184 strupper_m(acct_name);
5186 init_lsa_String(&lsa_acct_name, acct_name);
5188 /* Get samr policy handle */
5189 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5190 pipe_hnd->desthost,
5191 MAXIMUM_ALLOWED_ACCESS,
5192 &connect_pol);
5193 if (!NT_STATUS_IS_OK(result)) {
5194 goto done;
5197 /* Get domain policy handle */
5198 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5199 &connect_pol,
5200 MAXIMUM_ALLOWED_ACCESS,
5201 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5202 &domain_pol);
5203 if (!NT_STATUS_IS_OK(result)) {
5204 goto done;
5207 /* This call can take a long time - allow the server to time out.
5208 * 35 seconds should do it. */
5210 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5212 /* Create trusting domain's account */
5213 acb_info = ACB_NORMAL;
5214 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5215 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5216 SAMR_USER_ACCESS_SET_PASSWORD |
5217 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5218 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5220 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5221 &domain_pol,
5222 &lsa_acct_name,
5223 acb_info,
5224 acct_flags,
5225 &user_pol,
5226 &access_granted,
5227 &user_rid);
5229 /* And restore our original timeout. */
5230 rpccli_set_timeout(pipe_hnd, orig_timeout);
5232 if (!NT_STATUS_IS_OK(result)) {
5233 d_printf("net rpc trustdom add: create user %s failed %s\n",
5234 acct_name, nt_errstr(result));
5235 goto done;
5239 struct samr_CryptPassword crypt_pwd;
5241 ZERO_STRUCT(info.info23);
5243 init_samr_CryptPassword(argv[1],
5244 &cli->user_session_key,
5245 &crypt_pwd);
5247 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5248 SAMR_FIELD_NT_PASSWORD_PRESENT;
5249 info.info23.info.acct_flags = ACB_DOMTRUST;
5250 info.info23.password = crypt_pwd;
5252 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5253 &user_pol,
5255 &info);
5257 if (!NT_STATUS_IS_OK(result)) {
5258 DEBUG(0,("Could not set trust account password: %s\n",
5259 nt_errstr(result)));
5260 goto done;
5264 done:
5265 SAFE_FREE(acct_name);
5266 return result;
5270 * Create interdomain trust account for a remote domain.
5272 * @param argc Standard argc.
5273 * @param argv Standard argv without initial components.
5275 * @return Integer status (0 means success).
5278 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5280 if (argc > 0 && !c->display_usage) {
5281 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5282 rpc_trustdom_add_internals, argc, argv);
5283 } else {
5284 d_printf("Usage:\n"
5285 "net rpc trustdom add <domain_name> <trust password>\n");
5286 return -1;
5292 * Remove interdomain trust account from the RPC server.
5293 * All parameters (except for argc and argv) are passed by run_rpc_command
5294 * function.
5296 * @param c A net_context structure.
5297 * @param domain_sid The domain sid acquired from the server.
5298 * @param cli A cli_state connected to the server.
5299 * @param mem_ctx Talloc context, destroyed on completion of the function.
5300 * @param argc Standard main() style argc.
5301 * @param argv Standard main() style argv. Initial components are already
5302 * stripped.
5304 * @return normal NTSTATUS return code.
5307 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5308 const DOM_SID *domain_sid,
5309 const char *domain_name,
5310 struct cli_state *cli,
5311 struct rpc_pipe_client *pipe_hnd,
5312 TALLOC_CTX *mem_ctx,
5313 int argc,
5314 const char **argv)
5316 struct policy_handle connect_pol, domain_pol, user_pol;
5317 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5318 char *acct_name;
5319 DOM_SID trust_acct_sid;
5320 struct samr_Ids user_rids, name_types;
5321 struct lsa_String lsa_acct_name;
5323 if (argc != 1) {
5324 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5325 return NT_STATUS_INVALID_PARAMETER;
5329 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5331 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5333 if (acct_name == NULL)
5334 return NT_STATUS_NO_MEMORY;
5336 strupper_m(acct_name);
5338 /* Get samr policy handle */
5339 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5340 pipe_hnd->desthost,
5341 MAXIMUM_ALLOWED_ACCESS,
5342 &connect_pol);
5343 if (!NT_STATUS_IS_OK(result)) {
5344 goto done;
5347 /* Get domain policy handle */
5348 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5349 &connect_pol,
5350 MAXIMUM_ALLOWED_ACCESS,
5351 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5352 &domain_pol);
5353 if (!NT_STATUS_IS_OK(result)) {
5354 goto done;
5357 init_lsa_String(&lsa_acct_name, acct_name);
5359 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5360 &domain_pol,
5362 &lsa_acct_name,
5363 &user_rids,
5364 &name_types);
5366 if (!NT_STATUS_IS_OK(result)) {
5367 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5368 acct_name, nt_errstr(result) );
5369 goto done;
5372 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5373 &domain_pol,
5374 MAXIMUM_ALLOWED_ACCESS,
5375 user_rids.ids[0],
5376 &user_pol);
5378 if (!NT_STATUS_IS_OK(result)) {
5379 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5380 acct_name, nt_errstr(result) );
5381 goto done;
5384 /* append the rid to the domain sid */
5385 sid_copy(&trust_acct_sid, domain_sid);
5386 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5387 goto done;
5390 /* remove the sid */
5392 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5393 &user_pol,
5394 &trust_acct_sid);
5395 if (!NT_STATUS_IS_OK(result)) {
5396 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5397 acct_name, nt_errstr(result) );
5398 goto done;
5401 /* Delete user */
5403 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5404 &user_pol);
5406 if (!NT_STATUS_IS_OK(result)) {
5407 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5408 acct_name, nt_errstr(result) );
5409 goto done;
5412 if (!NT_STATUS_IS_OK(result)) {
5413 d_printf("Could not set trust account password: %s\n",
5414 nt_errstr(result));
5415 goto done;
5418 done:
5419 return result;
5423 * Delete interdomain trust account for a remote domain.
5425 * @param argc Standard argc.
5426 * @param argv Standard argv without initial components.
5428 * @return Integer status (0 means success).
5431 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5433 if (argc > 0 && !c->display_usage) {
5434 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5435 rpc_trustdom_del_internals, argc, argv);
5436 } else {
5437 d_printf("Usage:\n"
5438 "net rpc trustdom del <domain>\n");
5439 return -1;
5443 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5444 struct cli_state *cli,
5445 TALLOC_CTX *mem_ctx,
5446 const char *domain_name)
5448 char *dc_name = NULL;
5449 const char *buffer = NULL;
5450 struct rpc_pipe_client *netr;
5451 NTSTATUS status;
5453 /* Use NetServerEnum2 */
5455 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5456 SAFE_FREE(dc_name);
5457 return NT_STATUS_OK;
5460 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5461 for domain %s\n", domain_name));
5463 /* Try netr_GetDcName */
5465 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5466 &netr);
5467 if (!NT_STATUS_IS_OK(status)) {
5468 return status;
5471 status = rpccli_netr_GetDcName(netr, mem_ctx,
5472 cli->desthost,
5473 domain_name,
5474 &buffer,
5475 NULL);
5476 TALLOC_FREE(netr);
5478 if (NT_STATUS_IS_OK(status)) {
5479 return status;
5482 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5483 for domain %s\n", domain_name));
5485 return status;
5489 * Establish trust relationship to a trusting domain.
5490 * Interdomain account must already be created on remote PDC.
5492 * @param c A net_context structure.
5493 * @param argc Standard argc.
5494 * @param argv Standard argv without initial components.
5496 * @return Integer status (0 means success).
5499 static int rpc_trustdom_establish(struct net_context *c, int argc,
5500 const char **argv)
5502 struct cli_state *cli = NULL;
5503 struct sockaddr_storage server_ss;
5504 struct rpc_pipe_client *pipe_hnd = NULL;
5505 struct policy_handle connect_hnd;
5506 TALLOC_CTX *mem_ctx;
5507 NTSTATUS nt_status;
5508 DOM_SID *domain_sid;
5510 char* domain_name;
5511 char* acct_name;
5512 fstring pdc_name;
5513 union lsa_PolicyInformation *info = NULL;
5516 * Connect to \\server\ipc$ as 'our domain' account with password
5519 if (argc != 1 || c->display_usage) {
5520 d_printf("Usage:\n"
5521 "net rpc trustdom establish <domain_name>\n");
5522 return -1;
5525 domain_name = smb_xstrdup(argv[0]);
5526 strupper_m(domain_name);
5528 /* account name used at first is our domain's name with '$' */
5529 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5530 return -1;
5532 strupper_m(acct_name);
5535 * opt_workgroup will be used by connection functions further,
5536 * hence it should be set to remote domain name instead of ours
5538 if (c->opt_workgroup) {
5539 c->opt_workgroup = smb_xstrdup(domain_name);
5542 c->opt_user_name = acct_name;
5544 /* find the domain controller */
5545 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5546 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5547 return -1;
5550 /* connect to ipc$ as username/password */
5551 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5552 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5554 /* Is it trusting domain account for sure ? */
5555 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5556 nt_errstr(nt_status)));
5557 return -1;
5560 /* store who we connected to */
5562 saf_store( domain_name, pdc_name );
5565 * Connect to \\server\ipc$ again (this time anonymously)
5568 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5569 (char*)pdc_name);
5571 if (NT_STATUS_IS_ERR(nt_status)) {
5572 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5573 domain_name, nt_errstr(nt_status)));
5574 return -1;
5577 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5578 "domain %s", domain_name))) {
5579 DEBUG(0, ("talloc_init() failed\n"));
5580 cli_shutdown(cli);
5581 return -1;
5584 /* Make sure we're talking to a proper server */
5586 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5587 if (!NT_STATUS_IS_OK(nt_status)) {
5588 cli_shutdown(cli);
5589 talloc_destroy(mem_ctx);
5590 return -1;
5594 * Call LsaOpenPolicy and LsaQueryInfo
5597 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5598 &pipe_hnd);
5599 if (!NT_STATUS_IS_OK(nt_status)) {
5600 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5601 cli_shutdown(cli);
5602 talloc_destroy(mem_ctx);
5603 return -1;
5606 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5607 &connect_hnd);
5608 if (NT_STATUS_IS_ERR(nt_status)) {
5609 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5610 nt_errstr(nt_status)));
5611 cli_shutdown(cli);
5612 talloc_destroy(mem_ctx);
5613 return -1;
5616 /* Querying info level 5 */
5618 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5619 &connect_hnd,
5620 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5621 &info);
5622 if (NT_STATUS_IS_ERR(nt_status)) {
5623 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5624 nt_errstr(nt_status)));
5625 cli_shutdown(cli);
5626 talloc_destroy(mem_ctx);
5627 return -1;
5630 domain_sid = info->account_domain.sid;
5632 /* There should be actually query info level 3 (following nt serv behaviour),
5633 but I still don't know if it's _really_ necessary */
5636 * Store the password in secrets db
5639 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5640 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5641 cli_shutdown(cli);
5642 talloc_destroy(mem_ctx);
5643 return -1;
5647 * Close the pipes and clean up
5650 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5651 if (NT_STATUS_IS_ERR(nt_status)) {
5652 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5653 nt_errstr(nt_status)));
5654 cli_shutdown(cli);
5655 talloc_destroy(mem_ctx);
5656 return -1;
5659 cli_shutdown(cli);
5661 talloc_destroy(mem_ctx);
5663 d_printf("Trust to domain %s established\n", domain_name);
5664 return 0;
5668 * Revoke trust relationship to the remote domain.
5670 * @param c A net_context structure.
5671 * @param argc Standard argc.
5672 * @param argv Standard argv without initial components.
5674 * @return Integer status (0 means success).
5677 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5678 const char **argv)
5680 char* domain_name;
5681 int rc = -1;
5683 if (argc < 1 || c->display_usage) {
5684 d_printf("Usage:\n"
5685 "net rpc trustdom revoke <domain_name>\n"
5686 " Revoke trust relationship\n"
5687 " domain_name\tName of domain to revoke trust\n");
5688 return -1;
5691 /* generate upper cased domain name */
5692 domain_name = smb_xstrdup(argv[0]);
5693 strupper_m(domain_name);
5695 /* delete password of the trust */
5696 if (!pdb_del_trusteddom_pw(domain_name)) {
5697 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5698 domain_name));
5699 goto done;
5702 rc = 0;
5703 done:
5704 SAFE_FREE(domain_name);
5705 return rc;
5708 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5709 const DOM_SID *domain_sid,
5710 const char *domain_name,
5711 struct cli_state *cli,
5712 struct rpc_pipe_client *pipe_hnd,
5713 TALLOC_CTX *mem_ctx,
5714 int argc,
5715 const char **argv)
5717 fstring str_sid;
5718 sid_to_fstring(str_sid, domain_sid);
5719 d_printf("%s\n", str_sid);
5720 return NT_STATUS_OK;
5723 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5725 fstring ascii_sid, padding;
5726 int pad_len, col_len = 20;
5728 /* convert sid into ascii string */
5729 sid_to_fstring(ascii_sid, dom_sid);
5731 /* calculate padding space for d_printf to look nicer */
5732 pad_len = col_len - strlen(trusted_dom_name);
5733 padding[pad_len] = 0;
5734 do padding[--pad_len] = ' '; while (pad_len);
5736 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5739 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5740 TALLOC_CTX *mem_ctx,
5741 struct policy_handle *pol,
5742 DOM_SID dom_sid,
5743 const char *trusted_dom_name)
5745 NTSTATUS nt_status;
5746 union lsa_TrustedDomainInfo *info = NULL;
5747 char *cleartextpwd = NULL;
5748 uint8_t nt_hash[16];
5749 DATA_BLOB data = data_blob_null;
5751 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5752 pol,
5753 &dom_sid,
5754 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5755 &info);
5756 if (NT_STATUS_IS_ERR(nt_status)) {
5757 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5758 nt_errstr(nt_status)));
5759 goto done;
5762 data = data_blob(info->password.password->data,
5763 info->password.password->length);
5765 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
5766 DEBUG(0, ("Could not retrieve password hash\n"));
5767 goto done;
5770 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
5772 if (cleartextpwd == NULL) {
5773 DEBUG(0,("retrieved NULL password\n"));
5774 nt_status = NT_STATUS_UNSUCCESSFUL;
5775 goto done;
5778 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5779 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5780 nt_status = NT_STATUS_UNSUCCESSFUL;
5781 goto done;
5784 #ifdef DEBUG_PASSWORD
5785 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5786 "password: [%s]\n", trusted_dom_name,
5787 sid_string_dbg(&dom_sid), cleartextpwd));
5788 #endif
5790 done:
5791 SAFE_FREE(cleartextpwd);
5792 data_blob_free(&data);
5794 return nt_status;
5797 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5798 const char **argv)
5800 /* common variables */
5801 TALLOC_CTX* mem_ctx;
5802 struct cli_state *cli = NULL;
5803 struct rpc_pipe_client *pipe_hnd = NULL;
5804 NTSTATUS nt_status;
5805 const char *domain_name = NULL;
5806 DOM_SID *queried_dom_sid;
5807 struct policy_handle connect_hnd;
5808 union lsa_PolicyInformation *info = NULL;
5810 /* trusted domains listing variables */
5811 unsigned int enum_ctx = 0;
5812 int i;
5813 struct lsa_DomainList dom_list;
5814 fstring pdc_name;
5816 if (c->display_usage) {
5817 d_printf("Usage:\n"
5818 "net rpc trustdom vampire\n"
5819 " Vampire trust relationship from remote server\n");
5820 return 0;
5824 * Listing trusted domains (stored in secrets.tdb, if local)
5827 mem_ctx = talloc_init("trust relationships vampire");
5830 * set domain and pdc name to local samba server (default)
5831 * or to remote one given in command line
5834 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5835 domain_name = c->opt_workgroup;
5836 c->opt_target_workgroup = c->opt_workgroup;
5837 } else {
5838 fstrcpy(pdc_name, global_myname());
5839 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5840 c->opt_target_workgroup = domain_name;
5843 /* open \PIPE\lsarpc and open policy handle */
5844 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5845 if (!NT_STATUS_IS_OK(nt_status)) {
5846 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5847 nt_errstr(nt_status)));
5848 talloc_destroy(mem_ctx);
5849 return -1;
5852 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5853 &pipe_hnd);
5854 if (!NT_STATUS_IS_OK(nt_status)) {
5855 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5856 nt_errstr(nt_status) ));
5857 cli_shutdown(cli);
5858 talloc_destroy(mem_ctx);
5859 return -1;
5862 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5863 &connect_hnd);
5864 if (NT_STATUS_IS_ERR(nt_status)) {
5865 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5866 nt_errstr(nt_status)));
5867 cli_shutdown(cli);
5868 talloc_destroy(mem_ctx);
5869 return -1;
5872 /* query info level 5 to obtain sid of a domain being queried */
5873 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5874 &connect_hnd,
5875 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5876 &info);
5878 if (NT_STATUS_IS_ERR(nt_status)) {
5879 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5880 nt_errstr(nt_status)));
5881 cli_shutdown(cli);
5882 talloc_destroy(mem_ctx);
5883 return -1;
5886 queried_dom_sid = info->account_domain.sid;
5889 * Keep calling LsaEnumTrustdom over opened pipe until
5890 * the end of enumeration is reached
5893 d_printf("Vampire trusted domains:\n\n");
5895 do {
5896 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5897 &connect_hnd,
5898 &enum_ctx,
5899 &dom_list,
5900 (uint32_t)-1);
5901 if (NT_STATUS_IS_ERR(nt_status)) {
5902 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5903 nt_errstr(nt_status)));
5904 cli_shutdown(cli);
5905 talloc_destroy(mem_ctx);
5906 return -1;
5909 for (i = 0; i < dom_list.count; i++) {
5911 print_trusted_domain(dom_list.domains[i].sid,
5912 dom_list.domains[i].name.string);
5914 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5915 *dom_list.domains[i].sid,
5916 dom_list.domains[i].name.string);
5917 if (!NT_STATUS_IS_OK(nt_status)) {
5918 cli_shutdown(cli);
5919 talloc_destroy(mem_ctx);
5920 return -1;
5925 * in case of no trusted domains say something rather
5926 * than just display blank line
5928 if (!dom_list.count) d_printf("none\n");
5930 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5932 /* close this connection before doing next one */
5933 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5934 if (NT_STATUS_IS_ERR(nt_status)) {
5935 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5936 nt_errstr(nt_status)));
5937 cli_shutdown(cli);
5938 talloc_destroy(mem_ctx);
5939 return -1;
5942 /* close lsarpc pipe and connection to IPC$ */
5943 cli_shutdown(cli);
5945 talloc_destroy(mem_ctx);
5946 return 0;
5949 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
5951 /* common variables */
5952 TALLOC_CTX* mem_ctx;
5953 struct cli_state *cli = NULL, *remote_cli = NULL;
5954 struct rpc_pipe_client *pipe_hnd = NULL;
5955 NTSTATUS nt_status;
5956 const char *domain_name = NULL;
5957 DOM_SID *queried_dom_sid;
5958 int ascii_dom_name_len;
5959 struct policy_handle connect_hnd;
5960 union lsa_PolicyInformation *info = NULL;
5962 /* trusted domains listing variables */
5963 unsigned int num_domains, enum_ctx = 0;
5964 int i;
5965 struct lsa_DomainList dom_list;
5966 fstring pdc_name;
5967 bool found_domain;
5969 /* trusting domains listing variables */
5970 struct policy_handle domain_hnd;
5971 struct samr_SamArray *trusts = NULL;
5973 if (c->display_usage) {
5974 d_printf("Usage:\n"
5975 "net rpc trustdom list\n"
5976 " List trust relationships\n");
5977 return 0;
5981 * Listing trusted domains (stored in secrets.tdb, if local)
5984 mem_ctx = talloc_init("trust relationships listing");
5987 * set domain and pdc name to local samba server (default)
5988 * or to remote one given in command line
5991 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5992 domain_name = c->opt_workgroup;
5993 c->opt_target_workgroup = c->opt_workgroup;
5994 } else {
5995 fstrcpy(pdc_name, global_myname());
5996 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5997 c->opt_target_workgroup = domain_name;
6000 /* open \PIPE\lsarpc and open policy handle */
6001 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6002 if (!NT_STATUS_IS_OK(nt_status)) {
6003 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6004 nt_errstr(nt_status)));
6005 talloc_destroy(mem_ctx);
6006 return -1;
6009 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6010 &pipe_hnd);
6011 if (!NT_STATUS_IS_OK(nt_status)) {
6012 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6013 nt_errstr(nt_status) ));
6014 cli_shutdown(cli);
6015 talloc_destroy(mem_ctx);
6016 return -1;
6019 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6020 &connect_hnd);
6021 if (NT_STATUS_IS_ERR(nt_status)) {
6022 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6023 nt_errstr(nt_status)));
6024 cli_shutdown(cli);
6025 talloc_destroy(mem_ctx);
6026 return -1;
6029 /* query info level 5 to obtain sid of a domain being queried */
6030 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6031 &connect_hnd,
6032 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6033 &info);
6035 if (NT_STATUS_IS_ERR(nt_status)) {
6036 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6037 nt_errstr(nt_status)));
6038 cli_shutdown(cli);
6039 talloc_destroy(mem_ctx);
6040 return -1;
6043 queried_dom_sid = info->account_domain.sid;
6046 * Keep calling LsaEnumTrustdom over opened pipe until
6047 * the end of enumeration is reached
6050 d_printf("Trusted domains list:\n\n");
6052 found_domain = false;
6054 do {
6055 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6056 &connect_hnd,
6057 &enum_ctx,
6058 &dom_list,
6059 (uint32_t)-1);
6060 if (NT_STATUS_IS_ERR(nt_status)) {
6061 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6062 nt_errstr(nt_status)));
6063 cli_shutdown(cli);
6064 talloc_destroy(mem_ctx);
6065 return -1;
6068 for (i = 0; i < dom_list.count; i++) {
6069 print_trusted_domain(dom_list.domains[i].sid,
6070 dom_list.domains[i].name.string);
6071 found_domain = true;
6074 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6077 * in case of no trusted domains say something rather
6078 * than just display blank line
6080 if (!found_domain) {
6081 d_printf("none\n");
6084 /* close this connection before doing next one */
6085 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6086 if (NT_STATUS_IS_ERR(nt_status)) {
6087 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6088 nt_errstr(nt_status)));
6089 cli_shutdown(cli);
6090 talloc_destroy(mem_ctx);
6091 return -1;
6094 TALLOC_FREE(pipe_hnd);
6097 * Listing trusting domains (stored in passdb backend, if local)
6100 d_printf("\nTrusting domains list:\n\n");
6103 * Open \PIPE\samr and get needed policy handles
6105 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6106 &pipe_hnd);
6107 if (!NT_STATUS_IS_OK(nt_status)) {
6108 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6109 cli_shutdown(cli);
6110 talloc_destroy(mem_ctx);
6111 return -1;
6114 /* SamrConnect2 */
6115 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6116 pipe_hnd->desthost,
6117 SAMR_ACCESS_LOOKUP_DOMAIN,
6118 &connect_hnd);
6119 if (!NT_STATUS_IS_OK(nt_status)) {
6120 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6121 nt_errstr(nt_status)));
6122 cli_shutdown(cli);
6123 talloc_destroy(mem_ctx);
6124 return -1;
6127 /* SamrOpenDomain - we have to open domain policy handle in order to be
6128 able to enumerate accounts*/
6129 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6130 &connect_hnd,
6131 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6132 queried_dom_sid,
6133 &domain_hnd);
6134 if (!NT_STATUS_IS_OK(nt_status)) {
6135 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6136 nt_errstr(nt_status)));
6137 cli_shutdown(cli);
6138 talloc_destroy(mem_ctx);
6139 return -1;
6143 * perform actual enumeration
6146 found_domain = false;
6148 enum_ctx = 0; /* reset enumeration context from last enumeration */
6149 do {
6151 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6152 &domain_hnd,
6153 &enum_ctx,
6154 ACB_DOMTRUST,
6155 &trusts,
6156 0xffff,
6157 &num_domains);
6158 if (NT_STATUS_IS_ERR(nt_status)) {
6159 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6160 nt_errstr(nt_status)));
6161 cli_shutdown(cli);
6162 talloc_destroy(mem_ctx);
6163 return -1;
6166 for (i = 0; i < num_domains; i++) {
6168 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6170 found_domain = true;
6173 * get each single domain's sid (do we _really_ need this ?):
6174 * 1) connect to domain's pdc
6175 * 2) query the pdc for domain's sid
6178 /* get rid of '$' tail */
6179 ascii_dom_name_len = strlen(str);
6180 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6181 str[ascii_dom_name_len - 1] = '\0';
6183 /* set opt_* variables to remote domain */
6184 strupper_m(str);
6185 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6186 c->opt_target_workgroup = c->opt_workgroup;
6188 d_printf("%-20s", str);
6190 /* connect to remote domain controller */
6191 nt_status = net_make_ipc_connection(c,
6192 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6193 &remote_cli);
6194 if (NT_STATUS_IS_OK(nt_status)) {
6195 /* query for domain's sid */
6196 if (run_rpc_command(
6197 c, remote_cli,
6198 &ndr_table_lsarpc.syntax_id, 0,
6199 rpc_query_domain_sid, argc,
6200 argv))
6201 d_fprintf(stderr, "couldn't get domain's sid\n");
6203 cli_shutdown(remote_cli);
6205 } else {
6206 d_fprintf(stderr, "domain controller is not "
6207 "responding: %s\n",
6208 nt_errstr(nt_status));
6212 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6214 if (!found_domain) {
6215 d_printf("none\n");
6218 /* close opened samr and domain policy handles */
6219 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6220 if (!NT_STATUS_IS_OK(nt_status)) {
6221 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6224 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6225 if (!NT_STATUS_IS_OK(nt_status)) {
6226 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6229 /* close samr pipe and connection to IPC$ */
6230 cli_shutdown(cli);
6232 talloc_destroy(mem_ctx);
6233 return 0;
6237 * Entrypoint for 'net rpc trustdom' code.
6239 * @param argc Standard argc.
6240 * @param argv Standard argv without initial components.
6242 * @return Integer status (0 means success).
6245 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6247 struct functable func[] = {
6249 "add",
6250 rpc_trustdom_add,
6251 NET_TRANSPORT_RPC,
6252 "Add trusted domain's account",
6253 "net rpc trustdom add\n"
6254 " Add trusted domain's account"
6257 "del",
6258 rpc_trustdom_del,
6259 NET_TRANSPORT_RPC,
6260 "Remove trusted domain's account",
6261 "net rpc trustdom del\n"
6262 " Remove trusted domain's account"
6265 "establish",
6266 rpc_trustdom_establish,
6267 NET_TRANSPORT_RPC,
6268 "Establish trust relationship",
6269 "net rpc trustdom establish\n"
6270 " Establish trust relationship"
6273 "revoke",
6274 rpc_trustdom_revoke,
6275 NET_TRANSPORT_RPC,
6276 "Revoke trust relationship",
6277 "net rpc trustdom revoke\n"
6278 " Revoke trust relationship"
6281 "list",
6282 rpc_trustdom_list,
6283 NET_TRANSPORT_RPC,
6284 "List domain trusts",
6285 "net rpc trustdom list\n"
6286 " List domain trusts"
6289 "vampire",
6290 rpc_trustdom_vampire,
6291 NET_TRANSPORT_RPC,
6292 "Vampire trusts from remote server",
6293 "net rpc trustdom vampire\n"
6294 " Vampire trusts from remote server"
6296 {NULL, NULL, 0, NULL, NULL}
6299 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6303 * Check if a server will take rpc commands
6304 * @param flags Type of server to connect to (PDC, DMB, localhost)
6305 * if the host is not explicitly specified
6306 * @return bool (true means rpc supported)
6308 bool net_rpc_check(struct net_context *c, unsigned flags)
6310 struct cli_state *cli;
6311 bool ret = false;
6312 struct sockaddr_storage server_ss;
6313 char *server_name = NULL;
6314 NTSTATUS status;
6316 /* flags (i.e. server type) may depend on command */
6317 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6318 return false;
6320 if ((cli = cli_initialise()) == NULL) {
6321 return false;
6324 status = cli_connect(cli, server_name, &server_ss);
6325 if (!NT_STATUS_IS_OK(status))
6326 goto done;
6327 if (!attempt_netbios_session_request(&cli, global_myname(),
6328 server_name, &server_ss))
6329 goto done;
6330 status = cli_negprot(cli);
6331 if (!NT_STATUS_IS_OK(status))
6332 goto done;
6333 if (cli->protocol < PROTOCOL_NT1)
6334 goto done;
6336 ret = true;
6337 done:
6338 cli_shutdown(cli);
6339 return ret;
6342 /* dump sam database via samsync rpc calls */
6343 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6344 if (c->display_usage) {
6345 d_printf("Usage:\n"
6346 "net rpc samdump\n"
6347 " Dump remote SAM database\n");
6348 return 0;
6351 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6352 NET_FLAGS_ANONYMOUS,
6353 rpc_samdump_internals, argc, argv);
6356 /* syncronise sam database via samsync rpc calls */
6357 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6359 struct functable func[] = {
6361 "ldif",
6362 rpc_vampire_ldif,
6363 NET_TRANSPORT_RPC,
6364 "Dump remote SAM database to ldif",
6365 "net rpc vampire ldif\n"
6366 " Dump remote SAM database to LDIF file or stdout"
6369 "keytab",
6370 rpc_vampire_keytab,
6371 NET_TRANSPORT_RPC,
6372 "Dump remote SAM database to Kerberos Keytab",
6373 "net rpc vampire keytab\n"
6374 " Dump remote SAM database to Kerberos keytab file"
6377 "passdb",
6378 rpc_vampire_passdb,
6379 NET_TRANSPORT_RPC,
6380 "Dump remote SAM database to passdb",
6381 "net rpc vampire passdb\n"
6382 " Dump remote SAM database to passdb"
6385 {NULL, NULL, 0, NULL, NULL}
6388 if (argc == 0) {
6389 if (c->display_usage) {
6390 d_printf("Usage:\n"
6391 "net rpc vampire\n"
6392 " Vampire remote SAM database\n");
6393 return 0;
6396 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6397 NET_FLAGS_ANONYMOUS,
6398 rpc_vampire_internals,
6399 argc, argv);
6402 return net_run_function(c, argc, argv, "net rpc vampire", func);
6406 * Migrate everything from a print server.
6408 * @param c A net_context structure.
6409 * @param argc Standard main() style argc.
6410 * @param argv Standard main() style argv. Initial components are already
6411 * stripped.
6413 * @return A shell status integer (0 for success).
6415 * The order is important !
6416 * To successfully add drivers the print queues have to exist !
6417 * Applying ACLs should be the last step, because you're easily locked out.
6420 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6421 const char **argv)
6423 int ret;
6425 if (c->display_usage) {
6426 d_printf("Usage:\n"
6427 "net rpc printer migrate all\n"
6428 " Migrate everything from a print server\n");
6429 return 0;
6432 if (!c->opt_host) {
6433 d_printf("no server to migrate\n");
6434 return -1;
6437 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6438 rpc_printer_migrate_printers_internals, argc,
6439 argv);
6440 if (ret)
6441 return ret;
6443 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6444 rpc_printer_migrate_drivers_internals, argc,
6445 argv);
6446 if (ret)
6447 return ret;
6449 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6450 rpc_printer_migrate_forms_internals, argc, argv);
6451 if (ret)
6452 return ret;
6454 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6455 rpc_printer_migrate_settings_internals, argc,
6456 argv);
6457 if (ret)
6458 return ret;
6460 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6461 rpc_printer_migrate_security_internals, argc,
6462 argv);
6467 * Migrate print drivers from a print server.
6469 * @param c A net_context structure.
6470 * @param argc Standard main() style argc.
6471 * @param argv Standard main() style argv. Initial components are already
6472 * stripped.
6474 * @return A shell status integer (0 for success).
6476 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6477 const char **argv)
6479 if (c->display_usage) {
6480 d_printf("Usage:\n"
6481 "net rpc printer migrate drivers\n"
6482 " Migrate print-drivers from a print-server\n");
6483 return 0;
6486 if (!c->opt_host) {
6487 d_printf("no server to migrate\n");
6488 return -1;
6491 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6492 rpc_printer_migrate_drivers_internals,
6493 argc, argv);
6497 * Migrate print-forms from a print-server.
6499 * @param c A net_context structure.
6500 * @param argc Standard main() style argc.
6501 * @param argv Standard main() style argv. Initial components are already
6502 * stripped.
6504 * @return A shell status integer (0 for success).
6506 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6507 const char **argv)
6509 if (c->display_usage) {
6510 d_printf("Usage:\n"
6511 "net rpc printer migrate forms\n"
6512 " Migrate print-forms from a print-server\n");
6513 return 0;
6516 if (!c->opt_host) {
6517 d_printf("no server to migrate\n");
6518 return -1;
6521 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6522 rpc_printer_migrate_forms_internals,
6523 argc, argv);
6527 * Migrate printers from a print-server.
6529 * @param c A net_context structure.
6530 * @param argc Standard main() style argc.
6531 * @param argv Standard main() style argv. Initial components are already
6532 * stripped.
6534 * @return A shell status integer (0 for success).
6536 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6537 const char **argv)
6539 if (c->display_usage) {
6540 d_printf("Usage:\n"
6541 "net rpc printer migrate printers\n"
6542 " Migrate printers from a print-server\n");
6543 return 0;
6546 if (!c->opt_host) {
6547 d_printf("no server to migrate\n");
6548 return -1;
6551 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6552 rpc_printer_migrate_printers_internals,
6553 argc, argv);
6557 * Migrate printer-ACLs from a print-server
6559 * @param c A net_context structure.
6560 * @param argc Standard main() style argc.
6561 * @param argv Standard main() style argv. Initial components are already
6562 * stripped.
6564 * @return A shell status integer (0 for success).
6566 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6567 const char **argv)
6569 if (c->display_usage) {
6570 d_printf("Usage:\n"
6571 "net rpc printer migrate security\n"
6572 " Migrate printer-ACLs from a print-server\n");
6573 return 0;
6576 if (!c->opt_host) {
6577 d_printf("no server to migrate\n");
6578 return -1;
6581 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6582 rpc_printer_migrate_security_internals,
6583 argc, argv);
6587 * Migrate printer-settings from a print-server.
6589 * @param c A net_context structure.
6590 * @param argc Standard main() style argc.
6591 * @param argv Standard main() style argv. Initial components are already
6592 * stripped.
6594 * @return A shell status integer (0 for success).
6596 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6597 const char **argv)
6599 if (c->display_usage) {
6600 d_printf("Usage:\n"
6601 "net rpc printer migrate settings\n"
6602 " Migrate printer-settings from a print-server\n");
6603 return 0;
6606 if (!c->opt_host) {
6607 d_printf("no server to migrate\n");
6608 return -1;
6611 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6612 rpc_printer_migrate_settings_internals,
6613 argc, argv);
6617 * 'net rpc printer' entrypoint.
6619 * @param c A net_context structure.
6620 * @param argc Standard main() style argc.
6621 * @param argv Standard main() style argv. Initial components are already
6622 * stripped.
6625 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6628 /* ouch: when addriver and setdriver are called from within
6629 rpc_printer_migrate_drivers_internals, the printer-queue already
6630 *has* to exist */
6632 struct functable func[] = {
6634 "all",
6635 rpc_printer_migrate_all,
6636 NET_TRANSPORT_RPC,
6637 "Migrate all from remote to local print server",
6638 "net rpc printer migrate all\n"
6639 " Migrate all from remote to local print server"
6642 "drivers",
6643 rpc_printer_migrate_drivers,
6644 NET_TRANSPORT_RPC,
6645 "Migrate drivers to local server",
6646 "net rpc printer migrate drivers\n"
6647 " Migrate drivers to local server"
6650 "forms",
6651 rpc_printer_migrate_forms,
6652 NET_TRANSPORT_RPC,
6653 "Migrate froms to local server",
6654 "net rpc printer migrate forms\n"
6655 " Migrate froms to local server"
6658 "printers",
6659 rpc_printer_migrate_printers,
6660 NET_TRANSPORT_RPC,
6661 "Migrate printers to local server",
6662 "net rpc printer migrate printers\n"
6663 " Migrate printers to local server"
6666 "security",
6667 rpc_printer_migrate_security,
6668 NET_TRANSPORT_RPC,
6669 "Mirgate printer ACLs to local server",
6670 "net rpc printer migrate security\n"
6671 " Mirgate printer ACLs to local server"
6674 "settings",
6675 rpc_printer_migrate_settings,
6676 NET_TRANSPORT_RPC,
6677 "Migrate printer settings to local server",
6678 "net rpc printer migrate settings\n"
6679 " Migrate printer settings to local server"
6681 {NULL, NULL, 0, NULL, NULL}
6684 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6689 * List printers on a remote RPC server.
6691 * @param c A net_context structure.
6692 * @param argc Standard main() style argc.
6693 * @param argv Standard main() style argv. Initial components are already
6694 * stripped.
6696 * @return A shell status integer (0 for success).
6698 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6700 if (c->display_usage) {
6701 d_printf("Usage:\n"
6702 "net rpc printer list\n"
6703 " List printers on a remote RPC server\n");
6704 return 0;
6707 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6708 rpc_printer_list_internals,
6709 argc, argv);
6713 * List printer-drivers on a remote RPC server.
6715 * @param c A net_context structure.
6716 * @param argc Standard main() style argc.
6717 * @param argv Standard main() style argv. Initial components are already
6718 * stripped.
6720 * @return A shell status integer (0 for success).
6722 static int rpc_printer_driver_list(struct net_context *c, int argc,
6723 const char **argv)
6725 if (c->display_usage) {
6726 d_printf("Usage:\n"
6727 "net rpc printer driver\n"
6728 " List printer-drivers on a remote RPC server\n");
6729 return 0;
6732 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6733 rpc_printer_driver_list_internals,
6734 argc, argv);
6738 * Publish printer in ADS via MSRPC.
6740 * @param c A net_context structure.
6741 * @param argc Standard main() style argc.
6742 * @param argv Standard main() style argv. Initial components are already
6743 * stripped.
6745 * @return A shell status integer (0 for success).
6747 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6748 const char **argv)
6750 if (c->display_usage) {
6751 d_printf("Usage:\n"
6752 "net rpc printer publish publish\n"
6753 " Publish printer in ADS via MSRPC\n");
6754 return 0;
6757 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6758 rpc_printer_publish_publish_internals,
6759 argc, argv);
6763 * Update printer in ADS via MSRPC.
6765 * @param c A net_context structure.
6766 * @param argc Standard main() style argc.
6767 * @param argv Standard main() style argv. Initial components are already
6768 * stripped.
6770 * @return A shell status integer (0 for success).
6772 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6774 if (c->display_usage) {
6775 d_printf("Usage:\n"
6776 "net rpc printer publish update\n"
6777 " Update printer in ADS via MSRPC\n");
6778 return 0;
6781 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6782 rpc_printer_publish_update_internals,
6783 argc, argv);
6787 * UnPublish printer in ADS via MSRPC.
6789 * @param c A net_context structure.
6790 * @param argc Standard main() style argc.
6791 * @param argv Standard main() style argv. Initial components are already
6792 * stripped.
6794 * @return A shell status integer (0 for success).
6796 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6797 const char **argv)
6799 if (c->display_usage) {
6800 d_printf("Usage:\n"
6801 "net rpc printer publish unpublish\n"
6802 " UnPublish printer in ADS via MSRPC\n");
6803 return 0;
6806 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6807 rpc_printer_publish_unpublish_internals,
6808 argc, argv);
6812 * List published printers via MSRPC.
6814 * @param c A net_context structure.
6815 * @param argc Standard main() style argc.
6816 * @param argv Standard main() style argv. Initial components are already
6817 * stripped.
6819 * @return A shell status integer (0 for success).
6821 static int rpc_printer_publish_list(struct net_context *c, int argc,
6822 const char **argv)
6824 if (c->display_usage) {
6825 d_printf("Usage:\n"
6826 "net rpc printer publish list\n"
6827 " List published printers via MSRPC\n");
6828 return 0;
6831 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6832 rpc_printer_publish_list_internals,
6833 argc, argv);
6838 * Publish printer in ADS.
6840 * @param c A net_context structure.
6841 * @param argc Standard main() style argc.
6842 * @param argv Standard main() style argv. Initial components are already
6843 * stripped.
6845 * @return A shell status integer (0 for success).
6847 static int rpc_printer_publish(struct net_context *c, int argc,
6848 const char **argv)
6851 struct functable func[] = {
6853 "publish",
6854 rpc_printer_publish_publish,
6855 NET_TRANSPORT_RPC,
6856 "Publish printer in AD",
6857 "net rpc printer publish publish\n"
6858 " Publish printer in AD"
6861 "update",
6862 rpc_printer_publish_update,
6863 NET_TRANSPORT_RPC,
6864 "Update printer in AD",
6865 "net rpc printer publish update\n"
6866 " Update printer in AD"
6869 "unpublish",
6870 rpc_printer_publish_unpublish,
6871 NET_TRANSPORT_RPC,
6872 "Unpublish printer",
6873 "net rpc printer publish unpublish\n"
6874 " Unpublish printer"
6877 "list",
6878 rpc_printer_publish_list,
6879 NET_TRANSPORT_RPC,
6880 "List published printers",
6881 "net rpc printer publish list\n"
6882 " List published printers"
6884 {NULL, NULL, 0, NULL, NULL}
6887 if (argc == 0) {
6888 if (c->display_usage) {
6889 d_printf("Usage:\n");
6890 d_printf("net rpc printer publish\n"
6891 " List published printers\n"
6892 " Alias of net rpc printer publish list\n");
6893 net_display_usage_from_functable(func);
6894 return 0;
6896 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6897 rpc_printer_publish_list_internals,
6898 argc, argv);
6901 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6907 * Display rpc printer help page.
6909 * @param c A net_context structure.
6910 * @param argc Standard main() style argc.
6911 * @param argv Standard main() style argv. Initial components are already
6912 * stripped.
6914 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6916 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
6917 "\tlists all printers on print-server\n\n");
6918 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6919 "\tlists all printer-drivers on print-server\n\n");
6920 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6921 "\tpublishes printer settings in Active Directory\n"
6922 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6923 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6924 "\n\tmigrates printers from remote to local server\n\n");
6925 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6926 "\n\tmigrates printer-settings from remote to local server\n\n");
6927 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6928 "\n\tmigrates printer-drivers from remote to local server\n\n");
6929 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6930 "\n\tmigrates printer-forms from remote to local server\n\n");
6931 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6932 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6933 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6934 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6935 "\tremote to local print-server\n\n");
6936 net_common_methods_usage(c, argc, argv);
6937 net_common_flags_usage(c, argc, argv);
6938 d_printf(
6939 "\t-v or --verbose\t\t\tgive verbose output\n"
6940 "\t --destination\t\tmigration target server (default: localhost)\n");
6942 return -1;
6946 * 'net rpc printer' entrypoint.
6948 * @param c A net_context structure.
6949 * @param argc Standard main() style argc.
6950 * @param argv Standard main() style argv. Initial components are already
6951 * stripped.
6953 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
6955 struct functable func[] = {
6957 "list",
6958 rpc_printer_list,
6959 NET_TRANSPORT_RPC,
6960 "List all printers on print server",
6961 "net rpc printer list\n"
6962 " List all printers on print server"
6965 "migrate",
6966 rpc_printer_migrate,
6967 NET_TRANSPORT_RPC,
6968 "Migrate printer to local server",
6969 "net rpc printer migrate\n"
6970 " Migrate printer to local server"
6973 "driver",
6974 rpc_printer_driver_list,
6975 NET_TRANSPORT_RPC,
6976 "List printer drivers",
6977 "net rpc printer driver\n"
6978 " List printer drivers"
6981 "publish",
6982 rpc_printer_publish,
6983 NET_TRANSPORT_RPC,
6984 "Publish printer in AD",
6985 "net rpc printer publish\n"
6986 " Publish printer in AD"
6988 {NULL, NULL, 0, NULL, NULL}
6991 if (argc == 0) {
6992 if (c->display_usage) {
6993 d_printf("Usage:\n");
6994 d_printf("net rpc printer\n"
6995 " List printers\n");
6996 net_display_usage_from_functable(func);
6997 return 0;
6999 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7000 rpc_printer_list_internals,
7001 argc, argv);
7004 return net_run_function(c, argc, argv, "net rpc printer", func);
7008 * 'net rpc' entrypoint.
7010 * @param c A net_context structure.
7011 * @param argc Standard main() style argc.
7012 * @param argv Standard main() style argv. Initial components are already
7013 * stripped.
7016 int net_rpc(struct net_context *c, int argc, const char **argv)
7018 NET_API_STATUS status;
7020 struct functable func[] = {
7022 "audit",
7023 net_rpc_audit,
7024 NET_TRANSPORT_RPC,
7025 "Modify global audit settings",
7026 "net rpc audit\n"
7027 " Modify global audit settings"
7030 "info",
7031 net_rpc_info,
7032 NET_TRANSPORT_RPC,
7033 "Show basic info about a domain",
7034 "net rpc info\n"
7035 " Show basic info about a domain"
7038 "join",
7039 net_rpc_join,
7040 NET_TRANSPORT_RPC,
7041 "Join a domain",
7042 "net rpc join\n"
7043 " Join a domain"
7046 "oldjoin",
7047 net_rpc_oldjoin,
7048 NET_TRANSPORT_RPC,
7049 "Join a domain created in server manager",
7050 "net rpc oldjoin\n"
7051 " Join a domain created in server manager"
7054 "testjoin",
7055 net_rpc_testjoin,
7056 NET_TRANSPORT_RPC,
7057 "Test that a join is valid",
7058 "net rpc testjoin\n"
7059 " Test that a join is valid"
7062 "user",
7063 net_rpc_user,
7064 NET_TRANSPORT_RPC,
7065 "List/modify users",
7066 "net rpc user\n"
7067 " List/modify users"
7070 "password",
7071 rpc_user_password,
7072 NET_TRANSPORT_RPC,
7073 "Change a user password",
7074 "net rpc password\n"
7075 " Change a user password\n"
7076 " Alias for net rpc user password"
7079 "group",
7080 net_rpc_group,
7081 NET_TRANSPORT_RPC,
7082 "List/modify groups",
7083 "net rpc group\n"
7084 " List/modify groups"
7087 "share",
7088 net_rpc_share,
7089 NET_TRANSPORT_RPC,
7090 "List/modify shares",
7091 "net rpc share\n"
7092 " List/modify shares"
7095 "file",
7096 net_rpc_file,
7097 NET_TRANSPORT_RPC,
7098 "List open files",
7099 "net rpc file\n"
7100 " List open files"
7103 "printer",
7104 net_rpc_printer,
7105 NET_TRANSPORT_RPC,
7106 "List/modify printers",
7107 "net rpc printer\n"
7108 " List/modify printers"
7111 "changetrustpw",
7112 net_rpc_changetrustpw,
7113 NET_TRANSPORT_RPC,
7114 "Change trust account password",
7115 "net rpc changetrustpw\n"
7116 " Change trust account password"
7119 "trustdom",
7120 rpc_trustdom,
7121 NET_TRANSPORT_RPC,
7122 "Modify domain trusts",
7123 "net rpc trustdom\n"
7124 " Modify domain trusts"
7127 "abortshutdown",
7128 rpc_shutdown_abort,
7129 NET_TRANSPORT_RPC,
7130 "Abort a remote shutdown",
7131 "net rpc abortshutdown\n"
7132 " Abort a remote shutdown"
7135 "shutdown",
7136 rpc_shutdown,
7137 NET_TRANSPORT_RPC,
7138 "Shutdown a remote server",
7139 "net rpc shutdown\n"
7140 " Shutdown a remote server"
7143 "samdump",
7144 rpc_samdump,
7145 NET_TRANSPORT_RPC,
7146 "Dump SAM data of remote NT PDC",
7147 "net rpc samdump\n"
7148 " Dump SAM data of remote NT PDC"
7151 "vampire",
7152 rpc_vampire,
7153 NET_TRANSPORT_RPC,
7154 "Sync a remote NT PDC's data into local passdb",
7155 "net rpc vampire\n"
7156 " Sync a remote NT PDC's data into local passdb"
7159 "getsid",
7160 net_rpc_getsid,
7161 NET_TRANSPORT_RPC,
7162 "Fetch the domain sid into local secrets.tdb",
7163 "net rpc getsid\n"
7164 " Fetch the domain sid into local secrets.tdb"
7167 "rights",
7168 net_rpc_rights,
7169 NET_TRANSPORT_RPC,
7170 "Manage privileges assigned to SID",
7171 "net rpc rights\n"
7172 " Manage privileges assigned to SID"
7175 "service",
7176 net_rpc_service,
7177 NET_TRANSPORT_RPC,
7178 "Start/stop/query remote services",
7179 "net rpc service\n"
7180 " Start/stop/query remote services"
7183 "registry",
7184 net_rpc_registry,
7185 NET_TRANSPORT_RPC,
7186 "Manage registry hives",
7187 "net rpc registry\n"
7188 " Manage registry hives"
7191 "shell",
7192 net_rpc_shell,
7193 NET_TRANSPORT_RPC,
7194 "Open interactive shell on remote server",
7195 "net rpc shell\n"
7196 " Open interactive shell on remote server"
7198 {NULL, NULL, 0, NULL, NULL}
7201 status = libnetapi_init(&c->netapi_ctx);
7202 if (status != 0) {
7203 return -1;
7205 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7206 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7207 if (c->opt_kerberos) {
7208 libnetapi_set_use_kerberos(c->netapi_ctx);
7211 return net_run_function(c, argc, argv, "net rpc", func);