s3: Add an async smbsock_connect
[Samba.git] / source3 / utils / net_rpc.c
blobaa041720b1c913fb7a5cd4ac4767af055444a663
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 = talloc_strdup(c, getpass(prompt));
762 SAFE_FREE(prompt);
763 if (u1003.usri1003_password == NULL) {
764 return -1;
768 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
770 /* Display results */
771 if (status != 0) {
772 d_fprintf(stderr, "Failed to set password for '%s' with: %s.\n",
773 argv[0], libnetapi_get_error_string(c->netapi_ctx,
774 status));
775 return -1;
778 return 0;
782 * List a user's groups from a remote RPC server.
784 * @param argc Standard main() style argc.
785 * @param argv Standard main() style argv. Initial components are already
786 * stripped.
788 * @return A shell status integer (0 for success)
791 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
794 NET_API_STATUS status;
795 struct GROUP_USERS_INFO_0 *u0 = NULL;
796 uint32_t entries_read = 0;
797 uint32_t total_entries = 0;
798 int i;
801 if (argc < 1 || c->display_usage) {
802 rpc_user_usage(c, argc, argv);
803 return 0;
806 status = NetUserGetGroups(c->opt_host,
807 argv[0],
809 (uint8_t **)(void *)&u0,
810 (uint32_t)-1,
811 &entries_read,
812 &total_entries);
813 if (status != 0) {
814 d_fprintf(stderr, "Failed to get groups for '%s' with: %s.\n",
815 argv[0], libnetapi_get_error_string(c->netapi_ctx,
816 status));
817 return -1;
820 for (i=0; i < entries_read; i++) {
821 printf("%s\n", u0->grui0_name);
822 u0++;
825 return 0;
829 * List users on a remote RPC server.
831 * All parameters are provided by the run_rpc_command function, except for
832 * argc, argv which are passed through.
834 * @param domain_sid The domain sid acquired from the remote server.
835 * @param cli A cli_state connected to the server.
836 * @param mem_ctx Talloc context, destroyed on completion of the function.
837 * @param argc Standard main() style argc.
838 * @param argv Standard main() style argv. Initial components are already
839 * stripped.
841 * @return Normal NTSTATUS return.
844 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
846 NET_API_STATUS status;
847 uint32_t start_idx=0, num_entries, i, loop_count = 0;
848 struct NET_DISPLAY_USER *info = NULL;
849 void *buffer = NULL;
851 /* Query domain users */
852 if (c->opt_long_list_entries)
853 d_printf("\nUser name Comment"
854 "\n-----------------------------\n");
855 do {
856 uint32_t max_entries, max_size;
858 get_query_dispinfo_params(
859 loop_count, &max_entries, &max_size);
861 status = NetQueryDisplayInformation(c->opt_host,
863 start_idx,
864 max_entries,
865 max_size,
866 &num_entries,
867 &buffer);
868 if (status != 0 && status != ERROR_MORE_DATA) {
869 return status;
872 info = (struct NET_DISPLAY_USER *)buffer;
874 for (i = 0; i < num_entries; i++) {
876 if (c->opt_long_list_entries)
877 printf("%-21.21s %s\n", info->usri1_name,
878 info->usri1_comment);
879 else
880 printf("%s\n", info->usri1_name);
881 info++;
884 NetApiBufferFree(buffer);
886 loop_count++;
887 start_idx += num_entries;
889 } while (status == ERROR_MORE_DATA);
891 return status;
895 * 'net rpc user' entrypoint.
896 * @param argc Standard main() style argc.
897 * @param argv Standard main() style argv. Initial components are already
898 * stripped.
901 int net_rpc_user(struct net_context *c, int argc, const char **argv)
903 NET_API_STATUS status;
905 struct functable func[] = {
907 "add",
908 rpc_user_add,
909 NET_TRANSPORT_RPC,
910 "Add specified user",
911 "net rpc user add\n"
912 " Add specified user"
915 "info",
916 rpc_user_info,
917 NET_TRANSPORT_RPC,
918 "List domain groups of user",
919 "net rpc user info\n"
920 " Lis domain groups of user"
923 "delete",
924 rpc_user_delete,
925 NET_TRANSPORT_RPC,
926 "Remove specified user",
927 "net rpc user delete\n"
928 " Remove specified user"
931 "password",
932 rpc_user_password,
933 NET_TRANSPORT_RPC,
934 "Change user password",
935 "net rpc user password\n"
936 " Change user password"
939 "rename",
940 rpc_user_rename,
941 NET_TRANSPORT_RPC,
942 "Rename specified user",
943 "net rpc user rename\n"
944 " Rename specified user"
946 {NULL, NULL, 0, NULL, NULL}
949 status = libnetapi_init(&c->netapi_ctx);
950 if (status != 0) {
951 return -1;
953 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
954 libnetapi_set_password(c->netapi_ctx, c->opt_password);
955 if (c->opt_kerberos) {
956 libnetapi_set_use_kerberos(c->netapi_ctx);
959 if (argc == 0) {
960 if (c->display_usage) {
961 d_printf("Usage:\n");
962 d_printf("net rpc user\n"
963 " List all users\n");
964 net_display_usage_from_functable(func);
965 return 0;
968 return rpc_user_list(c, argc, argv);
971 return net_run_function(c, argc, argv, "net rpc user", func);
974 static NTSTATUS rpc_sh_user_list(struct net_context *c,
975 TALLOC_CTX *mem_ctx,
976 struct rpc_sh_ctx *ctx,
977 struct rpc_pipe_client *pipe_hnd,
978 int argc, const char **argv)
980 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
983 static NTSTATUS rpc_sh_user_info(struct net_context *c,
984 TALLOC_CTX *mem_ctx,
985 struct rpc_sh_ctx *ctx,
986 struct rpc_pipe_client *pipe_hnd,
987 int argc, const char **argv)
989 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
992 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
993 TALLOC_CTX *mem_ctx,
994 struct rpc_sh_ctx *ctx,
995 struct rpc_pipe_client *pipe_hnd,
996 int argc, const char **argv,
997 NTSTATUS (*fn)(
998 struct net_context *c,
999 TALLOC_CTX *mem_ctx,
1000 struct rpc_sh_ctx *ctx,
1001 struct rpc_pipe_client *pipe_hnd,
1002 struct policy_handle *user_hnd,
1003 int argc, const char **argv))
1005 struct policy_handle connect_pol, domain_pol, user_pol;
1006 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1007 DOM_SID sid;
1008 uint32 rid;
1009 enum lsa_SidType type;
1011 if (argc == 0) {
1012 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1013 return NT_STATUS_INVALID_PARAMETER;
1016 ZERO_STRUCT(connect_pol);
1017 ZERO_STRUCT(domain_pol);
1018 ZERO_STRUCT(user_pol);
1020 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1021 argv[0], NULL, NULL, &sid, &type);
1022 if (!NT_STATUS_IS_OK(result)) {
1023 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1024 nt_errstr(result));
1025 goto done;
1028 if (type != SID_NAME_USER) {
1029 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1030 sid_type_lookup(type));
1031 result = NT_STATUS_NO_SUCH_USER;
1032 goto done;
1035 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1036 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1037 result = NT_STATUS_NO_SUCH_USER;
1038 goto done;
1041 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1042 pipe_hnd->desthost,
1043 MAXIMUM_ALLOWED_ACCESS,
1044 &connect_pol);
1045 if (!NT_STATUS_IS_OK(result)) {
1046 goto done;
1049 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1050 &connect_pol,
1051 MAXIMUM_ALLOWED_ACCESS,
1052 ctx->domain_sid,
1053 &domain_pol);
1054 if (!NT_STATUS_IS_OK(result)) {
1055 goto done;
1058 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1059 &domain_pol,
1060 MAXIMUM_ALLOWED_ACCESS,
1061 rid,
1062 &user_pol);
1063 if (!NT_STATUS_IS_OK(result)) {
1064 goto done;
1067 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1069 done:
1070 if (is_valid_policy_hnd(&user_pol)) {
1071 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1073 if (is_valid_policy_hnd(&domain_pol)) {
1074 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1076 if (is_valid_policy_hnd(&connect_pol)) {
1077 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1079 return result;
1082 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1083 TALLOC_CTX *mem_ctx,
1084 struct rpc_sh_ctx *ctx,
1085 struct rpc_pipe_client *pipe_hnd,
1086 struct policy_handle *user_hnd,
1087 int argc, const char **argv)
1089 NTSTATUS result;
1090 union samr_UserInfo *info = NULL;
1092 if (argc != 0) {
1093 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1094 return NT_STATUS_INVALID_PARAMETER;
1097 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1098 user_hnd,
1100 &info);
1101 if (!NT_STATUS_IS_OK(result)) {
1102 return result;
1105 d_printf("user rid: %d, group rid: %d\n",
1106 info->info21.rid,
1107 info->info21.primary_gid);
1109 return result;
1112 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1113 TALLOC_CTX *mem_ctx,
1114 struct rpc_sh_ctx *ctx,
1115 struct rpc_pipe_client *pipe_hnd,
1116 int argc, const char **argv)
1118 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1119 rpc_sh_user_show_internals);
1122 #define FETCHSTR(name, rec) \
1123 do { if (strequal(ctx->thiscmd, name)) { \
1124 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1125 } while (0);
1127 #define SETSTR(name, rec, flag) \
1128 do { if (strequal(ctx->thiscmd, name)) { \
1129 init_lsa_String(&(info->info21.rec), argv[0]); \
1130 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1131 } while (0);
1133 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1134 TALLOC_CTX *mem_ctx,
1135 struct rpc_sh_ctx *ctx,
1136 struct rpc_pipe_client *pipe_hnd,
1137 struct policy_handle *user_hnd,
1138 int argc, const char **argv)
1140 NTSTATUS result;
1141 const char *username;
1142 const char *oldval = "";
1143 union samr_UserInfo *info = NULL;
1145 if (argc > 1) {
1146 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1147 ctx->whoami);
1148 return NT_STATUS_INVALID_PARAMETER;
1151 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1152 user_hnd,
1154 &info);
1155 if (!NT_STATUS_IS_OK(result)) {
1156 return result;
1159 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1161 FETCHSTR("fullname", full_name);
1162 FETCHSTR("homedir", home_directory);
1163 FETCHSTR("homedrive", home_drive);
1164 FETCHSTR("logonscript", logon_script);
1165 FETCHSTR("profilepath", profile_path);
1166 FETCHSTR("description", description);
1168 if (argc == 0) {
1169 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1170 goto done;
1173 if (strcmp(argv[0], "NULL") == 0) {
1174 argv[0] = "";
1177 ZERO_STRUCT(info->info21);
1179 SETSTR("fullname", full_name, FULL_NAME);
1180 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1181 SETSTR("homedrive", home_drive, HOME_DRIVE);
1182 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1183 SETSTR("profilepath", profile_path, PROFILE_PATH);
1184 SETSTR("description", description, DESCRIPTION);
1186 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1187 user_hnd,
1189 info);
1191 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1192 ctx->thiscmd, oldval, argv[0]);
1194 done:
1196 return result;
1199 #define HANDLEFLG(name, rec) \
1200 do { if (strequal(ctx->thiscmd, name)) { \
1201 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1202 if (newval) { \
1203 newflags = oldflags | ACB_##rec; \
1204 } else { \
1205 newflags = oldflags & ~ACB_##rec; \
1206 } } } while (0);
1208 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1209 TALLOC_CTX *mem_ctx,
1210 struct rpc_sh_ctx *ctx,
1211 struct rpc_pipe_client *pipe_hnd,
1212 int argc, const char **argv)
1214 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1215 rpc_sh_user_str_edit_internals);
1218 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1219 TALLOC_CTX *mem_ctx,
1220 struct rpc_sh_ctx *ctx,
1221 struct rpc_pipe_client *pipe_hnd,
1222 struct policy_handle *user_hnd,
1223 int argc, const char **argv)
1225 NTSTATUS result;
1226 const char *username;
1227 const char *oldval = "unknown";
1228 uint32 oldflags, newflags;
1229 bool newval;
1230 union samr_UserInfo *info = NULL;
1232 if ((argc > 1) ||
1233 ((argc == 1) && !strequal(argv[0], "yes") &&
1234 !strequal(argv[0], "no"))) {
1235 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1236 ctx->whoami);
1237 return NT_STATUS_INVALID_PARAMETER;
1240 newval = strequal(argv[0], "yes");
1242 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1243 user_hnd,
1245 &info);
1246 if (!NT_STATUS_IS_OK(result)) {
1247 return result;
1250 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1251 oldflags = info->info21.acct_flags;
1252 newflags = info->info21.acct_flags;
1254 HANDLEFLG("disabled", DISABLED);
1255 HANDLEFLG("pwnotreq", PWNOTREQ);
1256 HANDLEFLG("autolock", AUTOLOCK);
1257 HANDLEFLG("pwnoexp", PWNOEXP);
1259 if (argc == 0) {
1260 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1261 goto done;
1264 ZERO_STRUCT(info->info21);
1266 info->info21.acct_flags = newflags;
1267 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1269 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1270 user_hnd,
1272 info);
1274 if (NT_STATUS_IS_OK(result)) {
1275 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1276 ctx->thiscmd, oldval, argv[0]);
1279 done:
1281 return result;
1284 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1285 TALLOC_CTX *mem_ctx,
1286 struct rpc_sh_ctx *ctx,
1287 struct rpc_pipe_client *pipe_hnd,
1288 int argc, const char **argv)
1290 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1291 rpc_sh_user_flag_edit_internals);
1294 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1295 TALLOC_CTX *mem_ctx,
1296 struct rpc_sh_ctx *ctx)
1298 static struct rpc_sh_cmd cmds[] = {
1300 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1301 "Show/Set a user's full name" },
1303 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1304 "Show/Set a user's home directory" },
1306 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1307 "Show/Set a user's home drive" },
1309 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1310 "Show/Set a user's logon script" },
1312 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1313 "Show/Set a user's profile path" },
1315 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1316 "Show/Set a user's description" },
1318 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1319 "Show/Set whether a user is disabled" },
1321 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1322 "Show/Set whether a user locked out" },
1324 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1325 "Show/Set whether a user does not need a password" },
1327 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1328 "Show/Set whether a user's password does not expire" },
1330 { NULL, NULL, 0, NULL, NULL }
1333 return cmds;
1336 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1337 TALLOC_CTX *mem_ctx,
1338 struct rpc_sh_ctx *ctx)
1340 static struct rpc_sh_cmd cmds[] = {
1342 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1343 "List available users" },
1345 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1346 "List the domain groups a user is member of" },
1348 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1349 "Show info about a user" },
1351 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1352 "Show/Modify a user's fields" },
1354 { NULL, NULL, 0, NULL, NULL }
1357 return cmds;
1360 /****************************************************************************/
1363 * Basic usage function for 'net rpc group'.
1364 * @param argc Standard main() style argc.
1365 * @param argv Standard main() style argv. Initial components are already
1366 * stripped.
1369 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1371 return net_group_usage(c, argc, argv);
1375 * Delete group on a remote RPC server.
1377 * All parameters are provided by the run_rpc_command function, except for
1378 * argc, argv which are passed through.
1380 * @param domain_sid The domain sid acquired from the remote server.
1381 * @param cli A cli_state connected to the server.
1382 * @param mem_ctx Talloc context, destroyed on completion of the function.
1383 * @param argc Standard main() style argc.
1384 * @param argv Standard main() style argv. Initial components are already
1385 * stripped.
1387 * @return Normal NTSTATUS return.
1390 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1391 const DOM_SID *domain_sid,
1392 const char *domain_name,
1393 struct cli_state *cli,
1394 struct rpc_pipe_client *pipe_hnd,
1395 TALLOC_CTX *mem_ctx,
1396 int argc,
1397 const char **argv)
1399 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1400 bool group_is_primary = false;
1401 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1402 uint32_t group_rid;
1403 struct samr_RidTypeArray *rids = NULL;
1404 /* char **names; */
1405 int i;
1406 /* struct samr_RidWithAttribute *user_gids; */
1408 struct samr_Ids group_rids, name_types;
1409 struct lsa_String lsa_acct_name;
1410 union samr_UserInfo *info = NULL;
1412 if (argc < 1 || c->display_usage) {
1413 rpc_group_usage(c, argc,argv);
1414 return NT_STATUS_OK; /* ok? */
1417 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1418 pipe_hnd->desthost,
1419 MAXIMUM_ALLOWED_ACCESS,
1420 &connect_pol);
1422 if (!NT_STATUS_IS_OK(result)) {
1423 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1424 goto done;
1427 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1428 &connect_pol,
1429 MAXIMUM_ALLOWED_ACCESS,
1430 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1431 &domain_pol);
1433 if (!NT_STATUS_IS_OK(result)) {
1434 d_fprintf(stderr, "Request open_domain failed\n");
1435 goto done;
1438 init_lsa_String(&lsa_acct_name, argv[0]);
1440 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1441 &domain_pol,
1443 &lsa_acct_name,
1444 &group_rids,
1445 &name_types);
1446 if (!NT_STATUS_IS_OK(result)) {
1447 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1448 goto done;
1451 switch (name_types.ids[0])
1453 case SID_NAME_DOM_GRP:
1454 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1455 &domain_pol,
1456 MAXIMUM_ALLOWED_ACCESS,
1457 group_rids.ids[0],
1458 &group_pol);
1459 if (!NT_STATUS_IS_OK(result)) {
1460 d_fprintf(stderr, "Request open_group failed");
1461 goto done;
1464 group_rid = group_rids.ids[0];
1466 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1467 &group_pol,
1468 &rids);
1470 if (!NT_STATUS_IS_OK(result)) {
1471 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1472 goto done;
1475 if (c->opt_verbose) {
1476 d_printf("Domain Group %s (rid: %d) has %d members\n",
1477 argv[0],group_rid, rids->count);
1480 /* Check if group is anyone's primary group */
1481 for (i = 0; i < rids->count; i++)
1483 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1484 &domain_pol,
1485 MAXIMUM_ALLOWED_ACCESS,
1486 rids->rids[i],
1487 &user_pol);
1489 if (!NT_STATUS_IS_OK(result)) {
1490 d_fprintf(stderr, "Unable to open group member %d\n",
1491 rids->rids[i]);
1492 goto done;
1495 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1496 &user_pol,
1498 &info);
1500 if (!NT_STATUS_IS_OK(result)) {
1501 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1502 rids->rids[i]);
1503 goto done;
1506 if (info->info21.primary_gid == group_rid) {
1507 if (c->opt_verbose) {
1508 d_printf("Group is primary group of %s\n",
1509 info->info21.account_name.string);
1511 group_is_primary = true;
1514 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1517 if (group_is_primary) {
1518 d_fprintf(stderr, "Unable to delete group because some "
1519 "of it's members have it as primary group\n");
1520 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1521 goto done;
1524 /* remove all group members */
1525 for (i = 0; i < rids->count; i++)
1527 if (c->opt_verbose)
1528 d_printf("Remove group member %d...",
1529 rids->rids[i]);
1530 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1531 &group_pol,
1532 rids->rids[i]);
1534 if (NT_STATUS_IS_OK(result)) {
1535 if (c->opt_verbose)
1536 d_printf("ok\n");
1537 } else {
1538 if (c->opt_verbose)
1539 d_printf("failed\n");
1540 goto done;
1544 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1545 &group_pol);
1547 break;
1548 /* removing a local group is easier... */
1549 case SID_NAME_ALIAS:
1550 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1551 &domain_pol,
1552 MAXIMUM_ALLOWED_ACCESS,
1553 group_rids.ids[0],
1554 &group_pol);
1556 if (!NT_STATUS_IS_OK(result)) {
1557 d_fprintf(stderr, "Request open_alias failed\n");
1558 goto done;
1561 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1562 &group_pol);
1563 break;
1564 default:
1565 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1566 argv[0],sid_type_lookup(name_types.ids[0]));
1567 result = NT_STATUS_UNSUCCESSFUL;
1568 goto done;
1571 if (NT_STATUS_IS_OK(result)) {
1572 if (c->opt_verbose)
1573 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1574 } else {
1575 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1576 get_friendly_nt_error_msg(result));
1579 done:
1580 return result;
1584 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1586 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1587 rpc_group_delete_internals, argc,argv);
1590 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1592 NET_API_STATUS status;
1593 struct GROUP_INFO_1 info1;
1594 uint32_t parm_error = 0;
1596 if (argc != 1 || c->display_usage) {
1597 rpc_group_usage(c, argc, argv);
1598 return 0;
1601 ZERO_STRUCT(info1);
1603 info1.grpi1_name = argv[0];
1604 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1605 info1.grpi1_comment = c->opt_comment;
1608 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1610 if (status != 0) {
1611 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1612 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1613 status));
1614 return -1;
1615 } else {
1616 d_printf("Added group '%s'.\n", argv[0]);
1619 return 0;
1622 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1624 NET_API_STATUS status;
1625 struct LOCALGROUP_INFO_1 info1;
1626 uint32_t parm_error = 0;
1628 if (argc != 1 || c->display_usage) {
1629 rpc_group_usage(c, argc, argv);
1630 return 0;
1633 ZERO_STRUCT(info1);
1635 info1.lgrpi1_name = argv[0];
1636 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1637 info1.lgrpi1_comment = c->opt_comment;
1640 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1642 if (status != 0) {
1643 d_fprintf(stderr, "Failed to add alias '%s' with: %s.\n",
1644 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1645 status));
1646 return -1;
1647 } else {
1648 d_printf("Added alias '%s'.\n", argv[0]);
1651 return 0;
1654 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1656 if (c->opt_localgroup)
1657 return rpc_alias_add_internals(c, argc, argv);
1659 return rpc_group_add_internals(c, argc, argv);
1662 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1663 TALLOC_CTX *mem_ctx,
1664 const char *name,
1665 DOM_SID *sid,
1666 enum lsa_SidType *type)
1668 DOM_SID *sids = NULL;
1669 enum lsa_SidType *types = NULL;
1670 struct rpc_pipe_client *pipe_hnd = NULL;
1671 struct policy_handle lsa_pol;
1672 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1674 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1675 &pipe_hnd);
1676 if (!NT_STATUS_IS_OK(result)) {
1677 goto done;
1680 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1681 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1683 if (!NT_STATUS_IS_OK(result)) {
1684 goto done;
1687 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1688 &name, NULL, 1, &sids, &types);
1690 if (NT_STATUS_IS_OK(result)) {
1691 sid_copy(sid, &sids[0]);
1692 *type = types[0];
1695 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1697 done:
1698 if (pipe_hnd) {
1699 TALLOC_FREE(pipe_hnd);
1702 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1704 /* Try as S-1-5-whatever */
1706 DOM_SID tmp_sid;
1708 if (string_to_sid(&tmp_sid, name)) {
1709 sid_copy(sid, &tmp_sid);
1710 *type = SID_NAME_UNKNOWN;
1711 result = NT_STATUS_OK;
1715 return result;
1718 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1719 TALLOC_CTX *mem_ctx,
1720 const DOM_SID *group_sid,
1721 const char *member)
1723 struct policy_handle connect_pol, domain_pol;
1724 NTSTATUS result;
1725 uint32 group_rid;
1726 struct policy_handle group_pol;
1728 struct samr_Ids rids, rid_types;
1729 struct lsa_String lsa_acct_name;
1731 DOM_SID sid;
1733 sid_copy(&sid, group_sid);
1735 if (!sid_split_rid(&sid, &group_rid)) {
1736 return NT_STATUS_UNSUCCESSFUL;
1739 /* Get sam policy handle */
1740 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1741 pipe_hnd->desthost,
1742 MAXIMUM_ALLOWED_ACCESS,
1743 &connect_pol);
1744 if (!NT_STATUS_IS_OK(result)) {
1745 return result;
1748 /* Get domain policy handle */
1749 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1750 &connect_pol,
1751 MAXIMUM_ALLOWED_ACCESS,
1752 &sid,
1753 &domain_pol);
1754 if (!NT_STATUS_IS_OK(result)) {
1755 return result;
1758 init_lsa_String(&lsa_acct_name, member);
1760 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1761 &domain_pol,
1763 &lsa_acct_name,
1764 &rids,
1765 &rid_types);
1767 if (!NT_STATUS_IS_OK(result)) {
1768 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1769 goto done;
1772 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1773 &domain_pol,
1774 MAXIMUM_ALLOWED_ACCESS,
1775 group_rid,
1776 &group_pol);
1778 if (!NT_STATUS_IS_OK(result)) {
1779 goto done;
1782 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1783 &group_pol,
1784 rids.ids[0],
1785 0x0005); /* unknown flags */
1787 done:
1788 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1789 return result;
1792 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1793 TALLOC_CTX *mem_ctx,
1794 const DOM_SID *alias_sid,
1795 const char *member)
1797 struct policy_handle connect_pol, domain_pol;
1798 NTSTATUS result;
1799 uint32 alias_rid;
1800 struct policy_handle alias_pol;
1802 DOM_SID member_sid;
1803 enum lsa_SidType member_type;
1805 DOM_SID sid;
1807 sid_copy(&sid, alias_sid);
1809 if (!sid_split_rid(&sid, &alias_rid)) {
1810 return NT_STATUS_UNSUCCESSFUL;
1813 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1814 member, &member_sid, &member_type);
1816 if (!NT_STATUS_IS_OK(result)) {
1817 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1818 return result;
1821 /* Get sam policy handle */
1822 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1823 pipe_hnd->desthost,
1824 MAXIMUM_ALLOWED_ACCESS,
1825 &connect_pol);
1826 if (!NT_STATUS_IS_OK(result)) {
1827 goto done;
1830 /* Get domain policy handle */
1831 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1832 &connect_pol,
1833 MAXIMUM_ALLOWED_ACCESS,
1834 &sid,
1835 &domain_pol);
1836 if (!NT_STATUS_IS_OK(result)) {
1837 goto done;
1840 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1841 &domain_pol,
1842 MAXIMUM_ALLOWED_ACCESS,
1843 alias_rid,
1844 &alias_pol);
1846 if (!NT_STATUS_IS_OK(result)) {
1847 return result;
1850 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1851 &alias_pol,
1852 &member_sid);
1854 if (!NT_STATUS_IS_OK(result)) {
1855 return result;
1858 done:
1859 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1860 return result;
1863 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1864 const DOM_SID *domain_sid,
1865 const char *domain_name,
1866 struct cli_state *cli,
1867 struct rpc_pipe_client *pipe_hnd,
1868 TALLOC_CTX *mem_ctx,
1869 int argc,
1870 const char **argv)
1872 DOM_SID group_sid;
1873 enum lsa_SidType group_type;
1875 if (argc != 2 || c->display_usage) {
1876 d_printf("Usage:\n"
1877 "net rpc group addmem <group> <member>\n"
1878 " Add a member to a group\n"
1879 " group\tGroup to add member to\n"
1880 " member\tMember to add to group\n");
1881 return NT_STATUS_UNSUCCESSFUL;
1884 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1885 &group_sid, &group_type))) {
1886 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
1887 return NT_STATUS_UNSUCCESSFUL;
1890 if (group_type == SID_NAME_DOM_GRP) {
1891 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1892 &group_sid, argv[1]);
1894 if (!NT_STATUS_IS_OK(result)) {
1895 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1896 argv[1], argv[0], nt_errstr(result));
1898 return result;
1901 if (group_type == SID_NAME_ALIAS) {
1902 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1903 &group_sid, argv[1]);
1905 if (!NT_STATUS_IS_OK(result)) {
1906 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1907 argv[1], argv[0], nt_errstr(result));
1909 return result;
1912 d_fprintf(stderr, "Can only add members to global or local groups "
1913 "which %s is not\n", argv[0]);
1915 return NT_STATUS_UNSUCCESSFUL;
1918 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1920 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1921 rpc_group_addmem_internals,
1922 argc, argv);
1925 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1926 struct rpc_pipe_client *pipe_hnd,
1927 TALLOC_CTX *mem_ctx,
1928 const DOM_SID *group_sid,
1929 const char *member)
1931 struct policy_handle connect_pol, domain_pol;
1932 NTSTATUS result;
1933 uint32 group_rid;
1934 struct policy_handle group_pol;
1936 struct samr_Ids rids, rid_types;
1937 struct lsa_String lsa_acct_name;
1939 DOM_SID sid;
1941 sid_copy(&sid, group_sid);
1943 if (!sid_split_rid(&sid, &group_rid))
1944 return NT_STATUS_UNSUCCESSFUL;
1946 /* Get sam policy handle */
1947 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1948 pipe_hnd->desthost,
1949 MAXIMUM_ALLOWED_ACCESS,
1950 &connect_pol);
1951 if (!NT_STATUS_IS_OK(result))
1952 return result;
1954 /* Get domain policy handle */
1955 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1956 &connect_pol,
1957 MAXIMUM_ALLOWED_ACCESS,
1958 &sid,
1959 &domain_pol);
1960 if (!NT_STATUS_IS_OK(result))
1961 return result;
1963 init_lsa_String(&lsa_acct_name, member);
1965 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1966 &domain_pol,
1968 &lsa_acct_name,
1969 &rids,
1970 &rid_types);
1971 if (!NT_STATUS_IS_OK(result)) {
1972 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1973 goto done;
1976 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1977 &domain_pol,
1978 MAXIMUM_ALLOWED_ACCESS,
1979 group_rid,
1980 &group_pol);
1982 if (!NT_STATUS_IS_OK(result))
1983 goto done;
1985 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1986 &group_pol,
1987 rids.ids[0]);
1989 done:
1990 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1991 return result;
1994 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
1995 TALLOC_CTX *mem_ctx,
1996 const DOM_SID *alias_sid,
1997 const char *member)
1999 struct policy_handle connect_pol, domain_pol;
2000 NTSTATUS result;
2001 uint32 alias_rid;
2002 struct policy_handle alias_pol;
2004 DOM_SID member_sid;
2005 enum lsa_SidType member_type;
2007 DOM_SID sid;
2009 sid_copy(&sid, alias_sid);
2011 if (!sid_split_rid(&sid, &alias_rid))
2012 return NT_STATUS_UNSUCCESSFUL;
2014 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2015 member, &member_sid, &member_type);
2017 if (!NT_STATUS_IS_OK(result)) {
2018 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2019 return result;
2022 /* Get sam policy handle */
2023 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2024 pipe_hnd->desthost,
2025 MAXIMUM_ALLOWED_ACCESS,
2026 &connect_pol);
2027 if (!NT_STATUS_IS_OK(result)) {
2028 goto done;
2031 /* Get domain policy handle */
2032 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2033 &connect_pol,
2034 MAXIMUM_ALLOWED_ACCESS,
2035 &sid,
2036 &domain_pol);
2037 if (!NT_STATUS_IS_OK(result)) {
2038 goto done;
2041 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2042 &domain_pol,
2043 MAXIMUM_ALLOWED_ACCESS,
2044 alias_rid,
2045 &alias_pol);
2047 if (!NT_STATUS_IS_OK(result))
2048 return result;
2050 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2051 &alias_pol,
2052 &member_sid);
2054 if (!NT_STATUS_IS_OK(result))
2055 return result;
2057 done:
2058 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2059 return result;
2062 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2063 const DOM_SID *domain_sid,
2064 const char *domain_name,
2065 struct cli_state *cli,
2066 struct rpc_pipe_client *pipe_hnd,
2067 TALLOC_CTX *mem_ctx,
2068 int argc,
2069 const char **argv)
2071 DOM_SID group_sid;
2072 enum lsa_SidType group_type;
2074 if (argc != 2 || c->display_usage) {
2075 d_printf("Usage:\n"
2076 "net rpc group delmem <group> <member>\n"
2077 " Delete a member from a group\n"
2078 " group\tGroup to delete member from\n"
2079 " member\tMember to delete from group\n");
2080 return NT_STATUS_UNSUCCESSFUL;
2083 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2084 &group_sid, &group_type))) {
2085 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2086 return NT_STATUS_UNSUCCESSFUL;
2089 if (group_type == SID_NAME_DOM_GRP) {
2090 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2091 &group_sid, argv[1]);
2093 if (!NT_STATUS_IS_OK(result)) {
2094 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2095 argv[1], argv[0], nt_errstr(result));
2097 return result;
2100 if (group_type == SID_NAME_ALIAS) {
2101 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2102 &group_sid, argv[1]);
2104 if (!NT_STATUS_IS_OK(result)) {
2105 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2106 argv[1], argv[0], nt_errstr(result));
2108 return result;
2111 d_fprintf(stderr, "Can only delete members from global or local groups "
2112 "which %s is not\n", argv[0]);
2114 return NT_STATUS_UNSUCCESSFUL;
2117 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2119 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2120 rpc_group_delmem_internals,
2121 argc, argv);
2125 * List groups on a remote RPC server.
2127 * All parameters are provided by the run_rpc_command function, except for
2128 * argc, argv which are passes through.
2130 * @param domain_sid The domain sid acquired from the remote server.
2131 * @param cli A cli_state connected to the server.
2132 * @param mem_ctx Talloc context, destroyed on completion of the function.
2133 * @param argc Standard main() style argc.
2134 * @param argv Standard main() style argv. Initial components are already
2135 * stripped.
2137 * @return Normal NTSTATUS return.
2140 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2141 const DOM_SID *domain_sid,
2142 const char *domain_name,
2143 struct cli_state *cli,
2144 struct rpc_pipe_client *pipe_hnd,
2145 TALLOC_CTX *mem_ctx,
2146 int argc,
2147 const char **argv)
2149 struct policy_handle connect_pol, domain_pol;
2150 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2151 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2152 struct samr_SamArray *groups = NULL;
2153 bool global = false;
2154 bool local = false;
2155 bool builtin = false;
2157 if (c->display_usage) {
2158 d_printf("Usage:\n"
2159 "net rpc group list [global] [local] [builtin]\n"
2160 " List groups on RPC server\n"
2161 " global\tList global groups\n"
2162 " local\tList local groups\n"
2163 " builtin\tList builtin groups\n"
2164 " If none of global, local or builtin is "
2165 "specified, all three options are considered set\n");
2166 return NT_STATUS_OK;
2169 if (argc == 0) {
2170 global = true;
2171 local = true;
2172 builtin = true;
2175 for (i=0; i<argc; i++) {
2176 if (strequal(argv[i], "global"))
2177 global = true;
2179 if (strequal(argv[i], "local"))
2180 local = true;
2182 if (strequal(argv[i], "builtin"))
2183 builtin = true;
2186 /* Get sam policy handle */
2188 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2189 pipe_hnd->desthost,
2190 MAXIMUM_ALLOWED_ACCESS,
2191 &connect_pol);
2192 if (!NT_STATUS_IS_OK(result)) {
2193 goto done;
2196 /* Get domain policy handle */
2198 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2199 &connect_pol,
2200 MAXIMUM_ALLOWED_ACCESS,
2201 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2202 &domain_pol);
2203 if (!NT_STATUS_IS_OK(result)) {
2204 goto done;
2207 /* Query domain groups */
2208 if (c->opt_long_list_entries)
2209 d_printf("\nGroup name Comment"
2210 "\n-----------------------------\n");
2211 do {
2212 uint32_t max_size, total_size, returned_size;
2213 union samr_DispInfo info;
2215 if (!global) break;
2217 get_query_dispinfo_params(
2218 loop_count, &max_entries, &max_size);
2220 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2221 &domain_pol,
2223 start_idx,
2224 max_entries,
2225 max_size,
2226 &total_size,
2227 &returned_size,
2228 &info);
2229 num_entries = info.info3.count;
2230 start_idx += info.info3.count;
2232 if (!NT_STATUS_IS_OK(result) &&
2233 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2234 break;
2236 for (i = 0; i < num_entries; i++) {
2238 const char *group = NULL;
2239 const char *desc = NULL;
2241 group = info.info3.entries[i].account_name.string;
2242 desc = info.info3.entries[i].description.string;
2244 if (c->opt_long_list_entries)
2245 printf("%-21.21s %-50.50s\n",
2246 group, desc);
2247 else
2248 printf("%s\n", group);
2250 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2251 /* query domain aliases */
2252 start_idx = 0;
2253 do {
2254 if (!local) break;
2256 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2257 &domain_pol,
2258 &start_idx,
2259 &groups,
2260 0xffff,
2261 &num_entries);
2262 if (!NT_STATUS_IS_OK(result) &&
2263 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2264 break;
2266 for (i = 0; i < num_entries; i++) {
2268 const char *description = NULL;
2270 if (c->opt_long_list_entries) {
2272 struct policy_handle alias_pol;
2273 union samr_AliasInfo *info = NULL;
2275 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2276 &domain_pol,
2277 0x8,
2278 groups->entries[i].idx,
2279 &alias_pol))) &&
2280 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2281 &alias_pol,
2283 &info))) &&
2284 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2285 &alias_pol)))) {
2286 description = info->description.string;
2290 if (description != NULL) {
2291 printf("%-21.21s %-50.50s\n",
2292 groups->entries[i].name.string,
2293 description);
2294 } else {
2295 printf("%s\n", groups->entries[i].name.string);
2298 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2299 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2300 /* Get builtin policy handle */
2302 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2303 &connect_pol,
2304 MAXIMUM_ALLOWED_ACCESS,
2305 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2306 &domain_pol);
2307 if (!NT_STATUS_IS_OK(result)) {
2308 goto done;
2310 /* query builtin aliases */
2311 start_idx = 0;
2312 do {
2313 if (!builtin) break;
2315 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2316 &domain_pol,
2317 &start_idx,
2318 &groups,
2319 max_entries,
2320 &num_entries);
2321 if (!NT_STATUS_IS_OK(result) &&
2322 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2323 break;
2325 for (i = 0; i < num_entries; i++) {
2327 const char *description = NULL;
2329 if (c->opt_long_list_entries) {
2331 struct policy_handle alias_pol;
2332 union samr_AliasInfo *info = NULL;
2334 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2335 &domain_pol,
2336 0x8,
2337 groups->entries[i].idx,
2338 &alias_pol))) &&
2339 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2340 &alias_pol,
2342 &info))) &&
2343 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2344 &alias_pol)))) {
2345 description = info->description.string;
2349 if (description != NULL) {
2350 printf("%-21.21s %-50.50s\n",
2351 groups->entries[i].name.string,
2352 description);
2353 } else {
2354 printf("%s\n", groups->entries[i].name.string);
2357 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2359 done:
2360 return result;
2363 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2365 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2366 rpc_group_list_internals,
2367 argc, argv);
2370 static NTSTATUS rpc_list_group_members(struct net_context *c,
2371 struct rpc_pipe_client *pipe_hnd,
2372 TALLOC_CTX *mem_ctx,
2373 const char *domain_name,
2374 const DOM_SID *domain_sid,
2375 struct policy_handle *domain_pol,
2376 uint32 rid)
2378 NTSTATUS result;
2379 struct policy_handle group_pol;
2380 uint32 num_members, *group_rids;
2381 int i;
2382 struct samr_RidTypeArray *rids = NULL;
2383 struct lsa_Strings names;
2384 struct samr_Ids types;
2386 fstring sid_str;
2387 sid_to_fstring(sid_str, domain_sid);
2389 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2390 domain_pol,
2391 MAXIMUM_ALLOWED_ACCESS,
2392 rid,
2393 &group_pol);
2395 if (!NT_STATUS_IS_OK(result))
2396 return result;
2398 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2399 &group_pol,
2400 &rids);
2402 if (!NT_STATUS_IS_OK(result))
2403 return result;
2405 num_members = rids->count;
2406 group_rids = rids->rids;
2408 while (num_members > 0) {
2409 int this_time = 512;
2411 if (num_members < this_time)
2412 this_time = num_members;
2414 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2415 domain_pol,
2416 this_time,
2417 group_rids,
2418 &names,
2419 &types);
2421 if (!NT_STATUS_IS_OK(result))
2422 return result;
2424 /* We only have users as members, but make the output
2425 the same as the output of alias members */
2427 for (i = 0; i < this_time; i++) {
2429 if (c->opt_long_list_entries) {
2430 printf("%s-%d %s\\%s %d\n", sid_str,
2431 group_rids[i], domain_name,
2432 names.names[i].string,
2433 SID_NAME_USER);
2434 } else {
2435 printf("%s\\%s\n", domain_name,
2436 names.names[i].string);
2440 num_members -= this_time;
2441 group_rids += 512;
2444 return NT_STATUS_OK;
2447 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2448 struct rpc_pipe_client *pipe_hnd,
2449 TALLOC_CTX *mem_ctx,
2450 struct policy_handle *domain_pol,
2451 uint32 rid)
2453 NTSTATUS result;
2454 struct rpc_pipe_client *lsa_pipe;
2455 struct policy_handle alias_pol, lsa_pol;
2456 uint32 num_members;
2457 DOM_SID *alias_sids;
2458 char **domains;
2459 char **names;
2460 enum lsa_SidType *types;
2461 int i;
2462 struct lsa_SidArray sid_array;
2464 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2465 domain_pol,
2466 MAXIMUM_ALLOWED_ACCESS,
2467 rid,
2468 &alias_pol);
2470 if (!NT_STATUS_IS_OK(result))
2471 return result;
2473 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2474 &alias_pol,
2475 &sid_array);
2477 if (!NT_STATUS_IS_OK(result)) {
2478 d_fprintf(stderr, "Couldn't list alias members\n");
2479 return result;
2482 num_members = sid_array.num_sids;
2484 if (num_members == 0) {
2485 return NT_STATUS_OK;
2488 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2489 &ndr_table_lsarpc.syntax_id,
2490 &lsa_pipe);
2491 if (!NT_STATUS_IS_OK(result)) {
2492 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2493 nt_errstr(result) );
2494 return result;
2497 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2498 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2500 if (!NT_STATUS_IS_OK(result)) {
2501 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2502 TALLOC_FREE(lsa_pipe);
2503 return result;
2506 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2507 if (!alias_sids) {
2508 d_fprintf(stderr, "Out of memory\n");
2509 TALLOC_FREE(lsa_pipe);
2510 return NT_STATUS_NO_MEMORY;
2513 for (i=0; i<num_members; i++) {
2514 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2517 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2518 num_members, alias_sids,
2519 &domains, &names, &types);
2521 if (!NT_STATUS_IS_OK(result) &&
2522 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2523 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2524 TALLOC_FREE(lsa_pipe);
2525 return result;
2528 for (i = 0; i < num_members; i++) {
2529 fstring sid_str;
2530 sid_to_fstring(sid_str, &alias_sids[i]);
2532 if (c->opt_long_list_entries) {
2533 printf("%s %s\\%s %d\n", sid_str,
2534 domains[i] ? domains[i] : "*unknown*",
2535 names[i] ? names[i] : "*unknown*", types[i]);
2536 } else {
2537 if (domains[i])
2538 printf("%s\\%s\n", domains[i], names[i]);
2539 else
2540 printf("%s\n", sid_str);
2544 TALLOC_FREE(lsa_pipe);
2545 return NT_STATUS_OK;
2548 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2549 const DOM_SID *domain_sid,
2550 const char *domain_name,
2551 struct cli_state *cli,
2552 struct rpc_pipe_client *pipe_hnd,
2553 TALLOC_CTX *mem_ctx,
2554 int argc,
2555 const char **argv)
2557 NTSTATUS result;
2558 struct policy_handle connect_pol, domain_pol;
2559 struct samr_Ids rids, rid_types;
2560 struct lsa_String lsa_acct_name;
2562 /* Get sam policy handle */
2564 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2565 pipe_hnd->desthost,
2566 MAXIMUM_ALLOWED_ACCESS,
2567 &connect_pol);
2569 if (!NT_STATUS_IS_OK(result))
2570 return result;
2572 /* Get domain policy handle */
2574 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2575 &connect_pol,
2576 MAXIMUM_ALLOWED_ACCESS,
2577 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2578 &domain_pol);
2580 if (!NT_STATUS_IS_OK(result))
2581 return result;
2583 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2585 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2586 &domain_pol,
2588 &lsa_acct_name,
2589 &rids,
2590 &rid_types);
2592 if (!NT_STATUS_IS_OK(result)) {
2594 /* Ok, did not find it in the global sam, try with builtin */
2596 DOM_SID sid_Builtin;
2598 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2600 sid_copy(&sid_Builtin, &global_sid_Builtin);
2602 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2603 &connect_pol,
2604 MAXIMUM_ALLOWED_ACCESS,
2605 &sid_Builtin,
2606 &domain_pol);
2608 if (!NT_STATUS_IS_OK(result)) {
2609 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2610 return result;
2613 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2614 &domain_pol,
2616 &lsa_acct_name,
2617 &rids,
2618 &rid_types);
2620 if (!NT_STATUS_IS_OK(result)) {
2621 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2622 return result;
2626 if (rids.count != 1) {
2627 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2628 return result;
2631 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2632 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2633 domain_sid, &domain_pol,
2634 rids.ids[0]);
2637 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2638 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2639 rids.ids[0]);
2642 return NT_STATUS_NO_SUCH_GROUP;
2645 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2647 if (argc != 1 || c->display_usage) {
2648 return rpc_group_usage(c, argc, argv);
2651 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2652 rpc_group_members_internals,
2653 argc, argv);
2656 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2658 NET_API_STATUS status;
2659 struct GROUP_INFO_0 g0;
2660 uint32_t parm_err;
2662 if (argc != 2) {
2663 d_printf("Usage: 'net rpc group rename group newname'\n");
2664 return -1;
2667 g0.grpi0_name = argv[1];
2669 status = NetGroupSetInfo(c->opt_host,
2670 argv[0],
2672 (uint8_t *)&g0,
2673 &parm_err);
2675 if (status != 0) {
2676 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2677 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2678 status));
2679 return -1;
2682 return 0;
2685 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2687 if (argc != 2 || c->display_usage) {
2688 return rpc_group_usage(c, argc, argv);
2691 return rpc_group_rename_internals(c, argc, argv);
2695 * 'net rpc group' entrypoint.
2696 * @param argc Standard main() style argc.
2697 * @param argv Standard main() style argv. Initial components are already
2698 * stripped.
2701 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2703 NET_API_STATUS status;
2705 struct functable func[] = {
2707 "add",
2708 rpc_group_add,
2709 NET_TRANSPORT_RPC,
2710 "Create specified group",
2711 "net rpc group add\n"
2712 " Create specified group"
2715 "delete",
2716 rpc_group_delete,
2717 NET_TRANSPORT_RPC,
2718 "Delete specified group",
2719 "net rpc group delete\n"
2720 " Delete specified group"
2723 "addmem",
2724 rpc_group_addmem,
2725 NET_TRANSPORT_RPC,
2726 "Add member to group",
2727 "net rpc group addmem\n"
2728 " Add member to group"
2731 "delmem",
2732 rpc_group_delmem,
2733 NET_TRANSPORT_RPC,
2734 "Remove member from group",
2735 "net rpc group delmem\n"
2736 " Remove member from group"
2739 "list",
2740 rpc_group_list,
2741 NET_TRANSPORT_RPC,
2742 "List groups",
2743 "net rpc group list\n"
2744 " List groups"
2747 "members",
2748 rpc_group_members,
2749 NET_TRANSPORT_RPC,
2750 "List group members",
2751 "net rpc group members\n"
2752 " List group members"
2755 "rename",
2756 rpc_group_rename,
2757 NET_TRANSPORT_RPC,
2758 "Rename group",
2759 "net rpc group rename\n"
2760 " Rename group"
2762 {NULL, NULL, 0, NULL, NULL}
2765 status = libnetapi_init(&c->netapi_ctx);
2766 if (status != 0) {
2767 return -1;
2769 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2770 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2771 if (c->opt_kerberos) {
2772 libnetapi_set_use_kerberos(c->netapi_ctx);
2775 if (argc == 0) {
2776 if (c->display_usage) {
2777 d_printf("Usage:\n");
2778 d_printf("net rpc group\n"
2779 " Alias for net rpc group list global local "
2780 "builtin\n");
2781 net_display_usage_from_functable(func);
2782 return 0;
2785 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2786 rpc_group_list_internals,
2787 argc, argv);
2790 return net_run_function(c, argc, argv, "net rpc group", func);
2793 /****************************************************************************/
2795 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2797 return net_share_usage(c, argc, argv);
2801 * Add a share on a remote RPC server.
2803 * @param argc Standard main() style argc.
2804 * @param argv Standard main() style argv. Initial components are already
2805 * stripped.
2807 * @return A shell status integer (0 for success).
2810 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2812 NET_API_STATUS status;
2813 char *sharename;
2814 char *path;
2815 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2816 uint32 num_users=0, perms=0;
2817 char *password=NULL; /* don't allow a share password */
2818 struct SHARE_INFO_2 i2;
2819 uint32_t parm_error = 0;
2821 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2822 return rpc_share_usage(c, argc, argv);
2825 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2826 return -1;
2829 path = strchr(sharename, '=');
2830 if (!path) {
2831 return -1;
2834 *path++ = '\0';
2836 i2.shi2_netname = sharename;
2837 i2.shi2_type = type;
2838 i2.shi2_remark = c->opt_comment;
2839 i2.shi2_permissions = perms;
2840 i2.shi2_max_uses = c->opt_maxusers;
2841 i2.shi2_current_uses = num_users;
2842 i2.shi2_path = path;
2843 i2.shi2_passwd = password;
2845 status = NetShareAdd(c->opt_host,
2847 (uint8_t *)&i2,
2848 &parm_error);
2849 if (status != 0) {
2850 printf("NetShareAdd failed with: %s\n",
2851 libnetapi_get_error_string(c->netapi_ctx, status));
2854 return status;
2858 * Delete a share on a remote RPC server.
2860 * @param domain_sid The domain sid acquired from the remote server.
2861 * @param argc Standard main() style argc.
2862 * @param argv Standard main() style argv. Initial components are already
2863 * stripped.
2865 * @return A shell status integer (0 for success).
2867 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2869 if (argc < 1 || c->display_usage) {
2870 return rpc_share_usage(c, argc, argv);
2873 return NetShareDel(c->opt_host, argv[0], 0);
2877 * Formatted print of share info
2879 * @param r pointer to SHARE_INFO_1 to format
2882 static void display_share_info_1(struct net_context *c,
2883 struct SHARE_INFO_1 *r)
2885 if (c->opt_long_list_entries) {
2886 d_printf("%-12s %-8.8s %-50s\n",
2887 r->shi1_netname,
2888 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2889 r->shi1_remark);
2890 } else {
2891 d_printf("%s\n", r->shi1_netname);
2895 static WERROR get_share_info(struct net_context *c,
2896 struct rpc_pipe_client *pipe_hnd,
2897 TALLOC_CTX *mem_ctx,
2898 uint32 level,
2899 int argc,
2900 const char **argv,
2901 struct srvsvc_NetShareInfoCtr *info_ctr)
2903 WERROR result;
2904 NTSTATUS status;
2905 union srvsvc_NetShareInfo info;
2907 /* no specific share requested, enumerate all */
2908 if (argc == 0) {
2910 uint32_t preferred_len = 0xffffffff;
2911 uint32_t total_entries = 0;
2912 uint32_t resume_handle = 0;
2914 info_ctr->level = level;
2916 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2917 pipe_hnd->desthost,
2918 info_ctr,
2919 preferred_len,
2920 &total_entries,
2921 &resume_handle,
2922 &result);
2923 return result;
2926 /* request just one share */
2927 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2928 pipe_hnd->desthost,
2929 argv[0],
2930 level,
2931 &info,
2932 &result);
2934 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2935 goto done;
2938 /* construct ctr */
2939 ZERO_STRUCTP(info_ctr);
2941 info_ctr->level = level;
2943 switch (level) {
2944 case 1:
2946 struct srvsvc_NetShareCtr1 *ctr1;
2948 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2949 W_ERROR_HAVE_NO_MEMORY(ctr1);
2951 ctr1->count = 1;
2952 ctr1->array = info.info1;
2954 info_ctr->ctr.ctr1 = ctr1;
2956 case 2:
2958 struct srvsvc_NetShareCtr2 *ctr2;
2960 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2961 W_ERROR_HAVE_NO_MEMORY(ctr2);
2963 ctr2->count = 1;
2964 ctr2->array = info.info2;
2966 info_ctr->ctr.ctr2 = ctr2;
2968 case 502:
2970 struct srvsvc_NetShareCtr502 *ctr502;
2972 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
2973 W_ERROR_HAVE_NO_MEMORY(ctr502);
2975 ctr502->count = 1;
2976 ctr502->array = info.info502;
2978 info_ctr->ctr.ctr502 = ctr502;
2980 } /* switch */
2981 done:
2982 return result;
2985 /***
2986 * 'net rpc share list' entrypoint.
2987 * @param argc Standard main() style argc.
2988 * @param argv Standard main() style argv. Initial components are already
2989 * stripped.
2991 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
2993 NET_API_STATUS status;
2994 struct SHARE_INFO_1 *i1 = NULL;
2995 uint32_t entries_read = 0;
2996 uint32_t total_entries = 0;
2997 uint32_t resume_handle = 0;
2998 uint32_t i, level = 1;
3000 if (c->display_usage) {
3001 d_printf("Usage\n"
3002 "net rpc share list\n"
3003 " List shares on remote server\n");
3004 return 0;
3007 status = NetShareEnum(c->opt_host,
3008 level,
3009 (uint8_t **)(void *)&i1,
3010 (uint32_t)-1,
3011 &entries_read,
3012 &total_entries,
3013 &resume_handle);
3014 if (status != 0) {
3015 goto done;
3018 /* Display results */
3020 if (c->opt_long_list_entries) {
3021 d_printf(
3022 "\nEnumerating shared resources (exports) on remote server:\n\n"
3023 "\nShare name Type Description\n"
3024 "---------- ---- -----------\n");
3026 for (i = 0; i < entries_read; i++)
3027 display_share_info_1(c, &i1[i]);
3028 done:
3029 return status;
3032 static bool check_share_availability(struct cli_state *cli, const char *netname)
3034 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3035 d_printf("skipping [%s]: not a file share.\n", netname);
3036 return false;
3039 if (!cli_tdis(cli))
3040 return false;
3042 return true;
3045 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3046 const char *netname, uint32 type)
3048 /* only support disk shares */
3049 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3050 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3051 return false;
3054 /* skip builtin shares */
3055 /* FIXME: should print$ be added too ? */
3056 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3057 strequal(netname,"global"))
3058 return false;
3060 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3061 printf("excluding [%s]\n", netname);
3062 return false;
3065 return check_share_availability(cli, netname);
3069 * Migrate shares from a remote RPC server to the local RPC server.
3071 * All parameters are provided by the run_rpc_command function, except for
3072 * argc, argv which are passed through.
3074 * @param domain_sid The domain sid acquired from the remote server.
3075 * @param cli A cli_state connected to the server.
3076 * @param mem_ctx Talloc context, destroyed on completion of the function.
3077 * @param argc Standard main() style argc.
3078 * @param argv Standard main() style argv. Initial components are already
3079 * stripped.
3081 * @return Normal NTSTATUS return.
3084 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3085 const DOM_SID *domain_sid,
3086 const char *domain_name,
3087 struct cli_state *cli,
3088 struct rpc_pipe_client *pipe_hnd,
3089 TALLOC_CTX *mem_ctx,
3090 int argc,
3091 const char **argv)
3093 WERROR result;
3094 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3095 struct srvsvc_NetShareInfoCtr ctr_src;
3096 uint32 i;
3097 struct rpc_pipe_client *srvsvc_pipe = NULL;
3098 struct cli_state *cli_dst = NULL;
3099 uint32 level = 502; /* includes secdesc */
3100 uint32_t parm_error = 0;
3102 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3103 &ctr_src);
3104 if (!W_ERROR_IS_OK(result))
3105 goto done;
3107 /* connect destination PI_SRVSVC */
3108 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3109 &ndr_table_srvsvc.syntax_id);
3110 if (!NT_STATUS_IS_OK(nt_status))
3111 return nt_status;
3114 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3116 union srvsvc_NetShareInfo info;
3117 struct srvsvc_NetShareInfo502 info502 =
3118 ctr_src.ctr.ctr502->array[i];
3120 /* reset error-code */
3121 nt_status = NT_STATUS_UNSUCCESSFUL;
3123 if (!check_share_sanity(c, cli, info502.name, info502.type))
3124 continue;
3126 /* finally add the share on the dst server */
3128 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3129 info502.name, info502.path, info502.comment);
3131 info.info502 = &info502;
3133 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3134 srvsvc_pipe->desthost,
3135 502,
3136 &info,
3137 &parm_error,
3138 &result);
3140 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3141 printf(" [%s] does already exist\n",
3142 info502.name);
3143 continue;
3146 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3147 printf("cannot add share: %s\n", win_errstr(result));
3148 goto done;
3153 nt_status = NT_STATUS_OK;
3155 done:
3156 if (cli_dst) {
3157 cli_shutdown(cli_dst);
3160 return nt_status;
3165 * Migrate shares from a RPC server to another.
3167 * @param argc Standard main() style argc.
3168 * @param argv Standard main() style argv. Initial components are already
3169 * stripped.
3171 * @return A shell status integer (0 for success).
3173 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3174 const char **argv)
3176 if (c->display_usage) {
3177 d_printf("Usage:\n"
3178 "net rpc share migrate shares\n"
3179 " Migrate shares to local server\n");
3180 return 0;
3183 if (!c->opt_host) {
3184 printf("no server to migrate\n");
3185 return -1;
3188 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3189 rpc_share_migrate_shares_internals,
3190 argc, argv);
3194 * Copy a file/dir
3196 * @param f file_info
3197 * @param mask current search mask
3198 * @param state arg-pointer
3201 static void copy_fn(const char *mnt, file_info *f,
3202 const char *mask, void *state)
3204 static NTSTATUS nt_status;
3205 static struct copy_clistate *local_state;
3206 static fstring filename, new_mask;
3207 fstring dir;
3208 char *old_dir;
3209 struct net_context *c;
3211 local_state = (struct copy_clistate *)state;
3212 nt_status = NT_STATUS_UNSUCCESSFUL;
3214 c = local_state->c;
3216 if (strequal(f->name, ".") || strequal(f->name, ".."))
3217 return;
3219 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3221 /* DIRECTORY */
3222 if (f->mode & aDIR) {
3224 DEBUG(3,("got dir: %s\n", f->name));
3226 fstrcpy(dir, local_state->cwd);
3227 fstrcat(dir, "\\");
3228 fstrcat(dir, f->name);
3230 switch (net_mode_share)
3232 case NET_MODE_SHARE_MIGRATE:
3233 /* create that directory */
3234 nt_status = net_copy_file(c, local_state->mem_ctx,
3235 local_state->cli_share_src,
3236 local_state->cli_share_dst,
3237 dir, dir,
3238 c->opt_acls? true : false,
3239 c->opt_attrs? true : false,
3240 c->opt_timestamps? true:false,
3241 false);
3242 break;
3243 default:
3244 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3245 return;
3248 if (!NT_STATUS_IS_OK(nt_status))
3249 printf("could not handle dir %s: %s\n",
3250 dir, nt_errstr(nt_status));
3252 /* search below that directory */
3253 fstrcpy(new_mask, dir);
3254 fstrcat(new_mask, "\\*");
3256 old_dir = local_state->cwd;
3257 local_state->cwd = dir;
3258 if (!sync_files(local_state, new_mask))
3259 printf("could not handle files\n");
3260 local_state->cwd = old_dir;
3262 return;
3266 /* FILE */
3267 fstrcpy(filename, local_state->cwd);
3268 fstrcat(filename, "\\");
3269 fstrcat(filename, f->name);
3271 DEBUG(3,("got file: %s\n", filename));
3273 switch (net_mode_share)
3275 case NET_MODE_SHARE_MIGRATE:
3276 nt_status = net_copy_file(c, local_state->mem_ctx,
3277 local_state->cli_share_src,
3278 local_state->cli_share_dst,
3279 filename, filename,
3280 c->opt_acls? true : false,
3281 c->opt_attrs? true : false,
3282 c->opt_timestamps? true: false,
3283 true);
3284 break;
3285 default:
3286 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3287 return;
3290 if (!NT_STATUS_IS_OK(nt_status))
3291 printf("could not handle file %s: %s\n",
3292 filename, nt_errstr(nt_status));
3297 * sync files, can be called recursivly to list files
3298 * and then call copy_fn for each file
3300 * @param cp_clistate pointer to the copy_clistate we work with
3301 * @param mask the current search mask
3303 * @return Boolean result
3305 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3307 struct cli_state *targetcli;
3308 char *targetpath = NULL;
3310 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3312 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3313 mask, &targetcli, &targetpath ) ) {
3314 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3315 mask, cli_errstr(cp_clistate->cli_share_src));
3316 return false;
3319 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3320 d_fprintf(stderr, "listing %s failed with error: %s\n",
3321 mask, cli_errstr(targetcli));
3322 return false;
3325 return true;
3330 * Set the top level directory permissions before we do any further copies.
3331 * Should set up ACL inheritance.
3334 bool copy_top_level_perms(struct net_context *c,
3335 struct copy_clistate *cp_clistate,
3336 const char *sharename)
3338 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3340 switch (net_mode_share) {
3341 case NET_MODE_SHARE_MIGRATE:
3342 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3343 nt_status = net_copy_fileattr(c,
3344 cp_clistate->mem_ctx,
3345 cp_clistate->cli_share_src,
3346 cp_clistate->cli_share_dst,
3347 "\\", "\\",
3348 c->opt_acls? true : false,
3349 c->opt_attrs? true : false,
3350 c->opt_timestamps? true: false,
3351 false);
3352 break;
3353 default:
3354 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3355 break;
3358 if (!NT_STATUS_IS_OK(nt_status)) {
3359 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3360 sharename, nt_errstr(nt_status));
3361 return false;
3364 return true;
3368 * Sync all files inside a remote share to another share (over smb).
3370 * All parameters are provided by the run_rpc_command function, except for
3371 * argc, argv which are passed through.
3373 * @param domain_sid The domain sid acquired from the remote server.
3374 * @param cli A cli_state connected to the server.
3375 * @param mem_ctx Talloc context, destroyed on completion of the function.
3376 * @param argc Standard main() style argc.
3377 * @param argv Standard main() style argv. Initial components are already
3378 * stripped.
3380 * @return Normal NTSTATUS return.
3383 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3384 const DOM_SID *domain_sid,
3385 const char *domain_name,
3386 struct cli_state *cli,
3387 struct rpc_pipe_client *pipe_hnd,
3388 TALLOC_CTX *mem_ctx,
3389 int argc,
3390 const char **argv)
3392 WERROR result;
3393 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3394 struct srvsvc_NetShareInfoCtr ctr_src;
3395 uint32 i;
3396 uint32 level = 502;
3397 struct copy_clistate cp_clistate;
3398 bool got_src_share = false;
3399 bool got_dst_share = false;
3400 const char *mask = "\\*";
3401 char *dst = NULL;
3403 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3404 if (dst == NULL) {
3405 nt_status = NT_STATUS_NO_MEMORY;
3406 goto done;
3409 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3410 &ctr_src);
3412 if (!W_ERROR_IS_OK(result))
3413 goto done;
3415 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3417 struct srvsvc_NetShareInfo502 info502 =
3418 ctr_src.ctr.ctr502->array[i];
3420 if (!check_share_sanity(c, cli, info502.name, info502.type))
3421 continue;
3423 /* one might not want to mirror whole discs :) */
3424 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3425 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3426 continue;
3429 switch (net_mode_share)
3431 case NET_MODE_SHARE_MIGRATE:
3432 printf("syncing");
3433 break;
3434 default:
3435 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3436 break;
3438 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3439 info502.name,
3440 c->opt_acls ? "including" : "without",
3441 c->opt_attrs ? "including" : "without",
3442 c->opt_timestamps ? "(preserving timestamps)" : "");
3444 cp_clistate.mem_ctx = mem_ctx;
3445 cp_clistate.cli_share_src = NULL;
3446 cp_clistate.cli_share_dst = NULL;
3447 cp_clistate.cwd = NULL;
3448 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3449 cp_clistate.c = c;
3451 /* open share source */
3452 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3453 &cli->dest_ss, cli->desthost,
3454 info502.name, "A:");
3455 if (!NT_STATUS_IS_OK(nt_status))
3456 goto done;
3458 got_src_share = true;
3460 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3461 /* open share destination */
3462 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3463 NULL, dst, info502.name, "A:");
3464 if (!NT_STATUS_IS_OK(nt_status))
3465 goto done;
3467 got_dst_share = true;
3470 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3471 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3472 nt_status = NT_STATUS_UNSUCCESSFUL;
3473 goto done;
3476 if (!sync_files(&cp_clistate, mask)) {
3477 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3478 nt_status = NT_STATUS_UNSUCCESSFUL;
3479 goto done;
3483 nt_status = NT_STATUS_OK;
3485 done:
3487 if (got_src_share)
3488 cli_shutdown(cp_clistate.cli_share_src);
3490 if (got_dst_share)
3491 cli_shutdown(cp_clistate.cli_share_dst);
3493 SAFE_FREE(dst);
3494 return nt_status;
3498 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3500 if (c->display_usage) {
3501 d_printf("Usage:\n"
3502 "net share migrate files\n"
3503 " Migrate files to local server\n");
3504 return 0;
3507 if (!c->opt_host) {
3508 d_printf("no server to migrate\n");
3509 return -1;
3512 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3513 rpc_share_migrate_files_internals,
3514 argc, argv);
3518 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3520 * All parameters are provided by the run_rpc_command function, except for
3521 * argc, argv which are passed through.
3523 * @param domain_sid The domain sid acquired from the remote server.
3524 * @param cli A cli_state connected to the server.
3525 * @param mem_ctx Talloc context, destroyed on completion of the function.
3526 * @param argc Standard main() style argc.
3527 * @param argv Standard main() style argv. Initial components are already
3528 * stripped.
3530 * @return Normal NTSTATUS return.
3533 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3534 const DOM_SID *domain_sid,
3535 const char *domain_name,
3536 struct cli_state *cli,
3537 struct rpc_pipe_client *pipe_hnd,
3538 TALLOC_CTX *mem_ctx,
3539 int argc,
3540 const char **argv)
3542 WERROR result;
3543 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3544 struct srvsvc_NetShareInfoCtr ctr_src;
3545 union srvsvc_NetShareInfo info;
3546 uint32 i;
3547 struct rpc_pipe_client *srvsvc_pipe = NULL;
3548 struct cli_state *cli_dst = NULL;
3549 uint32 level = 502; /* includes secdesc */
3550 uint32_t parm_error = 0;
3552 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3553 &ctr_src);
3555 if (!W_ERROR_IS_OK(result))
3556 goto done;
3558 /* connect destination PI_SRVSVC */
3559 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3560 &ndr_table_srvsvc.syntax_id);
3561 if (!NT_STATUS_IS_OK(nt_status))
3562 return nt_status;
3565 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3567 struct srvsvc_NetShareInfo502 info502 =
3568 ctr_src.ctr.ctr502->array[i];
3570 /* reset error-code */
3571 nt_status = NT_STATUS_UNSUCCESSFUL;
3573 if (!check_share_sanity(c, cli, info502.name, info502.type))
3574 continue;
3576 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3577 info502.name, info502.path, info502.comment);
3579 if (c->opt_verbose)
3580 display_sec_desc(info502.sd_buf.sd);
3582 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3583 info.info502 = &info502;
3585 /* finally modify the share on the dst server */
3586 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3587 srvsvc_pipe->desthost,
3588 info502.name,
3589 level,
3590 &info,
3591 &parm_error,
3592 &result);
3593 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3594 printf("cannot set share-acl: %s\n", win_errstr(result));
3595 goto done;
3600 nt_status = NT_STATUS_OK;
3602 done:
3603 if (cli_dst) {
3604 cli_shutdown(cli_dst);
3607 return nt_status;
3612 * Migrate share-acls from a RPC server to another.
3614 * @param argc Standard main() style argc.
3615 * @param argv Standard main() style argv. Initial components are already
3616 * stripped.
3618 * @return A shell status integer (0 for success).
3620 static int rpc_share_migrate_security(struct net_context *c, int argc,
3621 const char **argv)
3623 if (c->display_usage) {
3624 d_printf("Usage:\n"
3625 "net rpc share migrate security\n"
3626 " Migrate share-acls to local server\n");
3627 return 0;
3630 if (!c->opt_host) {
3631 d_printf("no server to migrate\n");
3632 return -1;
3635 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3636 rpc_share_migrate_security_internals,
3637 argc, argv);
3641 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3642 * from one server to another.
3644 * @param argc Standard main() style argc.
3645 * @param argv Standard main() style argv. Initial components are already
3646 * stripped.
3648 * @return A shell status integer (0 for success).
3651 static int rpc_share_migrate_all(struct net_context *c, int argc,
3652 const char **argv)
3654 int ret;
3656 if (c->display_usage) {
3657 d_printf("Usage:\n"
3658 "net rpc share migrate all\n"
3659 " Migrates shares including all share settings\n");
3660 return 0;
3663 if (!c->opt_host) {
3664 d_printf("no server to migrate\n");
3665 return -1;
3668 /* order is important. we don't want to be locked out by the share-acl
3669 * before copying files - gd */
3671 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3672 rpc_share_migrate_shares_internals, argc, argv);
3673 if (ret)
3674 return ret;
3676 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3677 rpc_share_migrate_files_internals, argc, argv);
3678 if (ret)
3679 return ret;
3681 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3682 rpc_share_migrate_security_internals, argc,
3683 argv);
3688 * 'net rpc share migrate' entrypoint.
3689 * @param argc Standard main() style argc.
3690 * @param argv Standard main() style argv. Initial components are already
3691 * stripped.
3693 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3696 struct functable func[] = {
3698 "all",
3699 rpc_share_migrate_all,
3700 NET_TRANSPORT_RPC,
3701 "Migrate shares from remote to local server",
3702 "net rpc share migrate all\n"
3703 " Migrate shares from remote to local server"
3706 "files",
3707 rpc_share_migrate_files,
3708 NET_TRANSPORT_RPC,
3709 "Migrate files from remote to local server",
3710 "net rpc share migrate files\n"
3711 " Migrate files from remote to local server"
3714 "security",
3715 rpc_share_migrate_security,
3716 NET_TRANSPORT_RPC,
3717 "Migrate share-ACLs from remote to local server",
3718 "net rpc share migrate security\n"
3719 " Migrate share-ACLs from remote to local server"
3722 "shares",
3723 rpc_share_migrate_shares,
3724 NET_TRANSPORT_RPC,
3725 "Migrate shares from remote to local server",
3726 "net rpc share migrate shares\n"
3727 " Migrate shares from remote to local server"
3729 {NULL, NULL, 0, NULL, NULL}
3732 net_mode_share = NET_MODE_SHARE_MIGRATE;
3734 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3737 struct full_alias {
3738 DOM_SID sid;
3739 uint32 num_members;
3740 DOM_SID *members;
3743 static int num_server_aliases;
3744 static struct full_alias *server_aliases;
3747 * Add an alias to the static list.
3749 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3751 if (server_aliases == NULL)
3752 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3754 server_aliases[num_server_aliases] = *alias;
3755 num_server_aliases += 1;
3759 * For a specific domain on the server, fetch all the aliases
3760 * and their members. Add all of them to the server_aliases.
3763 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3764 TALLOC_CTX *mem_ctx,
3765 struct policy_handle *connect_pol,
3766 const DOM_SID *domain_sid)
3768 uint32 start_idx, max_entries, num_entries, i;
3769 struct samr_SamArray *groups = NULL;
3770 NTSTATUS result;
3771 struct policy_handle domain_pol;
3773 /* Get domain policy handle */
3775 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3776 connect_pol,
3777 MAXIMUM_ALLOWED_ACCESS,
3778 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3779 &domain_pol);
3780 if (!NT_STATUS_IS_OK(result))
3781 return result;
3783 start_idx = 0;
3784 max_entries = 250;
3786 do {
3787 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3788 &domain_pol,
3789 &start_idx,
3790 &groups,
3791 max_entries,
3792 &num_entries);
3793 for (i = 0; i < num_entries; i++) {
3795 struct policy_handle alias_pol;
3796 struct full_alias alias;
3797 struct lsa_SidArray sid_array;
3798 int j;
3800 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3801 &domain_pol,
3802 MAXIMUM_ALLOWED_ACCESS,
3803 groups->entries[i].idx,
3804 &alias_pol);
3805 if (!NT_STATUS_IS_OK(result))
3806 goto done;
3808 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3809 &alias_pol,
3810 &sid_array);
3811 if (!NT_STATUS_IS_OK(result))
3812 goto done;
3814 alias.num_members = sid_array.num_sids;
3816 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3817 if (!NT_STATUS_IS_OK(result))
3818 goto done;
3820 alias.members = NULL;
3822 if (alias.num_members > 0) {
3823 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3825 for (j = 0; j < alias.num_members; j++)
3826 sid_copy(&alias.members[j],
3827 sid_array.sids[j].sid);
3830 sid_copy(&alias.sid, domain_sid);
3831 sid_append_rid(&alias.sid, groups->entries[i].idx);
3833 push_alias(mem_ctx, &alias);
3835 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3837 result = NT_STATUS_OK;
3839 done:
3840 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3842 return result;
3846 * Dump server_aliases as names for debugging purposes.
3849 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3850 const DOM_SID *domain_sid,
3851 const char *domain_name,
3852 struct cli_state *cli,
3853 struct rpc_pipe_client *pipe_hnd,
3854 TALLOC_CTX *mem_ctx,
3855 int argc,
3856 const char **argv)
3858 int i;
3859 NTSTATUS result;
3860 struct policy_handle lsa_pol;
3862 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3863 SEC_FLAG_MAXIMUM_ALLOWED,
3864 &lsa_pol);
3865 if (!NT_STATUS_IS_OK(result))
3866 return result;
3868 for (i=0; i<num_server_aliases; i++) {
3869 char **names;
3870 char **domains;
3871 enum lsa_SidType *types;
3872 int j;
3874 struct full_alias *alias = &server_aliases[i];
3876 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3877 &alias->sid,
3878 &domains, &names, &types);
3879 if (!NT_STATUS_IS_OK(result))
3880 continue;
3882 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3884 if (alias->num_members == 0) {
3885 DEBUG(1, ("\n"));
3886 continue;
3889 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3890 alias->num_members,
3891 alias->members,
3892 &domains, &names, &types);
3894 if (!NT_STATUS_IS_OK(result) &&
3895 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3896 continue;
3898 for (j=0; j<alias->num_members; j++)
3899 DEBUG(1, ("%s\\%s (%d); ",
3900 domains[j] ? domains[j] : "*unknown*",
3901 names[j] ? names[j] : "*unknown*",types[j]));
3902 DEBUG(1, ("\n"));
3905 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3907 return NT_STATUS_OK;
3911 * Fetch a list of all server aliases and their members into
3912 * server_aliases.
3915 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3916 const DOM_SID *domain_sid,
3917 const char *domain_name,
3918 struct cli_state *cli,
3919 struct rpc_pipe_client *pipe_hnd,
3920 TALLOC_CTX *mem_ctx,
3921 int argc,
3922 const char **argv)
3924 NTSTATUS result;
3925 struct policy_handle connect_pol;
3927 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3928 pipe_hnd->desthost,
3929 MAXIMUM_ALLOWED_ACCESS,
3930 &connect_pol);
3932 if (!NT_STATUS_IS_OK(result))
3933 goto done;
3935 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3936 &global_sid_Builtin);
3938 if (!NT_STATUS_IS_OK(result))
3939 goto done;
3941 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3942 domain_sid);
3944 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3945 done:
3946 return result;
3949 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3951 token->num_sids = 4;
3953 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
3954 d_fprintf(stderr, "malloc failed\n");
3955 token->num_sids = 0;
3956 return;
3959 token->user_sids[0] = *user_sid;
3960 sid_copy(&token->user_sids[1], &global_sid_World);
3961 sid_copy(&token->user_sids[2], &global_sid_Network);
3962 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3965 static void free_user_token(NT_USER_TOKEN *token)
3967 SAFE_FREE(token->user_sids);
3970 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3972 if (is_sid_in_token(token, sid))
3973 return;
3975 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3976 if (!token->user_sids) {
3977 return;
3980 sid_copy(&token->user_sids[token->num_sids], sid);
3982 token->num_sids += 1;
3985 struct user_token {
3986 fstring name;
3987 NT_USER_TOKEN token;
3990 static void dump_user_token(struct user_token *token)
3992 int i;
3994 d_printf("%s\n", token->name);
3996 for (i=0; i<token->token.num_sids; i++) {
3997 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4001 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4003 int i;
4005 for (i=0; i<alias->num_members; i++) {
4006 if (sid_compare(sid, &alias->members[i]) == 0)
4007 return true;
4010 return false;
4013 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4015 int i;
4017 for (i=0; i<num_server_aliases; i++) {
4018 if (is_alias_member(&sid, &server_aliases[i]))
4019 add_sid_to_token(token, &server_aliases[i].sid);
4024 * We got a user token with all the SIDs we can know about without asking the
4025 * server directly. These are the user and domain group sids. All of these can
4026 * be members of aliases. So scan the list of aliases for each of the SIDs and
4027 * add them to the token.
4030 static void collect_alias_memberships(NT_USER_TOKEN *token)
4032 int num_global_sids = token->num_sids;
4033 int i;
4035 for (i=0; i<num_global_sids; i++) {
4036 collect_sid_memberships(token, token->user_sids[i]);
4040 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4042 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4043 enum wbcSidType type;
4044 fstring full_name;
4045 struct wbcDomainSid wsid;
4046 char *sid_str = NULL;
4047 DOM_SID user_sid;
4048 uint32_t num_groups;
4049 gid_t *groups = NULL;
4050 uint32_t i;
4052 fstr_sprintf(full_name, "%s%c%s",
4053 domain, *lp_winbind_separator(), user);
4055 /* First let's find out the user sid */
4057 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4059 if (!WBC_ERROR_IS_OK(wbc_status)) {
4060 DEBUG(1, ("winbind could not find %s: %s\n",
4061 full_name, wbcErrorString(wbc_status)));
4062 return false;
4065 wbc_status = wbcSidToString(&wsid, &sid_str);
4066 if (!WBC_ERROR_IS_OK(wbc_status)) {
4067 return false;
4070 if (type != SID_NAME_USER) {
4071 wbcFreeMemory(sid_str);
4072 DEBUG(1, ("%s is not a user\n", full_name));
4073 return false;
4076 if (!string_to_sid(&user_sid, sid_str)) {
4077 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4078 return false;
4081 wbcFreeMemory(sid_str);
4082 sid_str = NULL;
4084 init_user_token(token, &user_sid);
4086 /* And now the groups winbind knows about */
4088 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4089 if (!WBC_ERROR_IS_OK(wbc_status)) {
4090 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4091 full_name, wbcErrorString(wbc_status)));
4092 return false;
4095 for (i = 0; i < num_groups; i++) {
4096 gid_t gid = groups[i];
4097 DOM_SID sid;
4099 wbc_status = wbcGidToSid(gid, &wsid);
4100 if (!WBC_ERROR_IS_OK(wbc_status)) {
4101 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4102 (unsigned int)gid, wbcErrorString(wbc_status)));
4103 wbcFreeMemory(groups);
4104 return false;
4107 wbc_status = wbcSidToString(&wsid, &sid_str);
4108 if (!WBC_ERROR_IS_OK(wbc_status)) {
4109 wbcFreeMemory(groups);
4110 return false;
4113 DEBUG(3, (" %s\n", sid_str));
4115 string_to_sid(&sid, sid_str);
4116 wbcFreeMemory(sid_str);
4117 sid_str = NULL;
4119 add_sid_to_token(token, &sid);
4121 wbcFreeMemory(groups);
4123 return true;
4127 * Get a list of all user tokens we want to look at
4130 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4131 struct user_token **user_tokens)
4133 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4134 uint32_t i, num_users;
4135 const char **users;
4136 struct user_token *result;
4137 TALLOC_CTX *frame = NULL;
4139 if (lp_winbind_use_default_domain() &&
4140 (c->opt_target_workgroup == NULL)) {
4141 d_fprintf(stderr, "winbind use default domain = yes set, "
4142 "please specify a workgroup\n");
4143 return false;
4146 /* Send request to winbind daemon */
4148 wbc_status = wbcListUsers(NULL, &num_users, &users);
4149 if (!WBC_ERROR_IS_OK(wbc_status)) {
4150 DEBUG(1, ("winbind could not list users: %s\n",
4151 wbcErrorString(wbc_status)));
4152 return false;
4155 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4157 if (result == NULL) {
4158 DEBUG(1, ("Could not malloc sid array\n"));
4159 wbcFreeMemory(users);
4160 return false;
4163 frame = talloc_stackframe();
4164 for (i=0; i < num_users; i++) {
4165 fstring domain, user;
4166 char *p;
4168 fstrcpy(result[i].name, users[i]);
4170 p = strchr(users[i], *lp_winbind_separator());
4172 DEBUG(3, ("%s\n", users[i]));
4174 if (p == NULL) {
4175 fstrcpy(domain, c->opt_target_workgroup);
4176 fstrcpy(user, users[i]);
4177 } else {
4178 *p++ = '\0';
4179 fstrcpy(domain, users[i]);
4180 strupper_m(domain);
4181 fstrcpy(user, p);
4184 get_user_sids(domain, user, &(result[i].token));
4185 i+=1;
4187 TALLOC_FREE(frame);
4188 wbcFreeMemory(users);
4190 *num_tokens = num_users;
4191 *user_tokens = result;
4193 return true;
4196 static bool get_user_tokens_from_file(FILE *f,
4197 int *num_tokens,
4198 struct user_token **tokens)
4200 struct user_token *token = NULL;
4202 while (!feof(f)) {
4203 fstring line;
4205 if (fgets(line, sizeof(line)-1, f) == NULL) {
4206 return true;
4209 if (line[strlen(line)-1] == '\n')
4210 line[strlen(line)-1] = '\0';
4212 if (line[0] == ' ') {
4213 /* We have a SID */
4215 DOM_SID sid;
4216 if(!string_to_sid(&sid, &line[1])) {
4217 DEBUG(1,("get_user_tokens_from_file: Could "
4218 "not convert sid %s \n",&line[1]));
4219 return false;
4222 if (token == NULL) {
4223 DEBUG(0, ("File does not begin with username"));
4224 return false;
4227 add_sid_to_token(&token->token, &sid);
4228 continue;
4231 /* And a new user... */
4233 *num_tokens += 1;
4234 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4235 if (*tokens == NULL) {
4236 DEBUG(0, ("Could not realloc tokens\n"));
4237 return false;
4240 token = &((*tokens)[*num_tokens-1]);
4242 fstrcpy(token->name, line);
4243 token->token.num_sids = 0;
4244 token->token.user_sids = NULL;
4245 continue;
4248 return false;
4253 * Show the list of all users that have access to a share
4256 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4257 TALLOC_CTX *mem_ctx,
4258 const char *netname,
4259 int num_tokens,
4260 struct user_token *tokens)
4262 int fnum;
4263 SEC_DESC *share_sd = NULL;
4264 SEC_DESC *root_sd = NULL;
4265 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4266 int i;
4267 union srvsvc_NetShareInfo info;
4268 WERROR result;
4269 NTSTATUS status;
4270 uint16 cnum;
4272 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4273 pipe_hnd->desthost,
4274 netname,
4275 502,
4276 &info,
4277 &result);
4279 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4280 DEBUG(1, ("Coult not query secdesc for share %s\n",
4281 netname));
4282 return;
4285 share_sd = info.info502->sd_buf.sd;
4286 if (share_sd == NULL) {
4287 DEBUG(1, ("Got no secdesc for share %s\n",
4288 netname));
4291 cnum = cli->cnum;
4293 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4294 return;
4297 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4299 if (fnum != -1) {
4300 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4303 for (i=0; i<num_tokens; i++) {
4304 uint32 acc_granted;
4306 if (share_sd != NULL) {
4307 status = se_access_check(share_sd, &tokens[i].token,
4308 1, &acc_granted);
4310 if (!NT_STATUS_IS_OK(status)) {
4311 DEBUG(1, ("Could not check share_sd for "
4312 "user %s\n",
4313 tokens[i].name));
4314 continue;
4318 if (root_sd == NULL) {
4319 d_printf(" %s\n", tokens[i].name);
4320 continue;
4323 status = se_access_check(root_sd, &tokens[i].token,
4324 1, &acc_granted);
4325 if (!NT_STATUS_IS_OK(status)) {
4326 DEBUG(1, ("Could not check root_sd for user %s\n",
4327 tokens[i].name));
4328 continue;
4330 d_printf(" %s\n", tokens[i].name);
4333 if (fnum != -1)
4334 cli_close(cli, fnum);
4335 cli_tdis(cli);
4336 cli->cnum = cnum;
4338 return;
4341 struct share_list {
4342 int num_shares;
4343 char **shares;
4346 static void collect_share(const char *name, uint32 m,
4347 const char *comment, void *state)
4349 struct share_list *share_list = (struct share_list *)state;
4351 if (m != STYPE_DISKTREE)
4352 return;
4354 share_list->num_shares += 1;
4355 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4356 if (!share_list->shares) {
4357 share_list->num_shares = 0;
4358 return;
4360 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4364 * List shares on a remote RPC server, including the security descriptors.
4366 * All parameters are provided by the run_rpc_command function, except for
4367 * argc, argv which are passed through.
4369 * @param domain_sid The domain sid acquired from the remote server.
4370 * @param cli A cli_state connected to the server.
4371 * @param mem_ctx Talloc context, destroyed on completion of the function.
4372 * @param argc Standard main() style argc.
4373 * @param argv Standard main() style argv. Initial components are already
4374 * stripped.
4376 * @return Normal NTSTATUS return.
4379 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4380 const DOM_SID *domain_sid,
4381 const char *domain_name,
4382 struct cli_state *cli,
4383 struct rpc_pipe_client *pipe_hnd,
4384 TALLOC_CTX *mem_ctx,
4385 int argc,
4386 const char **argv)
4388 int ret;
4389 bool r;
4390 uint32 i;
4391 FILE *f;
4393 struct user_token *tokens = NULL;
4394 int num_tokens = 0;
4396 struct share_list share_list;
4398 if (argc == 0) {
4399 f = stdin;
4400 } else {
4401 f = fopen(argv[0], "r");
4404 if (f == NULL) {
4405 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4406 return NT_STATUS_UNSUCCESSFUL;
4409 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4411 if (f != stdin)
4412 fclose(f);
4414 if (!r) {
4415 DEBUG(0, ("Could not read users from file\n"));
4416 return NT_STATUS_UNSUCCESSFUL;
4419 for (i=0; i<num_tokens; i++)
4420 collect_alias_memberships(&tokens[i].token);
4422 share_list.num_shares = 0;
4423 share_list.shares = NULL;
4425 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4427 if (ret == -1) {
4428 DEBUG(0, ("Error returning browse list: %s\n",
4429 cli_errstr(cli)));
4430 goto done;
4433 for (i = 0; i < share_list.num_shares; i++) {
4434 char *netname = share_list.shares[i];
4436 if (netname[strlen(netname)-1] == '$')
4437 continue;
4439 d_printf("%s\n", netname);
4441 show_userlist(pipe_hnd, mem_ctx, netname,
4442 num_tokens, tokens);
4444 done:
4445 for (i=0; i<num_tokens; i++) {
4446 free_user_token(&tokens[i].token);
4448 SAFE_FREE(tokens);
4449 SAFE_FREE(share_list.shares);
4451 return NT_STATUS_OK;
4454 static int rpc_share_allowedusers(struct net_context *c, int argc,
4455 const char **argv)
4457 int result;
4459 if (c->display_usage) {
4460 d_printf("Usage:\n"
4461 "net rpc share allowedusers\n"
4462 " List allowed users\n");
4463 return 0;
4466 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4467 rpc_aliaslist_internals,
4468 argc, argv);
4469 if (result != 0)
4470 return result;
4472 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4473 rpc_aliaslist_dump,
4474 argc, argv);
4475 if (result != 0)
4476 return result;
4478 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4479 rpc_share_allowedusers_internals,
4480 argc, argv);
4483 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4485 int num_tokens = 0;
4486 struct user_token *tokens = NULL;
4487 int i;
4489 if (argc != 0) {
4490 net_usersidlist_usage(c, argc, argv);
4491 return 0;
4494 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4495 DEBUG(0, ("Could not get the user/sid list\n"));
4496 return 0;
4499 for (i=0; i<num_tokens; i++) {
4500 dump_user_token(&tokens[i]);
4501 free_user_token(&tokens[i].token);
4504 SAFE_FREE(tokens);
4505 return 1;
4508 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4510 d_printf("net usersidlist\n"
4511 "\tprints out a list of all users the running winbind knows\n"
4512 "\tabout, together with all their SIDs. This is used as\n"
4513 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4515 net_common_flags_usage(c, argc, argv);
4516 return -1;
4520 * 'net rpc share' entrypoint.
4521 * @param argc Standard main() style argc.
4522 * @param argv Standard main() style argv. Initial components are already
4523 * stripped.
4526 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4528 NET_API_STATUS status;
4530 struct functable func[] = {
4532 "add",
4533 rpc_share_add,
4534 NET_TRANSPORT_RPC,
4535 "Add share",
4536 "net rpc share add\n"
4537 " Add share"
4540 "delete",
4541 rpc_share_delete,
4542 NET_TRANSPORT_RPC,
4543 "Remove share",
4544 "net rpc share delete\n"
4545 " Remove share"
4548 "allowedusers",
4549 rpc_share_allowedusers,
4550 NET_TRANSPORT_RPC,
4551 "Modify allowed users",
4552 "net rpc share allowedusers\n"
4553 " Modify allowed users"
4556 "migrate",
4557 rpc_share_migrate,
4558 NET_TRANSPORT_RPC,
4559 "Migrate share to local server",
4560 "net rpc share migrate\n"
4561 " Migrate share to local server"
4564 "list",
4565 rpc_share_list,
4566 NET_TRANSPORT_RPC,
4567 "List shares",
4568 "net rpc share list\n"
4569 " List shares"
4571 {NULL, NULL, 0, NULL, NULL}
4574 status = libnetapi_init(&c->netapi_ctx);
4575 if (status != 0) {
4576 return -1;
4578 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4579 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4580 if (c->opt_kerberos) {
4581 libnetapi_set_use_kerberos(c->netapi_ctx);
4584 if (argc == 0) {
4585 if (c->display_usage) {
4586 d_printf("Usage:\n"
4587 "net rpc share\n"
4588 " List shares\n"
4589 " Alias for net rpc share list\n");
4590 net_display_usage_from_functable(func);
4591 return 0;
4594 return rpc_share_list(c, argc, argv);
4597 return net_run_function(c, argc, argv, "net rpc share", func);
4600 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4601 TALLOC_CTX *mem_ctx,
4602 struct rpc_sh_ctx *ctx,
4603 struct rpc_pipe_client *pipe_hnd,
4604 int argc, const char **argv)
4607 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4610 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4611 TALLOC_CTX *mem_ctx,
4612 struct rpc_sh_ctx *ctx,
4613 struct rpc_pipe_client *pipe_hnd,
4614 int argc, const char **argv)
4616 NET_API_STATUS status;
4617 uint32_t parm_err = 0;
4618 struct SHARE_INFO_2 i2;
4620 if ((argc < 2) || (argc > 3)) {
4621 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4622 ctx->whoami);
4623 return NT_STATUS_INVALID_PARAMETER;
4626 i2.shi2_netname = argv[0];
4627 i2.shi2_type = STYPE_DISKTREE;
4628 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4629 i2.shi2_permissions = 0;
4630 i2.shi2_max_uses = 0;
4631 i2.shi2_current_uses = 0;
4632 i2.shi2_path = argv[1];
4633 i2.shi2_passwd = NULL;
4635 status = NetShareAdd(pipe_hnd->desthost,
4637 (uint8_t *)&i2,
4638 &parm_err);
4640 return werror_to_ntstatus(W_ERROR(status));
4643 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4644 TALLOC_CTX *mem_ctx,
4645 struct rpc_sh_ctx *ctx,
4646 struct rpc_pipe_client *pipe_hnd,
4647 int argc, const char **argv)
4649 if (argc != 1) {
4650 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4651 return NT_STATUS_INVALID_PARAMETER;
4654 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4657 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4658 TALLOC_CTX *mem_ctx,
4659 struct rpc_sh_ctx *ctx,
4660 struct rpc_pipe_client *pipe_hnd,
4661 int argc, const char **argv)
4663 union srvsvc_NetShareInfo info;
4664 WERROR result;
4665 NTSTATUS status;
4667 if (argc != 1) {
4668 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4669 return NT_STATUS_INVALID_PARAMETER;
4672 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4673 pipe_hnd->desthost,
4674 argv[0],
4676 &info,
4677 &result);
4678 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4679 goto done;
4682 d_printf("Name: %s\n", info.info2->name);
4683 d_printf("Comment: %s\n", info.info2->comment);
4684 d_printf("Path: %s\n", info.info2->path);
4685 d_printf("Password: %s\n", info.info2->password);
4687 done:
4688 return werror_to_ntstatus(result);
4691 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4692 struct rpc_sh_ctx *ctx)
4694 static struct rpc_sh_cmd cmds[] = {
4696 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4697 "List available shares" },
4699 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4700 "Add a share" },
4702 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4703 "Delete a share" },
4705 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4706 "Get information about a share" },
4708 { NULL, NULL, 0, NULL, NULL }
4711 return cmds;
4714 /****************************************************************************/
4716 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4718 return net_file_usage(c, argc, argv);
4722 * Close a file on a remote RPC server.
4724 * @param argc Standard main() style argc.
4725 * @param argv Standard main() style argv. Initial components are already
4726 * stripped.
4728 * @return A shell status integer (0 for success).
4730 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4732 if (argc < 1 || c->display_usage) {
4733 return rpc_file_usage(c, argc, argv);
4736 return NetFileClose(c->opt_host, atoi(argv[0]));
4740 * Formatted print of open file info
4742 * @param r struct FILE_INFO_3 contents
4745 static void display_file_info_3(struct FILE_INFO_3 *r)
4747 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4748 r->fi3_id, r->fi3_username, r->fi3_permissions,
4749 r->fi3_num_locks, r->fi3_pathname);
4753 * List files for a user on a remote RPC server.
4755 * @param argc Standard main() style argc.
4756 * @param argv Standard main() style argv. Initial components are already
4757 * stripped.
4759 * @return A shell status integer (0 for success)..
4762 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4764 NET_API_STATUS status;
4765 uint32 preferred_len = 0xffffffff, i;
4766 const char *username=NULL;
4767 uint32_t total_entries = 0;
4768 uint32_t entries_read = 0;
4769 uint32_t resume_handle = 0;
4770 struct FILE_INFO_3 *i3 = NULL;
4772 if (c->display_usage) {
4773 return rpc_file_usage(c, argc, argv);
4776 /* if argc > 0, must be user command */
4777 if (argc > 0) {
4778 username = smb_xstrdup(argv[0]);
4781 status = NetFileEnum(c->opt_host,
4782 NULL,
4783 username,
4785 (uint8_t **)(void *)&i3,
4786 preferred_len,
4787 &entries_read,
4788 &total_entries,
4789 &resume_handle);
4791 if (status != 0) {
4792 goto done;
4795 /* Display results */
4797 d_printf(
4798 "\nEnumerating open files on remote server:\n\n"
4799 "\nFileId Opened by Perms Locks Path"
4800 "\n------ --------- ----- ----- ---- \n");
4801 for (i = 0; i < entries_read; i++) {
4802 display_file_info_3(&i3[i]);
4804 done:
4805 return status;
4809 * 'net rpc file' entrypoint.
4810 * @param argc Standard main() style argc.
4811 * @param argv Standard main() style argv. Initial components are already
4812 * stripped.
4815 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4817 NET_API_STATUS status;
4819 struct functable func[] = {
4821 "close",
4822 rpc_file_close,
4823 NET_TRANSPORT_RPC,
4824 "Close opened file",
4825 "net rpc file close\n"
4826 " Close opened file"
4829 "user",
4830 rpc_file_user,
4831 NET_TRANSPORT_RPC,
4832 "List files opened by user",
4833 "net rpc file user\n"
4834 " List files opened by user"
4836 #if 0
4838 "info",
4839 rpc_file_info,
4840 NET_TRANSPORT_RPC,
4841 "Display information about opened file",
4842 "net rpc file info\n"
4843 " Display information about opened file"
4845 #endif
4846 {NULL, NULL, 0, NULL, NULL}
4849 status = libnetapi_init(&c->netapi_ctx);
4850 if (status != 0) {
4851 return -1;
4853 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4854 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4855 if (c->opt_kerberos) {
4856 libnetapi_set_use_kerberos(c->netapi_ctx);
4859 if (argc == 0) {
4860 if (c->display_usage) {
4861 d_printf("Usage:\n");
4862 d_printf("net rpc file\n"
4863 " List opened files\n");
4864 net_display_usage_from_functable(func);
4865 return 0;
4868 return rpc_file_user(c, argc, argv);
4871 return net_run_function(c, argc, argv, "net rpc file", func);
4875 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4877 * All parameters are provided by the run_rpc_command function, except for
4878 * argc, argv which are passed through.
4880 * @param c A net_context structure.
4881 * @param domain_sid The domain sid acquired from the remote server.
4882 * @param cli A cli_state connected to the server.
4883 * @param mem_ctx Talloc context, destroyed on completion of the function.
4884 * @param argc Standard main() style argc.
4885 * @param argv Standard main() style argv. Initial components are already
4886 * stripped.
4888 * @return Normal NTSTATUS return.
4891 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4892 const DOM_SID *domain_sid,
4893 const char *domain_name,
4894 struct cli_state *cli,
4895 struct rpc_pipe_client *pipe_hnd,
4896 TALLOC_CTX *mem_ctx,
4897 int argc,
4898 const char **argv)
4900 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4902 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4904 if (NT_STATUS_IS_OK(result)) {
4905 d_printf("\nShutdown successfully aborted\n");
4906 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4907 } else
4908 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4910 return result;
4914 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4916 * All parameters are provided by the run_rpc_command function, except for
4917 * argc, argv which are passed through.
4919 * @param c A net_context structure.
4920 * @param domain_sid The domain sid acquired from the remote server.
4921 * @param cli A cli_state connected to the server.
4922 * @param mem_ctx Talloc context, destroyed on completion of the function.
4923 * @param argc Standard main() style argc.
4924 * @param argv Standard main() style argv. Initial components are already
4925 * stripped.
4927 * @return Normal NTSTATUS return.
4930 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4931 const DOM_SID *domain_sid,
4932 const char *domain_name,
4933 struct cli_state *cli,
4934 struct rpc_pipe_client *pipe_hnd,
4935 TALLOC_CTX *mem_ctx,
4936 int argc,
4937 const char **argv)
4939 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4941 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
4943 if (NT_STATUS_IS_OK(result)) {
4944 d_printf("\nShutdown successfully aborted\n");
4945 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4946 } else
4947 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4949 return result;
4953 * ABORT the shutdown of a remote RPC server.
4955 * @param argc Standard main() style argc.
4956 * @param argv Standard main() style argv. Initial components are already
4957 * stripped.
4959 * @return A shell status integer (0 for success).
4962 static int rpc_shutdown_abort(struct net_context *c, int argc,
4963 const char **argv)
4965 int rc = -1;
4967 if (c->display_usage) {
4968 d_printf("Usage:\n"
4969 "net rpc abortshutdown\n"
4970 " Abort a scheduled shutdown\n");
4971 return 0;
4974 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
4975 rpc_shutdown_abort_internals, argc, argv);
4977 if (rc == 0)
4978 return rc;
4980 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4982 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
4983 rpc_reg_shutdown_abort_internals,
4984 argc, argv);
4988 * Shut down a remote RPC Server via initshutdown pipe.
4990 * All parameters are provided by the run_rpc_command function, except for
4991 * argc, argv which are passed through.
4993 * @param c A net_context structure.
4994 * @param domain_sid The domain sid acquired from the remote server.
4995 * @param cli A cli_state connected to the server.
4996 * @param mem_ctx Talloc context, destroyed on completion of the function.
4997 * @param argc Standard main() style argc.
4998 * @param argv Standard main() style argv. Initial components are already
4999 * stripped.
5001 * @return Normal NTSTATUS return.
5004 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5005 const DOM_SID *domain_sid,
5006 const char *domain_name,
5007 struct cli_state *cli,
5008 struct rpc_pipe_client *pipe_hnd,
5009 TALLOC_CTX *mem_ctx,
5010 int argc,
5011 const char **argv)
5013 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5014 const char *msg = "This machine will be shutdown shortly";
5015 uint32 timeout = 20;
5016 struct lsa_StringLarge msg_string;
5018 if (c->opt_comment) {
5019 msg = c->opt_comment;
5021 if (c->opt_timeout) {
5022 timeout = c->opt_timeout;
5025 msg_string.string = msg;
5027 /* create an entry */
5028 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5029 &msg_string, timeout, c->opt_force, c->opt_reboot,
5030 NULL);
5032 if (NT_STATUS_IS_OK(result)) {
5033 d_printf("\nShutdown of remote machine succeeded\n");
5034 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5035 } else {
5036 DEBUG(1,("Shutdown of remote machine failed!\n"));
5038 return result;
5042 * Shut down a remote RPC Server via winreg pipe.
5044 * All parameters are provided by the run_rpc_command function, except for
5045 * argc, argv which are passed through.
5047 * @param c A net_context structure.
5048 * @param domain_sid The domain sid acquired from the remote server.
5049 * @param cli A cli_state connected to the server.
5050 * @param mem_ctx Talloc context, destroyed on completion of the function.
5051 * @param argc Standard main() style argc.
5052 * @param argv Standard main() style argv. Initial components are already
5053 * stripped.
5055 * @return Normal NTSTATUS return.
5058 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5059 const DOM_SID *domain_sid,
5060 const char *domain_name,
5061 struct cli_state *cli,
5062 struct rpc_pipe_client *pipe_hnd,
5063 TALLOC_CTX *mem_ctx,
5064 int argc,
5065 const char **argv)
5067 const char *msg = "This machine will be shutdown shortly";
5068 uint32 timeout = 20;
5069 struct lsa_StringLarge msg_string;
5070 NTSTATUS result;
5071 WERROR werr;
5073 if (c->opt_comment) {
5074 msg = c->opt_comment;
5076 msg_string.string = msg;
5078 if (c->opt_timeout) {
5079 timeout = c->opt_timeout;
5082 /* create an entry */
5083 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5084 &msg_string, timeout, c->opt_force, c->opt_reboot,
5085 &werr);
5087 if (NT_STATUS_IS_OK(result)) {
5088 d_printf("\nShutdown of remote machine succeeded\n");
5089 } else {
5090 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5091 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5092 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5093 else
5094 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5097 return result;
5101 * Shut down a remote RPC server.
5103 * @param argc Standard main() style argc.
5104 * @param argv Standard main() style argv. Initial components are already
5105 * stripped.
5107 * @return A shell status integer (0 for success).
5110 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5112 int rc = -1;
5114 if (c->display_usage) {
5115 d_printf("Usage:\n"
5116 "net rpc shutdown\n"
5117 " Shut down a remote RPC server\n");
5118 return 0;
5121 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5122 rpc_init_shutdown_internals, argc, argv);
5124 if (rc) {
5125 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5126 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5127 rpc_reg_shutdown_internals, argc, argv);
5130 return rc;
5133 /***************************************************************************
5134 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5135 ***************************************************************************/
5138 * Add interdomain trust account to the RPC server.
5139 * All parameters (except for argc and argv) are passed by run_rpc_command
5140 * function.
5142 * @param c A net_context structure.
5143 * @param domain_sid The domain sid acquired from the server.
5144 * @param cli A cli_state connected to the server.
5145 * @param mem_ctx Talloc context, destroyed on completion of the function.
5146 * @param argc Standard main() style argc.
5147 * @param argv Standard main() style argv. Initial components are already
5148 * stripped.
5150 * @return normal NTSTATUS return code.
5153 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5154 const DOM_SID *domain_sid,
5155 const char *domain_name,
5156 struct cli_state *cli,
5157 struct rpc_pipe_client *pipe_hnd,
5158 TALLOC_CTX *mem_ctx,
5159 int argc,
5160 const char **argv)
5162 struct policy_handle connect_pol, domain_pol, user_pol;
5163 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5164 char *acct_name;
5165 struct lsa_String lsa_acct_name;
5166 uint32 acb_info;
5167 uint32 acct_flags=0;
5168 uint32 user_rid;
5169 uint32_t access_granted = 0;
5170 union samr_UserInfo info;
5171 unsigned int orig_timeout;
5173 if (argc != 2) {
5174 d_printf("Usage: net rpc trustdom add <domain_name> "
5175 "<trust password>\n");
5176 return NT_STATUS_INVALID_PARAMETER;
5180 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5183 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5184 return NT_STATUS_NO_MEMORY;
5187 strupper_m(acct_name);
5189 init_lsa_String(&lsa_acct_name, acct_name);
5191 /* Get samr policy handle */
5192 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5193 pipe_hnd->desthost,
5194 MAXIMUM_ALLOWED_ACCESS,
5195 &connect_pol);
5196 if (!NT_STATUS_IS_OK(result)) {
5197 goto done;
5200 /* Get domain policy handle */
5201 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5202 &connect_pol,
5203 MAXIMUM_ALLOWED_ACCESS,
5204 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5205 &domain_pol);
5206 if (!NT_STATUS_IS_OK(result)) {
5207 goto done;
5210 /* This call can take a long time - allow the server to time out.
5211 * 35 seconds should do it. */
5213 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5215 /* Create trusting domain's account */
5216 acb_info = ACB_NORMAL;
5217 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5218 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5219 SAMR_USER_ACCESS_SET_PASSWORD |
5220 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5221 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5223 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5224 &domain_pol,
5225 &lsa_acct_name,
5226 acb_info,
5227 acct_flags,
5228 &user_pol,
5229 &access_granted,
5230 &user_rid);
5232 /* And restore our original timeout. */
5233 rpccli_set_timeout(pipe_hnd, orig_timeout);
5235 if (!NT_STATUS_IS_OK(result)) {
5236 d_printf("net rpc trustdom add: create user %s failed %s\n",
5237 acct_name, nt_errstr(result));
5238 goto done;
5242 struct samr_CryptPassword crypt_pwd;
5244 ZERO_STRUCT(info.info23);
5246 init_samr_CryptPassword(argv[1],
5247 &cli->user_session_key,
5248 &crypt_pwd);
5250 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5251 SAMR_FIELD_NT_PASSWORD_PRESENT;
5252 info.info23.info.acct_flags = ACB_DOMTRUST;
5253 info.info23.password = crypt_pwd;
5255 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5256 &user_pol,
5258 &info);
5260 if (!NT_STATUS_IS_OK(result)) {
5261 DEBUG(0,("Could not set trust account password: %s\n",
5262 nt_errstr(result)));
5263 goto done;
5267 done:
5268 SAFE_FREE(acct_name);
5269 return result;
5273 * Create interdomain trust account for a remote domain.
5275 * @param argc Standard argc.
5276 * @param argv Standard argv without initial components.
5278 * @return Integer status (0 means success).
5281 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5283 if (argc > 0 && !c->display_usage) {
5284 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5285 rpc_trustdom_add_internals, argc, argv);
5286 } else {
5287 d_printf("Usage:\n"
5288 "net rpc trustdom add <domain_name> <trust password>\n");
5289 return -1;
5295 * Remove interdomain trust account from the RPC server.
5296 * All parameters (except for argc and argv) are passed by run_rpc_command
5297 * function.
5299 * @param c A net_context structure.
5300 * @param domain_sid The domain sid acquired from the server.
5301 * @param cli A cli_state connected to the server.
5302 * @param mem_ctx Talloc context, destroyed on completion of the function.
5303 * @param argc Standard main() style argc.
5304 * @param argv Standard main() style argv. Initial components are already
5305 * stripped.
5307 * @return normal NTSTATUS return code.
5310 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5311 const DOM_SID *domain_sid,
5312 const char *domain_name,
5313 struct cli_state *cli,
5314 struct rpc_pipe_client *pipe_hnd,
5315 TALLOC_CTX *mem_ctx,
5316 int argc,
5317 const char **argv)
5319 struct policy_handle connect_pol, domain_pol, user_pol;
5320 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5321 char *acct_name;
5322 DOM_SID trust_acct_sid;
5323 struct samr_Ids user_rids, name_types;
5324 struct lsa_String lsa_acct_name;
5326 if (argc != 1) {
5327 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5328 return NT_STATUS_INVALID_PARAMETER;
5332 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5334 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5336 if (acct_name == NULL)
5337 return NT_STATUS_NO_MEMORY;
5339 strupper_m(acct_name);
5341 /* Get samr policy handle */
5342 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5343 pipe_hnd->desthost,
5344 MAXIMUM_ALLOWED_ACCESS,
5345 &connect_pol);
5346 if (!NT_STATUS_IS_OK(result)) {
5347 goto done;
5350 /* Get domain policy handle */
5351 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5352 &connect_pol,
5353 MAXIMUM_ALLOWED_ACCESS,
5354 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5355 &domain_pol);
5356 if (!NT_STATUS_IS_OK(result)) {
5357 goto done;
5360 init_lsa_String(&lsa_acct_name, acct_name);
5362 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5363 &domain_pol,
5365 &lsa_acct_name,
5366 &user_rids,
5367 &name_types);
5369 if (!NT_STATUS_IS_OK(result)) {
5370 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5371 acct_name, nt_errstr(result) );
5372 goto done;
5375 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5376 &domain_pol,
5377 MAXIMUM_ALLOWED_ACCESS,
5378 user_rids.ids[0],
5379 &user_pol);
5381 if (!NT_STATUS_IS_OK(result)) {
5382 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5383 acct_name, nt_errstr(result) );
5384 goto done;
5387 /* append the rid to the domain sid */
5388 sid_copy(&trust_acct_sid, domain_sid);
5389 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5390 goto done;
5393 /* remove the sid */
5395 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5396 &user_pol,
5397 &trust_acct_sid);
5398 if (!NT_STATUS_IS_OK(result)) {
5399 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5400 acct_name, nt_errstr(result) );
5401 goto done;
5404 /* Delete user */
5406 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5407 &user_pol);
5409 if (!NT_STATUS_IS_OK(result)) {
5410 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5411 acct_name, nt_errstr(result) );
5412 goto done;
5415 if (!NT_STATUS_IS_OK(result)) {
5416 d_printf("Could not set trust account password: %s\n",
5417 nt_errstr(result));
5418 goto done;
5421 done:
5422 return result;
5426 * Delete interdomain trust account for a remote domain.
5428 * @param argc Standard argc.
5429 * @param argv Standard argv without initial components.
5431 * @return Integer status (0 means success).
5434 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5436 if (argc > 0 && !c->display_usage) {
5437 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5438 rpc_trustdom_del_internals, argc, argv);
5439 } else {
5440 d_printf("Usage:\n"
5441 "net rpc trustdom del <domain>\n");
5442 return -1;
5446 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5447 struct cli_state *cli,
5448 TALLOC_CTX *mem_ctx,
5449 const char *domain_name)
5451 char *dc_name = NULL;
5452 const char *buffer = NULL;
5453 struct rpc_pipe_client *netr;
5454 NTSTATUS status;
5456 /* Use NetServerEnum2 */
5458 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5459 SAFE_FREE(dc_name);
5460 return NT_STATUS_OK;
5463 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5464 for domain %s\n", domain_name));
5466 /* Try netr_GetDcName */
5468 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5469 &netr);
5470 if (!NT_STATUS_IS_OK(status)) {
5471 return status;
5474 status = rpccli_netr_GetDcName(netr, mem_ctx,
5475 cli->desthost,
5476 domain_name,
5477 &buffer,
5478 NULL);
5479 TALLOC_FREE(netr);
5481 if (NT_STATUS_IS_OK(status)) {
5482 return status;
5485 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5486 for domain %s\n", domain_name));
5488 return status;
5492 * Establish trust relationship to a trusting domain.
5493 * Interdomain account must already be created on remote PDC.
5495 * @param c A net_context structure.
5496 * @param argc Standard argc.
5497 * @param argv Standard argv without initial components.
5499 * @return Integer status (0 means success).
5502 static int rpc_trustdom_establish(struct net_context *c, int argc,
5503 const char **argv)
5505 struct cli_state *cli = NULL;
5506 struct sockaddr_storage server_ss;
5507 struct rpc_pipe_client *pipe_hnd = NULL;
5508 struct policy_handle connect_hnd;
5509 TALLOC_CTX *mem_ctx;
5510 NTSTATUS nt_status;
5511 DOM_SID *domain_sid;
5513 char* domain_name;
5514 char* acct_name;
5515 fstring pdc_name;
5516 union lsa_PolicyInformation *info = NULL;
5519 * Connect to \\server\ipc$ as 'our domain' account with password
5522 if (argc != 1 || c->display_usage) {
5523 d_printf("Usage:\n"
5524 "net rpc trustdom establish <domain_name>\n");
5525 return -1;
5528 domain_name = smb_xstrdup(argv[0]);
5529 strupper_m(domain_name);
5531 /* account name used at first is our domain's name with '$' */
5532 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5533 return -1;
5535 strupper_m(acct_name);
5538 * opt_workgroup will be used by connection functions further,
5539 * hence it should be set to remote domain name instead of ours
5541 if (c->opt_workgroup) {
5542 c->opt_workgroup = smb_xstrdup(domain_name);
5545 c->opt_user_name = acct_name;
5547 /* find the domain controller */
5548 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5549 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5550 return -1;
5553 /* connect to ipc$ as username/password */
5554 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5555 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5557 /* Is it trusting domain account for sure ? */
5558 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5559 nt_errstr(nt_status)));
5560 return -1;
5563 /* store who we connected to */
5565 saf_store( domain_name, pdc_name );
5568 * Connect to \\server\ipc$ again (this time anonymously)
5571 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5572 (char*)pdc_name);
5574 if (NT_STATUS_IS_ERR(nt_status)) {
5575 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5576 domain_name, nt_errstr(nt_status)));
5577 return -1;
5580 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5581 "domain %s", domain_name))) {
5582 DEBUG(0, ("talloc_init() failed\n"));
5583 cli_shutdown(cli);
5584 return -1;
5587 /* Make sure we're talking to a proper server */
5589 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5590 if (!NT_STATUS_IS_OK(nt_status)) {
5591 cli_shutdown(cli);
5592 talloc_destroy(mem_ctx);
5593 return -1;
5597 * Call LsaOpenPolicy and LsaQueryInfo
5600 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5601 &pipe_hnd);
5602 if (!NT_STATUS_IS_OK(nt_status)) {
5603 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5604 cli_shutdown(cli);
5605 talloc_destroy(mem_ctx);
5606 return -1;
5609 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5610 &connect_hnd);
5611 if (NT_STATUS_IS_ERR(nt_status)) {
5612 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5613 nt_errstr(nt_status)));
5614 cli_shutdown(cli);
5615 talloc_destroy(mem_ctx);
5616 return -1;
5619 /* Querying info level 5 */
5621 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5622 &connect_hnd,
5623 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5624 &info);
5625 if (NT_STATUS_IS_ERR(nt_status)) {
5626 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5627 nt_errstr(nt_status)));
5628 cli_shutdown(cli);
5629 talloc_destroy(mem_ctx);
5630 return -1;
5633 domain_sid = info->account_domain.sid;
5635 /* There should be actually query info level 3 (following nt serv behaviour),
5636 but I still don't know if it's _really_ necessary */
5639 * Store the password in secrets db
5642 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5643 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5644 cli_shutdown(cli);
5645 talloc_destroy(mem_ctx);
5646 return -1;
5650 * Close the pipes and clean up
5653 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5654 if (NT_STATUS_IS_ERR(nt_status)) {
5655 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5656 nt_errstr(nt_status)));
5657 cli_shutdown(cli);
5658 talloc_destroy(mem_ctx);
5659 return -1;
5662 cli_shutdown(cli);
5664 talloc_destroy(mem_ctx);
5666 d_printf("Trust to domain %s established\n", domain_name);
5667 return 0;
5671 * Revoke trust relationship to the remote domain.
5673 * @param c A net_context structure.
5674 * @param argc Standard argc.
5675 * @param argv Standard argv without initial components.
5677 * @return Integer status (0 means success).
5680 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5681 const char **argv)
5683 char* domain_name;
5684 int rc = -1;
5686 if (argc < 1 || c->display_usage) {
5687 d_printf("Usage:\n"
5688 "net rpc trustdom revoke <domain_name>\n"
5689 " Revoke trust relationship\n"
5690 " domain_name\tName of domain to revoke trust\n");
5691 return -1;
5694 /* generate upper cased domain name */
5695 domain_name = smb_xstrdup(argv[0]);
5696 strupper_m(domain_name);
5698 /* delete password of the trust */
5699 if (!pdb_del_trusteddom_pw(domain_name)) {
5700 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5701 domain_name));
5702 goto done;
5705 rc = 0;
5706 done:
5707 SAFE_FREE(domain_name);
5708 return rc;
5711 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5712 const DOM_SID *domain_sid,
5713 const char *domain_name,
5714 struct cli_state *cli,
5715 struct rpc_pipe_client *pipe_hnd,
5716 TALLOC_CTX *mem_ctx,
5717 int argc,
5718 const char **argv)
5720 fstring str_sid;
5721 sid_to_fstring(str_sid, domain_sid);
5722 d_printf("%s\n", str_sid);
5723 return NT_STATUS_OK;
5726 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5728 fstring ascii_sid, padding;
5729 int pad_len, col_len = 20;
5731 /* convert sid into ascii string */
5732 sid_to_fstring(ascii_sid, dom_sid);
5734 /* calculate padding space for d_printf to look nicer */
5735 pad_len = col_len - strlen(trusted_dom_name);
5736 padding[pad_len] = 0;
5737 do padding[--pad_len] = ' '; while (pad_len);
5739 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5742 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5743 TALLOC_CTX *mem_ctx,
5744 struct policy_handle *pol,
5745 DOM_SID dom_sid,
5746 const char *trusted_dom_name)
5748 NTSTATUS nt_status;
5749 union lsa_TrustedDomainInfo *info = NULL;
5750 char *cleartextpwd = NULL;
5751 uint8_t nt_hash[16];
5752 DATA_BLOB data = data_blob_null;
5754 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5755 pol,
5756 &dom_sid,
5757 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5758 &info);
5759 if (NT_STATUS_IS_ERR(nt_status)) {
5760 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5761 nt_errstr(nt_status)));
5762 goto done;
5765 data = data_blob(info->password.password->data,
5766 info->password.password->length);
5768 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
5769 DEBUG(0, ("Could not retrieve password hash\n"));
5770 goto done;
5773 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
5775 if (cleartextpwd == NULL) {
5776 DEBUG(0,("retrieved NULL password\n"));
5777 nt_status = NT_STATUS_UNSUCCESSFUL;
5778 goto done;
5781 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5782 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5783 nt_status = NT_STATUS_UNSUCCESSFUL;
5784 goto done;
5787 #ifdef DEBUG_PASSWORD
5788 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5789 "password: [%s]\n", trusted_dom_name,
5790 sid_string_dbg(&dom_sid), cleartextpwd));
5791 #endif
5793 done:
5794 SAFE_FREE(cleartextpwd);
5795 data_blob_free(&data);
5797 return nt_status;
5800 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5801 const char **argv)
5803 /* common variables */
5804 TALLOC_CTX* mem_ctx;
5805 struct cli_state *cli = NULL;
5806 struct rpc_pipe_client *pipe_hnd = NULL;
5807 NTSTATUS nt_status;
5808 const char *domain_name = NULL;
5809 DOM_SID *queried_dom_sid;
5810 struct policy_handle connect_hnd;
5811 union lsa_PolicyInformation *info = NULL;
5813 /* trusted domains listing variables */
5814 unsigned int enum_ctx = 0;
5815 int i;
5816 struct lsa_DomainList dom_list;
5817 fstring pdc_name;
5819 if (c->display_usage) {
5820 d_printf("Usage:\n"
5821 "net rpc trustdom vampire\n"
5822 " Vampire trust relationship from remote server\n");
5823 return 0;
5827 * Listing trusted domains (stored in secrets.tdb, if local)
5830 mem_ctx = talloc_init("trust relationships vampire");
5833 * set domain and pdc name to local samba server (default)
5834 * or to remote one given in command line
5837 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5838 domain_name = c->opt_workgroup;
5839 c->opt_target_workgroup = c->opt_workgroup;
5840 } else {
5841 fstrcpy(pdc_name, global_myname());
5842 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5843 c->opt_target_workgroup = domain_name;
5846 /* open \PIPE\lsarpc and open policy handle */
5847 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5848 if (!NT_STATUS_IS_OK(nt_status)) {
5849 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5850 nt_errstr(nt_status)));
5851 talloc_destroy(mem_ctx);
5852 return -1;
5855 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5856 &pipe_hnd);
5857 if (!NT_STATUS_IS_OK(nt_status)) {
5858 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5859 nt_errstr(nt_status) ));
5860 cli_shutdown(cli);
5861 talloc_destroy(mem_ctx);
5862 return -1;
5865 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5866 &connect_hnd);
5867 if (NT_STATUS_IS_ERR(nt_status)) {
5868 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5869 nt_errstr(nt_status)));
5870 cli_shutdown(cli);
5871 talloc_destroy(mem_ctx);
5872 return -1;
5875 /* query info level 5 to obtain sid of a domain being queried */
5876 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5877 &connect_hnd,
5878 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5879 &info);
5881 if (NT_STATUS_IS_ERR(nt_status)) {
5882 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5883 nt_errstr(nt_status)));
5884 cli_shutdown(cli);
5885 talloc_destroy(mem_ctx);
5886 return -1;
5889 queried_dom_sid = info->account_domain.sid;
5892 * Keep calling LsaEnumTrustdom over opened pipe until
5893 * the end of enumeration is reached
5896 d_printf("Vampire trusted domains:\n\n");
5898 do {
5899 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5900 &connect_hnd,
5901 &enum_ctx,
5902 &dom_list,
5903 (uint32_t)-1);
5904 if (NT_STATUS_IS_ERR(nt_status)) {
5905 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5906 nt_errstr(nt_status)));
5907 cli_shutdown(cli);
5908 talloc_destroy(mem_ctx);
5909 return -1;
5912 for (i = 0; i < dom_list.count; i++) {
5914 print_trusted_domain(dom_list.domains[i].sid,
5915 dom_list.domains[i].name.string);
5917 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5918 *dom_list.domains[i].sid,
5919 dom_list.domains[i].name.string);
5920 if (!NT_STATUS_IS_OK(nt_status)) {
5921 cli_shutdown(cli);
5922 talloc_destroy(mem_ctx);
5923 return -1;
5928 * in case of no trusted domains say something rather
5929 * than just display blank line
5931 if (!dom_list.count) d_printf("none\n");
5933 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5935 /* close this connection before doing next one */
5936 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5937 if (NT_STATUS_IS_ERR(nt_status)) {
5938 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5939 nt_errstr(nt_status)));
5940 cli_shutdown(cli);
5941 talloc_destroy(mem_ctx);
5942 return -1;
5945 /* close lsarpc pipe and connection to IPC$ */
5946 cli_shutdown(cli);
5948 talloc_destroy(mem_ctx);
5949 return 0;
5952 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
5954 /* common variables */
5955 TALLOC_CTX* mem_ctx;
5956 struct cli_state *cli = NULL, *remote_cli = NULL;
5957 struct rpc_pipe_client *pipe_hnd = NULL;
5958 NTSTATUS nt_status;
5959 const char *domain_name = NULL;
5960 DOM_SID *queried_dom_sid;
5961 int ascii_dom_name_len;
5962 struct policy_handle connect_hnd;
5963 union lsa_PolicyInformation *info = NULL;
5965 /* trusted domains listing variables */
5966 unsigned int num_domains, enum_ctx = 0;
5967 int i;
5968 struct lsa_DomainList dom_list;
5969 fstring pdc_name;
5970 bool found_domain;
5972 /* trusting domains listing variables */
5973 struct policy_handle domain_hnd;
5974 struct samr_SamArray *trusts = NULL;
5976 if (c->display_usage) {
5977 d_printf("Usage:\n"
5978 "net rpc trustdom list\n"
5979 " List trust relationships\n");
5980 return 0;
5984 * Listing trusted domains (stored in secrets.tdb, if local)
5987 mem_ctx = talloc_init("trust relationships listing");
5990 * set domain and pdc name to local samba server (default)
5991 * or to remote one given in command line
5994 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5995 domain_name = c->opt_workgroup;
5996 c->opt_target_workgroup = c->opt_workgroup;
5997 } else {
5998 fstrcpy(pdc_name, global_myname());
5999 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6000 c->opt_target_workgroup = domain_name;
6003 /* open \PIPE\lsarpc and open policy handle */
6004 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6005 if (!NT_STATUS_IS_OK(nt_status)) {
6006 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6007 nt_errstr(nt_status)));
6008 talloc_destroy(mem_ctx);
6009 return -1;
6012 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6013 &pipe_hnd);
6014 if (!NT_STATUS_IS_OK(nt_status)) {
6015 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6016 nt_errstr(nt_status) ));
6017 cli_shutdown(cli);
6018 talloc_destroy(mem_ctx);
6019 return -1;
6022 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6023 &connect_hnd);
6024 if (NT_STATUS_IS_ERR(nt_status)) {
6025 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6026 nt_errstr(nt_status)));
6027 cli_shutdown(cli);
6028 talloc_destroy(mem_ctx);
6029 return -1;
6032 /* query info level 5 to obtain sid of a domain being queried */
6033 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6034 &connect_hnd,
6035 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6036 &info);
6038 if (NT_STATUS_IS_ERR(nt_status)) {
6039 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6040 nt_errstr(nt_status)));
6041 cli_shutdown(cli);
6042 talloc_destroy(mem_ctx);
6043 return -1;
6046 queried_dom_sid = info->account_domain.sid;
6049 * Keep calling LsaEnumTrustdom over opened pipe until
6050 * the end of enumeration is reached
6053 d_printf("Trusted domains list:\n\n");
6055 found_domain = false;
6057 do {
6058 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6059 &connect_hnd,
6060 &enum_ctx,
6061 &dom_list,
6062 (uint32_t)-1);
6063 if (NT_STATUS_IS_ERR(nt_status)) {
6064 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6065 nt_errstr(nt_status)));
6066 cli_shutdown(cli);
6067 talloc_destroy(mem_ctx);
6068 return -1;
6071 for (i = 0; i < dom_list.count; i++) {
6072 print_trusted_domain(dom_list.domains[i].sid,
6073 dom_list.domains[i].name.string);
6074 found_domain = true;
6077 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6080 * in case of no trusted domains say something rather
6081 * than just display blank line
6083 if (!found_domain) {
6084 d_printf("none\n");
6087 /* close this connection before doing next one */
6088 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6089 if (NT_STATUS_IS_ERR(nt_status)) {
6090 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6091 nt_errstr(nt_status)));
6092 cli_shutdown(cli);
6093 talloc_destroy(mem_ctx);
6094 return -1;
6097 TALLOC_FREE(pipe_hnd);
6100 * Listing trusting domains (stored in passdb backend, if local)
6103 d_printf("\nTrusting domains list:\n\n");
6106 * Open \PIPE\samr and get needed policy handles
6108 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6109 &pipe_hnd);
6110 if (!NT_STATUS_IS_OK(nt_status)) {
6111 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6112 cli_shutdown(cli);
6113 talloc_destroy(mem_ctx);
6114 return -1;
6117 /* SamrConnect2 */
6118 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6119 pipe_hnd->desthost,
6120 SAMR_ACCESS_LOOKUP_DOMAIN,
6121 &connect_hnd);
6122 if (!NT_STATUS_IS_OK(nt_status)) {
6123 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6124 nt_errstr(nt_status)));
6125 cli_shutdown(cli);
6126 talloc_destroy(mem_ctx);
6127 return -1;
6130 /* SamrOpenDomain - we have to open domain policy handle in order to be
6131 able to enumerate accounts*/
6132 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6133 &connect_hnd,
6134 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6135 queried_dom_sid,
6136 &domain_hnd);
6137 if (!NT_STATUS_IS_OK(nt_status)) {
6138 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6139 nt_errstr(nt_status)));
6140 cli_shutdown(cli);
6141 talloc_destroy(mem_ctx);
6142 return -1;
6146 * perform actual enumeration
6149 found_domain = false;
6151 enum_ctx = 0; /* reset enumeration context from last enumeration */
6152 do {
6154 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6155 &domain_hnd,
6156 &enum_ctx,
6157 ACB_DOMTRUST,
6158 &trusts,
6159 0xffff,
6160 &num_domains);
6161 if (NT_STATUS_IS_ERR(nt_status)) {
6162 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6163 nt_errstr(nt_status)));
6164 cli_shutdown(cli);
6165 talloc_destroy(mem_ctx);
6166 return -1;
6169 for (i = 0; i < num_domains; i++) {
6171 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6173 found_domain = true;
6176 * get each single domain's sid (do we _really_ need this ?):
6177 * 1) connect to domain's pdc
6178 * 2) query the pdc for domain's sid
6181 /* get rid of '$' tail */
6182 ascii_dom_name_len = strlen(str);
6183 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6184 str[ascii_dom_name_len - 1] = '\0';
6186 /* set opt_* variables to remote domain */
6187 strupper_m(str);
6188 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6189 c->opt_target_workgroup = c->opt_workgroup;
6191 d_printf("%-20s", str);
6193 /* connect to remote domain controller */
6194 nt_status = net_make_ipc_connection(c,
6195 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6196 &remote_cli);
6197 if (NT_STATUS_IS_OK(nt_status)) {
6198 /* query for domain's sid */
6199 if (run_rpc_command(
6200 c, remote_cli,
6201 &ndr_table_lsarpc.syntax_id, 0,
6202 rpc_query_domain_sid, argc,
6203 argv))
6204 d_fprintf(stderr, "couldn't get domain's sid\n");
6206 cli_shutdown(remote_cli);
6208 } else {
6209 d_fprintf(stderr, "domain controller is not "
6210 "responding: %s\n",
6211 nt_errstr(nt_status));
6215 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6217 if (!found_domain) {
6218 d_printf("none\n");
6221 /* close opened samr and domain policy handles */
6222 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6223 if (!NT_STATUS_IS_OK(nt_status)) {
6224 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6227 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6228 if (!NT_STATUS_IS_OK(nt_status)) {
6229 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6232 /* close samr pipe and connection to IPC$ */
6233 cli_shutdown(cli);
6235 talloc_destroy(mem_ctx);
6236 return 0;
6240 * Entrypoint for 'net rpc trustdom' code.
6242 * @param argc Standard argc.
6243 * @param argv Standard argv without initial components.
6245 * @return Integer status (0 means success).
6248 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6250 struct functable func[] = {
6252 "add",
6253 rpc_trustdom_add,
6254 NET_TRANSPORT_RPC,
6255 "Add trusted domain's account",
6256 "net rpc trustdom add\n"
6257 " Add trusted domain's account"
6260 "del",
6261 rpc_trustdom_del,
6262 NET_TRANSPORT_RPC,
6263 "Remove trusted domain's account",
6264 "net rpc trustdom del\n"
6265 " Remove trusted domain's account"
6268 "establish",
6269 rpc_trustdom_establish,
6270 NET_TRANSPORT_RPC,
6271 "Establish trust relationship",
6272 "net rpc trustdom establish\n"
6273 " Establish trust relationship"
6276 "revoke",
6277 rpc_trustdom_revoke,
6278 NET_TRANSPORT_RPC,
6279 "Revoke trust relationship",
6280 "net rpc trustdom revoke\n"
6281 " Revoke trust relationship"
6284 "list",
6285 rpc_trustdom_list,
6286 NET_TRANSPORT_RPC,
6287 "List domain trusts",
6288 "net rpc trustdom list\n"
6289 " List domain trusts"
6292 "vampire",
6293 rpc_trustdom_vampire,
6294 NET_TRANSPORT_RPC,
6295 "Vampire trusts from remote server",
6296 "net rpc trustdom vampire\n"
6297 " Vampire trusts from remote server"
6299 {NULL, NULL, 0, NULL, NULL}
6302 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6306 * Check if a server will take rpc commands
6307 * @param flags Type of server to connect to (PDC, DMB, localhost)
6308 * if the host is not explicitly specified
6309 * @return bool (true means rpc supported)
6311 bool net_rpc_check(struct net_context *c, unsigned flags)
6313 struct cli_state *cli;
6314 bool ret = false;
6315 struct sockaddr_storage server_ss;
6316 char *server_name = NULL;
6317 NTSTATUS status;
6319 /* flags (i.e. server type) may depend on command */
6320 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6321 return false;
6323 if ((cli = cli_initialise()) == NULL) {
6324 return false;
6327 status = cli_connect(cli, server_name, &server_ss);
6328 if (!NT_STATUS_IS_OK(status))
6329 goto done;
6330 if (!attempt_netbios_session_request(&cli, global_myname(),
6331 server_name, &server_ss))
6332 goto done;
6333 status = cli_negprot(cli);
6334 if (!NT_STATUS_IS_OK(status))
6335 goto done;
6336 if (cli->protocol < PROTOCOL_NT1)
6337 goto done;
6339 ret = true;
6340 done:
6341 cli_shutdown(cli);
6342 return ret;
6345 /* dump sam database via samsync rpc calls */
6346 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6347 if (c->display_usage) {
6348 d_printf("Usage:\n"
6349 "net rpc samdump\n"
6350 " Dump remote SAM database\n");
6351 return 0;
6354 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6355 NET_FLAGS_ANONYMOUS,
6356 rpc_samdump_internals, argc, argv);
6359 /* syncronise sam database via samsync rpc calls */
6360 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6362 struct functable func[] = {
6364 "ldif",
6365 rpc_vampire_ldif,
6366 NET_TRANSPORT_RPC,
6367 "Dump remote SAM database to ldif",
6368 "net rpc vampire ldif\n"
6369 " Dump remote SAM database to LDIF file or stdout"
6372 "keytab",
6373 rpc_vampire_keytab,
6374 NET_TRANSPORT_RPC,
6375 "Dump remote SAM database to Kerberos Keytab",
6376 "net rpc vampire keytab\n"
6377 " Dump remote SAM database to Kerberos keytab file"
6380 "passdb",
6381 rpc_vampire_passdb,
6382 NET_TRANSPORT_RPC,
6383 "Dump remote SAM database to passdb",
6384 "net rpc vampire passdb\n"
6385 " Dump remote SAM database to passdb"
6388 {NULL, NULL, 0, NULL, NULL}
6391 if (argc == 0) {
6392 if (c->display_usage) {
6393 d_printf("Usage:\n"
6394 "net rpc vampire\n"
6395 " Vampire remote SAM database\n");
6396 return 0;
6399 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6400 NET_FLAGS_ANONYMOUS,
6401 rpc_vampire_internals,
6402 argc, argv);
6405 return net_run_function(c, argc, argv, "net rpc vampire", func);
6409 * Migrate everything from a print server.
6411 * @param c A net_context structure.
6412 * @param argc Standard main() style argc.
6413 * @param argv Standard main() style argv. Initial components are already
6414 * stripped.
6416 * @return A shell status integer (0 for success).
6418 * The order is important !
6419 * To successfully add drivers the print queues have to exist !
6420 * Applying ACLs should be the last step, because you're easily locked out.
6423 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6424 const char **argv)
6426 int ret;
6428 if (c->display_usage) {
6429 d_printf("Usage:\n"
6430 "net rpc printer migrate all\n"
6431 " Migrate everything from a print server\n");
6432 return 0;
6435 if (!c->opt_host) {
6436 d_printf("no server to migrate\n");
6437 return -1;
6440 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6441 rpc_printer_migrate_printers_internals, argc,
6442 argv);
6443 if (ret)
6444 return ret;
6446 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6447 rpc_printer_migrate_drivers_internals, argc,
6448 argv);
6449 if (ret)
6450 return ret;
6452 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6453 rpc_printer_migrate_forms_internals, argc, argv);
6454 if (ret)
6455 return ret;
6457 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6458 rpc_printer_migrate_settings_internals, argc,
6459 argv);
6460 if (ret)
6461 return ret;
6463 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6464 rpc_printer_migrate_security_internals, argc,
6465 argv);
6470 * Migrate print drivers from a print server.
6472 * @param c A net_context structure.
6473 * @param argc Standard main() style argc.
6474 * @param argv Standard main() style argv. Initial components are already
6475 * stripped.
6477 * @return A shell status integer (0 for success).
6479 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6480 const char **argv)
6482 if (c->display_usage) {
6483 d_printf("Usage:\n"
6484 "net rpc printer migrate drivers\n"
6485 " Migrate print-drivers from a print-server\n");
6486 return 0;
6489 if (!c->opt_host) {
6490 d_printf("no server to migrate\n");
6491 return -1;
6494 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6495 rpc_printer_migrate_drivers_internals,
6496 argc, argv);
6500 * Migrate print-forms from a print-server.
6502 * @param c A net_context structure.
6503 * @param argc Standard main() style argc.
6504 * @param argv Standard main() style argv. Initial components are already
6505 * stripped.
6507 * @return A shell status integer (0 for success).
6509 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6510 const char **argv)
6512 if (c->display_usage) {
6513 d_printf("Usage:\n"
6514 "net rpc printer migrate forms\n"
6515 " Migrate print-forms from a print-server\n");
6516 return 0;
6519 if (!c->opt_host) {
6520 d_printf("no server to migrate\n");
6521 return -1;
6524 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6525 rpc_printer_migrate_forms_internals,
6526 argc, argv);
6530 * Migrate printers from a print-server.
6532 * @param c A net_context structure.
6533 * @param argc Standard main() style argc.
6534 * @param argv Standard main() style argv. Initial components are already
6535 * stripped.
6537 * @return A shell status integer (0 for success).
6539 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6540 const char **argv)
6542 if (c->display_usage) {
6543 d_printf("Usage:\n"
6544 "net rpc printer migrate printers\n"
6545 " Migrate printers from a print-server\n");
6546 return 0;
6549 if (!c->opt_host) {
6550 d_printf("no server to migrate\n");
6551 return -1;
6554 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6555 rpc_printer_migrate_printers_internals,
6556 argc, argv);
6560 * Migrate printer-ACLs from a print-server
6562 * @param c A net_context structure.
6563 * @param argc Standard main() style argc.
6564 * @param argv Standard main() style argv. Initial components are already
6565 * stripped.
6567 * @return A shell status integer (0 for success).
6569 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6570 const char **argv)
6572 if (c->display_usage) {
6573 d_printf("Usage:\n"
6574 "net rpc printer migrate security\n"
6575 " Migrate printer-ACLs from a print-server\n");
6576 return 0;
6579 if (!c->opt_host) {
6580 d_printf("no server to migrate\n");
6581 return -1;
6584 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6585 rpc_printer_migrate_security_internals,
6586 argc, argv);
6590 * Migrate printer-settings from a print-server.
6592 * @param c A net_context structure.
6593 * @param argc Standard main() style argc.
6594 * @param argv Standard main() style argv. Initial components are already
6595 * stripped.
6597 * @return A shell status integer (0 for success).
6599 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6600 const char **argv)
6602 if (c->display_usage) {
6603 d_printf("Usage:\n"
6604 "net rpc printer migrate settings\n"
6605 " Migrate printer-settings from a print-server\n");
6606 return 0;
6609 if (!c->opt_host) {
6610 d_printf("no server to migrate\n");
6611 return -1;
6614 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6615 rpc_printer_migrate_settings_internals,
6616 argc, argv);
6620 * 'net rpc printer' entrypoint.
6622 * @param c A net_context structure.
6623 * @param argc Standard main() style argc.
6624 * @param argv Standard main() style argv. Initial components are already
6625 * stripped.
6628 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6631 /* ouch: when addriver and setdriver are called from within
6632 rpc_printer_migrate_drivers_internals, the printer-queue already
6633 *has* to exist */
6635 struct functable func[] = {
6637 "all",
6638 rpc_printer_migrate_all,
6639 NET_TRANSPORT_RPC,
6640 "Migrate all from remote to local print server",
6641 "net rpc printer migrate all\n"
6642 " Migrate all from remote to local print server"
6645 "drivers",
6646 rpc_printer_migrate_drivers,
6647 NET_TRANSPORT_RPC,
6648 "Migrate drivers to local server",
6649 "net rpc printer migrate drivers\n"
6650 " Migrate drivers to local server"
6653 "forms",
6654 rpc_printer_migrate_forms,
6655 NET_TRANSPORT_RPC,
6656 "Migrate froms to local server",
6657 "net rpc printer migrate forms\n"
6658 " Migrate froms to local server"
6661 "printers",
6662 rpc_printer_migrate_printers,
6663 NET_TRANSPORT_RPC,
6664 "Migrate printers to local server",
6665 "net rpc printer migrate printers\n"
6666 " Migrate printers to local server"
6669 "security",
6670 rpc_printer_migrate_security,
6671 NET_TRANSPORT_RPC,
6672 "Mirgate printer ACLs to local server",
6673 "net rpc printer migrate security\n"
6674 " Mirgate printer ACLs to local server"
6677 "settings",
6678 rpc_printer_migrate_settings,
6679 NET_TRANSPORT_RPC,
6680 "Migrate printer settings to local server",
6681 "net rpc printer migrate settings\n"
6682 " Migrate printer settings to local server"
6684 {NULL, NULL, 0, NULL, NULL}
6687 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6692 * List printers on a remote RPC server.
6694 * @param c A net_context structure.
6695 * @param argc Standard main() style argc.
6696 * @param argv Standard main() style argv. Initial components are already
6697 * stripped.
6699 * @return A shell status integer (0 for success).
6701 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6703 if (c->display_usage) {
6704 d_printf("Usage:\n"
6705 "net rpc printer list\n"
6706 " List printers on a remote RPC server\n");
6707 return 0;
6710 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6711 rpc_printer_list_internals,
6712 argc, argv);
6716 * List printer-drivers on a remote RPC server.
6718 * @param c A net_context structure.
6719 * @param argc Standard main() style argc.
6720 * @param argv Standard main() style argv. Initial components are already
6721 * stripped.
6723 * @return A shell status integer (0 for success).
6725 static int rpc_printer_driver_list(struct net_context *c, int argc,
6726 const char **argv)
6728 if (c->display_usage) {
6729 d_printf("Usage:\n"
6730 "net rpc printer driver\n"
6731 " List printer-drivers on a remote RPC server\n");
6732 return 0;
6735 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6736 rpc_printer_driver_list_internals,
6737 argc, argv);
6741 * Publish printer in ADS via MSRPC.
6743 * @param c A net_context structure.
6744 * @param argc Standard main() style argc.
6745 * @param argv Standard main() style argv. Initial components are already
6746 * stripped.
6748 * @return A shell status integer (0 for success).
6750 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6751 const char **argv)
6753 if (c->display_usage) {
6754 d_printf("Usage:\n"
6755 "net rpc printer publish publish\n"
6756 " Publish printer in ADS via MSRPC\n");
6757 return 0;
6760 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6761 rpc_printer_publish_publish_internals,
6762 argc, argv);
6766 * Update printer in ADS via MSRPC.
6768 * @param c A net_context structure.
6769 * @param argc Standard main() style argc.
6770 * @param argv Standard main() style argv. Initial components are already
6771 * stripped.
6773 * @return A shell status integer (0 for success).
6775 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6777 if (c->display_usage) {
6778 d_printf("Usage:\n"
6779 "net rpc printer publish update\n"
6780 " Update printer in ADS via MSRPC\n");
6781 return 0;
6784 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6785 rpc_printer_publish_update_internals,
6786 argc, argv);
6790 * UnPublish printer in ADS via MSRPC.
6792 * @param c A net_context structure.
6793 * @param argc Standard main() style argc.
6794 * @param argv Standard main() style argv. Initial components are already
6795 * stripped.
6797 * @return A shell status integer (0 for success).
6799 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6800 const char **argv)
6802 if (c->display_usage) {
6803 d_printf("Usage:\n"
6804 "net rpc printer publish unpublish\n"
6805 " UnPublish printer in ADS via MSRPC\n");
6806 return 0;
6809 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6810 rpc_printer_publish_unpublish_internals,
6811 argc, argv);
6815 * List published printers via MSRPC.
6817 * @param c A net_context structure.
6818 * @param argc Standard main() style argc.
6819 * @param argv Standard main() style argv. Initial components are already
6820 * stripped.
6822 * @return A shell status integer (0 for success).
6824 static int rpc_printer_publish_list(struct net_context *c, int argc,
6825 const char **argv)
6827 if (c->display_usage) {
6828 d_printf("Usage:\n"
6829 "net rpc printer publish list\n"
6830 " List published printers via MSRPC\n");
6831 return 0;
6834 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6835 rpc_printer_publish_list_internals,
6836 argc, argv);
6841 * Publish printer in ADS.
6843 * @param c A net_context structure.
6844 * @param argc Standard main() style argc.
6845 * @param argv Standard main() style argv. Initial components are already
6846 * stripped.
6848 * @return A shell status integer (0 for success).
6850 static int rpc_printer_publish(struct net_context *c, int argc,
6851 const char **argv)
6854 struct functable func[] = {
6856 "publish",
6857 rpc_printer_publish_publish,
6858 NET_TRANSPORT_RPC,
6859 "Publish printer in AD",
6860 "net rpc printer publish publish\n"
6861 " Publish printer in AD"
6864 "update",
6865 rpc_printer_publish_update,
6866 NET_TRANSPORT_RPC,
6867 "Update printer in AD",
6868 "net rpc printer publish update\n"
6869 " Update printer in AD"
6872 "unpublish",
6873 rpc_printer_publish_unpublish,
6874 NET_TRANSPORT_RPC,
6875 "Unpublish printer",
6876 "net rpc printer publish unpublish\n"
6877 " Unpublish printer"
6880 "list",
6881 rpc_printer_publish_list,
6882 NET_TRANSPORT_RPC,
6883 "List published printers",
6884 "net rpc printer publish list\n"
6885 " List published printers"
6887 {NULL, NULL, 0, NULL, NULL}
6890 if (argc == 0) {
6891 if (c->display_usage) {
6892 d_printf("Usage:\n");
6893 d_printf("net rpc printer publish\n"
6894 " List published printers\n"
6895 " Alias of net rpc printer publish list\n");
6896 net_display_usage_from_functable(func);
6897 return 0;
6899 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6900 rpc_printer_publish_list_internals,
6901 argc, argv);
6904 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6910 * Display rpc printer help page.
6912 * @param c A net_context structure.
6913 * @param argc Standard main() style argc.
6914 * @param argv Standard main() style argv. Initial components are already
6915 * stripped.
6917 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6919 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
6920 "\tlists all printers on print-server\n\n");
6921 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6922 "\tlists all printer-drivers on print-server\n\n");
6923 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6924 "\tpublishes printer settings in Active Directory\n"
6925 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6926 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6927 "\n\tmigrates printers from remote to local server\n\n");
6928 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6929 "\n\tmigrates printer-settings from remote to local server\n\n");
6930 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6931 "\n\tmigrates printer-drivers from remote to local server\n\n");
6932 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6933 "\n\tmigrates printer-forms from remote to local server\n\n");
6934 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6935 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6936 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6937 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6938 "\tremote to local print-server\n\n");
6939 net_common_methods_usage(c, argc, argv);
6940 net_common_flags_usage(c, argc, argv);
6941 d_printf(
6942 "\t-v or --verbose\t\t\tgive verbose output\n"
6943 "\t --destination\t\tmigration target server (default: localhost)\n");
6945 return -1;
6949 * 'net rpc printer' entrypoint.
6951 * @param c A net_context structure.
6952 * @param argc Standard main() style argc.
6953 * @param argv Standard main() style argv. Initial components are already
6954 * stripped.
6956 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
6958 struct functable func[] = {
6960 "list",
6961 rpc_printer_list,
6962 NET_TRANSPORT_RPC,
6963 "List all printers on print server",
6964 "net rpc printer list\n"
6965 " List all printers on print server"
6968 "migrate",
6969 rpc_printer_migrate,
6970 NET_TRANSPORT_RPC,
6971 "Migrate printer to local server",
6972 "net rpc printer migrate\n"
6973 " Migrate printer to local server"
6976 "driver",
6977 rpc_printer_driver_list,
6978 NET_TRANSPORT_RPC,
6979 "List printer drivers",
6980 "net rpc printer driver\n"
6981 " List printer drivers"
6984 "publish",
6985 rpc_printer_publish,
6986 NET_TRANSPORT_RPC,
6987 "Publish printer in AD",
6988 "net rpc printer publish\n"
6989 " Publish printer in AD"
6991 {NULL, NULL, 0, NULL, NULL}
6994 if (argc == 0) {
6995 if (c->display_usage) {
6996 d_printf("Usage:\n");
6997 d_printf("net rpc printer\n"
6998 " List printers\n");
6999 net_display_usage_from_functable(func);
7000 return 0;
7002 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7003 rpc_printer_list_internals,
7004 argc, argv);
7007 return net_run_function(c, argc, argv, "net rpc printer", func);
7011 * 'net rpc' entrypoint.
7013 * @param c A net_context structure.
7014 * @param argc Standard main() style argc.
7015 * @param argv Standard main() style argv. Initial components are already
7016 * stripped.
7019 int net_rpc(struct net_context *c, int argc, const char **argv)
7021 NET_API_STATUS status;
7023 struct functable func[] = {
7025 "audit",
7026 net_rpc_audit,
7027 NET_TRANSPORT_RPC,
7028 "Modify global audit settings",
7029 "net rpc audit\n"
7030 " Modify global audit settings"
7033 "info",
7034 net_rpc_info,
7035 NET_TRANSPORT_RPC,
7036 "Show basic info about a domain",
7037 "net rpc info\n"
7038 " Show basic info about a domain"
7041 "join",
7042 net_rpc_join,
7043 NET_TRANSPORT_RPC,
7044 "Join a domain",
7045 "net rpc join\n"
7046 " Join a domain"
7049 "oldjoin",
7050 net_rpc_oldjoin,
7051 NET_TRANSPORT_RPC,
7052 "Join a domain created in server manager",
7053 "net rpc oldjoin\n"
7054 " Join a domain created in server manager"
7057 "testjoin",
7058 net_rpc_testjoin,
7059 NET_TRANSPORT_RPC,
7060 "Test that a join is valid",
7061 "net rpc testjoin\n"
7062 " Test that a join is valid"
7065 "user",
7066 net_rpc_user,
7067 NET_TRANSPORT_RPC,
7068 "List/modify users",
7069 "net rpc user\n"
7070 " List/modify users"
7073 "password",
7074 rpc_user_password,
7075 NET_TRANSPORT_RPC,
7076 "Change a user password",
7077 "net rpc password\n"
7078 " Change a user password\n"
7079 " Alias for net rpc user password"
7082 "group",
7083 net_rpc_group,
7084 NET_TRANSPORT_RPC,
7085 "List/modify groups",
7086 "net rpc group\n"
7087 " List/modify groups"
7090 "share",
7091 net_rpc_share,
7092 NET_TRANSPORT_RPC,
7093 "List/modify shares",
7094 "net rpc share\n"
7095 " List/modify shares"
7098 "file",
7099 net_rpc_file,
7100 NET_TRANSPORT_RPC,
7101 "List open files",
7102 "net rpc file\n"
7103 " List open files"
7106 "printer",
7107 net_rpc_printer,
7108 NET_TRANSPORT_RPC,
7109 "List/modify printers",
7110 "net rpc printer\n"
7111 " List/modify printers"
7114 "changetrustpw",
7115 net_rpc_changetrustpw,
7116 NET_TRANSPORT_RPC,
7117 "Change trust account password",
7118 "net rpc changetrustpw\n"
7119 " Change trust account password"
7122 "trustdom",
7123 rpc_trustdom,
7124 NET_TRANSPORT_RPC,
7125 "Modify domain trusts",
7126 "net rpc trustdom\n"
7127 " Modify domain trusts"
7130 "abortshutdown",
7131 rpc_shutdown_abort,
7132 NET_TRANSPORT_RPC,
7133 "Abort a remote shutdown",
7134 "net rpc abortshutdown\n"
7135 " Abort a remote shutdown"
7138 "shutdown",
7139 rpc_shutdown,
7140 NET_TRANSPORT_RPC,
7141 "Shutdown a remote server",
7142 "net rpc shutdown\n"
7143 " Shutdown a remote server"
7146 "samdump",
7147 rpc_samdump,
7148 NET_TRANSPORT_RPC,
7149 "Dump SAM data of remote NT PDC",
7150 "net rpc samdump\n"
7151 " Dump SAM data of remote NT PDC"
7154 "vampire",
7155 rpc_vampire,
7156 NET_TRANSPORT_RPC,
7157 "Sync a remote NT PDC's data into local passdb",
7158 "net rpc vampire\n"
7159 " Sync a remote NT PDC's data into local passdb"
7162 "getsid",
7163 net_rpc_getsid,
7164 NET_TRANSPORT_RPC,
7165 "Fetch the domain sid into local secrets.tdb",
7166 "net rpc getsid\n"
7167 " Fetch the domain sid into local secrets.tdb"
7170 "rights",
7171 net_rpc_rights,
7172 NET_TRANSPORT_RPC,
7173 "Manage privileges assigned to SID",
7174 "net rpc rights\n"
7175 " Manage privileges assigned to SID"
7178 "service",
7179 net_rpc_service,
7180 NET_TRANSPORT_RPC,
7181 "Start/stop/query remote services",
7182 "net rpc service\n"
7183 " Start/stop/query remote services"
7186 "registry",
7187 net_rpc_registry,
7188 NET_TRANSPORT_RPC,
7189 "Manage registry hives",
7190 "net rpc registry\n"
7191 " Manage registry hives"
7194 "shell",
7195 net_rpc_shell,
7196 NET_TRANSPORT_RPC,
7197 "Open interactive shell on remote server",
7198 "net rpc shell\n"
7199 " Open interactive shell on remote server"
7201 {NULL, NULL, 0, NULL, NULL}
7204 status = libnetapi_init(&c->netapi_ctx);
7205 if (status != 0) {
7206 return -1;
7208 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7209 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7210 if (c->opt_kerberos) {
7211 libnetapi_set_use_kerberos(c->netapi_ctx);
7214 return net_run_function(c, argc, argv, "net rpc", func);