source: branches/samba-3.2.x/source/utils/net_rpc.c

Last change on this file was 335, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 to 3.2.14 (final)

File size: 178.4 KB
Line 
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)
9
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.
14
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.
19
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/>. */
22
23#include "includes.h"
24#include "utils/net.h"
25
26static int net_mode_share;
27static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
28
29/**
30 * @file net_rpc.c
31 *
32 * @brief RPC based subcommands for the 'net' utility.
33 *
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.)
38 *
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
41 **/
42
43
44/**
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
47 *
48 * @param cli A cli_state already connected to the remote machine
49 *
50 * @return The Domain SID of the remote machine.
51 **/
52
53NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
54 DOM_SID **domain_sid,
55 const char **domain_name)
56{
57 struct rpc_pipe_client *lsa_pipe;
58 POLICY_HND pol;
59 NTSTATUS result = NT_STATUS_OK;
60 union lsa_PolicyInformation *info = NULL;
61
62 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
63 if (!lsa_pipe) {
64 d_fprintf(stderr, "Could not initialise lsa pipe\n");
65 return result;
66 }
67
68 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False,
69 SEC_RIGHTS_MAXIMUM_ALLOWED,
70 &pol);
71 if (!NT_STATUS_IS_OK(result)) {
72 d_fprintf(stderr, "open_policy failed: %s\n",
73 nt_errstr(result));
74 return result;
75 }
76
77 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
78 &pol,
79 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
80 &info);
81 if (!NT_STATUS_IS_OK(result)) {
82 d_fprintf(stderr, "lsaquery failed: %s\n",
83 nt_errstr(result));
84 return result;
85 }
86
87 *domain_name = info->account_domain.name.string;
88 *domain_sid = info->account_domain.sid;
89
90 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
91 cli_rpc_pipe_close(lsa_pipe);
92
93 return NT_STATUS_OK;
94}
95
96/**
97 * Run a single RPC command, from start to finish.
98 *
99 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
100 * @param conn_flag a NET_FLAG_ combination. Passed to
101 * net_make_ipc_connection.
102 * @param argc Standard main() style argc.
103 * @param argv Standard main() style argv. Initial components are already
104 * stripped.
105 * @return A shell status integer (0 for success).
106 */
107
108int run_rpc_command(struct cli_state *cli_arg,
109 const int pipe_idx,
110 int conn_flags,
111 rpc_command_fn fn,
112 int argc,
113 const char **argv)
114{
115 struct cli_state *cli = NULL;
116 struct rpc_pipe_client *pipe_hnd = NULL;
117 TALLOC_CTX *mem_ctx;
118 NTSTATUS nt_status;
119 DOM_SID *domain_sid;
120 const char *domain_name;
121 int ret = -1;
122
123 /* make use of cli_state handed over as an argument, if possible */
124 if (!cli_arg) {
125 nt_status = net_make_ipc_connection(conn_flags, &cli);
126 if (!NT_STATUS_IS_OK(nt_status)) {
127 DEBUG(1, ("failed to make ipc connection: %s\n",
128 nt_errstr(nt_status)));
129 return -1;
130 }
131 } else {
132 cli = cli_arg;
133 }
134
135 if (!cli) {
136 return -1;
137 }
138
139 /* Create mem_ctx */
140
141 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
142 DEBUG(0, ("talloc_init() failed\n"));
143 goto fail;
144 }
145
146 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
147 &domain_name);
148 if (!NT_STATUS_IS_OK(nt_status)) {
149 goto fail;
150 }
151
152 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
153 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
154 /* Always try and create an schannel netlogon pipe. */
155 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
156 PIPE_AUTH_LEVEL_PRIVACY,
157 domain_name,
158 &nt_status);
159 if (!pipe_hnd) {
160 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
161 nt_errstr(nt_status) ));
162 goto fail;
163 }
164 } else {
165 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
166 if (!pipe_hnd) {
167 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
168 cli_get_pipe_name(pipe_idx),
169 nt_errstr(nt_status) ));
170 goto fail;
171 }
172 }
173 }
174
175 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
176
177 if (!NT_STATUS_IS_OK(nt_status)) {
178 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
179 } else {
180 ret = 0;
181 DEBUG(5, ("rpc command function succedded\n"));
182 }
183
184 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
185 if (pipe_hnd) {
186 cli_rpc_pipe_close(pipe_hnd);
187 }
188 }
189
190fail:
191 /* close the connection only if it was opened here */
192 if (!cli_arg) {
193 cli_shutdown(cli);
194 }
195
196 talloc_destroy(mem_ctx);
197 return ret;
198}
199
200/**
201 * Force a change of the trust acccount password.
202 *
203 * All parameters are provided by the run_rpc_command function, except for
204 * argc, argv which are passed through.
205 *
206 * @param domain_sid The domain sid acquired from the remote server
207 * @param cli A cli_state connected to the server.
208 * @param mem_ctx Talloc context, destroyed on completion of the function.
209 * @param argc Standard main() style argc.
210 * @param argv Standard main() style argv. Initial components are already
211 * stripped.
212 *
213 * @return Normal NTSTATUS return.
214 **/
215
216static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
217 const char *domain_name,
218 struct cli_state *cli,
219 struct rpc_pipe_client *pipe_hnd,
220 TALLOC_CTX *mem_ctx,
221 int argc,
222 const char **argv)
223{
224
225 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
226}
227
228/**
229 * Force a change of the trust acccount password.
230 *
231 * @param argc Standard main() style argc.
232 * @param argv Standard main() style argv. Initial components are already
233 * stripped.
234 *
235 * @return A shell status integer (0 for success).
236 **/
237
238int net_rpc_changetrustpw(int argc, const char **argv)
239{
240 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
241 rpc_changetrustpw_internals,
242 argc, argv);
243}
244
245/**
246 * Join a domain, the old way.
247 *
248 * This uses 'machinename' as the inital password, and changes it.
249 *
250 * The password should be created with 'server manager' or equiv first.
251 *
252 * All parameters are provided by the run_rpc_command function, except for
253 * argc, argv which are passed through.
254 *
255 * @param domain_sid The domain sid acquired from the remote server.
256 * @param cli A cli_state connected to the server.
257 * @param mem_ctx Talloc context, destroyed on completion of the function.
258 * @param argc Standard main() style argc.
259 * @param argv Standard main() style argv. Initial components are already
260 * stripped.
261 *
262 * @return Normal NTSTATUS return.
263 **/
264
265static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
266 const char *domain_name,
267 struct cli_state *cli,
268 struct rpc_pipe_client *pipe_hnd,
269 TALLOC_CTX *mem_ctx,
270 int argc,
271 const char **argv)
272{
273
274 fstring trust_passwd;
275 unsigned char orig_trust_passwd_hash[16];
276 NTSTATUS result;
277 uint32 sec_channel_type;
278
279 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
280 if (!pipe_hnd) {
281 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
282 "error was %s\n",
283 cli->desthost,
284 nt_errstr(result) ));
285 return result;
286 }
287
288 /*
289 check what type of join - if the user want's to join as
290 a BDC, the server must agree that we are a BDC.
291 */
292 if (argc >= 0) {
293 sec_channel_type = get_sec_channel_type(argv[0]);
294 } else {
295 sec_channel_type = get_sec_channel_type(NULL);
296 }
297
298 fstrcpy(trust_passwd, global_myname());
299 strlower_m(trust_passwd);
300
301 /*
302 * Machine names can be 15 characters, but the max length on
303 * a password is 14. --jerry
304 */
305
306 trust_passwd[14] = '\0';
307
308 E_md4hash(trust_passwd, orig_trust_passwd_hash);
309
310 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
311 orig_trust_passwd_hash,
312 sec_channel_type);
313
314 if (NT_STATUS_IS_OK(result))
315 printf("Joined domain %s.\n",opt_target_workgroup);
316
317
318 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
319 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
320 result = NT_STATUS_UNSUCCESSFUL;
321 }
322
323 return result;
324}
325
326/**
327 * Join a domain, the old way.
328 *
329 * @param argc Standard main() style argc.
330 * @param argv Standard main() style argv. Initial components are already
331 * stripped.
332 *
333 * @return A shell status integer (0 for success)
334 **/
335
336static int net_rpc_perform_oldjoin(int argc, const char **argv)
337{
338 return run_rpc_command(NULL, PI_NETLOGON,
339 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
340 rpc_oldjoin_internals,
341 argc, argv);
342}
343
344/**
345 * Join a domain, the old way. This function exists to allow
346 * the message to be displayed when oldjoin was explicitly
347 * requested, but not when it was implied by "net rpc join".
348 *
349 * @param argc Standard main() style argc.
350 * @param argv Standard main() style argv. Initial components are already
351 * stripped.
352 *
353 * @return A shell status integer (0 for success)
354 **/
355
356static int net_rpc_oldjoin(int argc, const char **argv)
357{
358 int rc = net_rpc_perform_oldjoin(argc, argv);
359
360 if (rc) {
361 d_fprintf(stderr, "Failed to join domain\n");
362 }
363
364 return rc;
365}
366
367/**
368 * Basic usage function for 'net rpc join'.
369 * @param argc Standard main() style argc.
370 * @param argv Standard main() style argv. Initial components are already
371 * stripped.
372 **/
373
374static int rpc_join_usage(int argc, const char **argv)
375{
376 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
377 "\t to join a domain with admin username & password\n"\
378 "\t\t password will be prompted if needed and none is specified\n"\
379 "\t <type> can be (default MEMBER)\n"\
380 "\t\t BDC - Join as a BDC\n"\
381 "\t\t PDC - Join as a PDC\n"\
382 "\t\t MEMBER - Join as a MEMBER server\n");
383
384 net_common_flags_usage(argc, argv);
385 return -1;
386}
387
388/**
389 * 'net rpc join' entrypoint.
390 * @param argc Standard main() style argc.
391 * @param argv Standard main() style argv. Initial components are already
392 * stripped.
393 *
394 * Main 'net_rpc_join()' (where the admin username/password is used) is
395 * in net_rpc_join.c.
396 * Try to just change the password, but if that doesn't work, use/prompt
397 * for a username/password.
398 **/
399
400int net_rpc_join(int argc, const char **argv)
401{
402 if (lp_server_role() == ROLE_STANDALONE) {
403 d_printf("cannot join as standalone machine\n");
404 return -1;
405 }
406
407 if (strlen(global_myname()) > 15) {
408 d_printf("Our netbios name can be at most 15 chars long, "
409 "\"%s\" is %u chars long\n",
410 global_myname(), (unsigned int)strlen(global_myname()));
411 return -1;
412 }
413
414 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
415 return 0;
416
417 return net_rpc_join_newstyle(argc, argv);
418}
419
420/**
421 * display info about a rpc domain
422 *
423 * All parameters are provided by the run_rpc_command function, except for
424 * argc, argv which are passed through.
425 *
426 * @param domain_sid The domain sid acquired from the remote server.
427 * @param cli A cli_state connected to the server.
428 * @param mem_ctx Talloc context, destroyed on completion of the function.
429 * @param argc Standard main() style argc.
430 * @param argv Standard main() style argv. Initial components are already
431 * stripped.
432 *
433 * @return Normal NTSTATUS return.
434 **/
435
436NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
437 const char *domain_name,
438 struct cli_state *cli,
439 struct rpc_pipe_client *pipe_hnd,
440 TALLOC_CTX *mem_ctx,
441 int argc,
442 const char **argv)
443{
444 POLICY_HND connect_pol, domain_pol;
445 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
446 union samr_DomainInfo *info = NULL;
447 fstring sid_str;
448
449 sid_to_fstring(sid_str, domain_sid);
450
451 /* Get sam policy handle */
452 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
453 pipe_hnd->cli->desthost,
454 MAXIMUM_ALLOWED_ACCESS,
455 &connect_pol);
456 if (!NT_STATUS_IS_OK(result)) {
457 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
458 goto done;
459 }
460
461 /* Get domain policy handle */
462 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
463 &connect_pol,
464 MAXIMUM_ALLOWED_ACCESS,
465 CONST_DISCARD(struct dom_sid2 *, domain_sid),
466 &domain_pol);
467 if (!NT_STATUS_IS_OK(result)) {
468 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
469 goto done;
470 }
471
472 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
473 &domain_pol,
474 2,
475 &info);
476 if (NT_STATUS_IS_OK(result)) {
477 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
478 d_printf("Domain SID: %s\n", sid_str);
479 d_printf("Sequence number: %llu\n",
480 (unsigned long long)info->info2.sequence_num);
481 d_printf("Num users: %u\n", info->info2.num_users);
482 d_printf("Num domain groups: %u\n", info->info2.num_groups);
483 d_printf("Num local groups: %u\n", info->info2.num_aliases);
484 }
485
486 done:
487 return result;
488}
489
490/**
491 * 'net rpc info' entrypoint.
492 * @param argc Standard main() style argc
493 * @param argv Standard main() style argv. Initial components are already
494 * stripped.
495 **/
496
497int net_rpc_info(int argc, const char **argv)
498{
499 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
500 rpc_info_internals,
501 argc, argv);
502}
503
504/**
505 * Fetch domain SID into the local secrets.tdb
506 *
507 * All parameters are provided by the run_rpc_command function, except for
508 * argc, argv which are passed through.
509 *
510 * @param domain_sid The domain sid acquired from the remote server.
511 * @param cli A cli_state connected to the server.
512 * @param mem_ctx Talloc context, destroyed on completion of the function.
513 * @param argc Standard main() style argc.
514 * @param argv Standard main() style argv. Initial components are already
515 * stripped.
516 *
517 * @return Normal NTSTATUS return.
518 **/
519
520static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
521 const char *domain_name,
522 struct cli_state *cli,
523 struct rpc_pipe_client *pipe_hnd,
524 TALLOC_CTX *mem_ctx,
525 int argc,
526 const char **argv)
527{
528 fstring sid_str;
529
530 sid_to_fstring(sid_str, domain_sid);
531 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
532 sid_str, domain_name);
533
534 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
535 DEBUG(0,("Can't store domain SID\n"));
536 return NT_STATUS_UNSUCCESSFUL;
537 }
538
539 return NT_STATUS_OK;
540}
541
542/**
543 * 'net rpc getsid' entrypoint.
544 * @param argc Standard main() style argc.
545 * @param argv Standard main() style argv. Initial components are already
546 * stripped.
547 **/
548
549int net_rpc_getsid(int argc, const char **argv)
550{
551 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
552 rpc_getsid_internals,
553 argc, argv);
554}
555
556/****************************************************************************/
557
558/**
559 * Basic usage function for 'net rpc user'.
560 * @param argc Standard main() style argc.
561 * @param argv Standard main() style argv. Initial components are already
562 * stripped.
563 **/
564
565static int rpc_user_usage(int argc, const char **argv)
566{
567 return net_help_user(argc, argv);
568}
569
570/**
571 * Add a new user to a remote RPC server.
572 *
573 * @param argc Standard main() style argc.
574 * @param argv Standard main() style argv. Initial components are already
575 * stripped.
576 *
577 * @return A shell status integer (0 for success)
578 **/
579
580static int rpc_user_add(int argc, const char **argv)
581{
582 NET_API_STATUS status;
583 struct USER_INFO_1 info1;
584 uint32_t parm_error = 0;
585
586 if (argc < 1) {
587 d_printf("User must be specified\n");
588 rpc_user_usage(argc, argv);
589 return 0;
590 }
591
592 ZERO_STRUCT(info1);
593
594 info1.usri1_name = argv[0];
595 if (argc == 2) {
596 info1.usri1_password = argv[1];
597 }
598
599 status = NetUserAdd(opt_host, 1, (uint8_t *)&info1, &parm_error);
600
601 if (status != 0) {
602 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
603 argv[0], libnetapi_get_error_string(netapi_ctx, status));
604 return -1;
605 } else {
606 d_printf("Added user '%s'.\n", argv[0]);
607 }
608
609 return 0;
610}
611
612/**
613 * Rename a user on a remote RPC server.
614 *
615 * All parameters are provided by the run_rpc_command function, except for
616 * argc, argv which are passed through.
617 *
618 * @param domain_sid The domain sid acquired from the remote server.
619 * @param cli A cli_state connected to the server.
620 * @param mem_ctx Talloc context, destroyed on completion of the function.
621 * @param argc Standard main() style argc.
622 * @param argv Standard main() style argv. Initial components are already
623 * stripped.
624 *
625 * @return Normal NTSTATUS return.
626 **/
627
628static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
629 const char *domain_name,
630 struct cli_state *cli,
631 struct rpc_pipe_client *pipe_hnd,
632 TALLOC_CTX *mem_ctx,
633 int argc,
634 const char **argv)
635{
636 POLICY_HND connect_pol, domain_pol, user_pol;
637 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
638 uint32 info_level = 7;
639 const char *old_name, *new_name;
640 struct samr_Ids user_rids, name_types;
641 struct lsa_String lsa_acct_name;
642 union samr_UserInfo *info = NULL;
643
644 if (argc != 2) {
645 d_printf("Old and new username must be specified\n");
646 rpc_user_usage(argc, argv);
647 return NT_STATUS_OK;
648 }
649
650 old_name = argv[0];
651 new_name = argv[1];
652
653 /* Get sam policy handle */
654
655 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
656 pipe_hnd->cli->desthost,
657 MAXIMUM_ALLOWED_ACCESS,
658 &connect_pol);
659
660 if (!NT_STATUS_IS_OK(result)) {
661 goto done;
662 }
663
664 /* Get domain policy handle */
665
666 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
667 &connect_pol,
668 MAXIMUM_ALLOWED_ACCESS,
669 CONST_DISCARD(struct dom_sid2 *, domain_sid),
670 &domain_pol);
671 if (!NT_STATUS_IS_OK(result)) {
672 goto done;
673 }
674
675 init_lsa_String(&lsa_acct_name, old_name);
676
677 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
678 &domain_pol,
679 1,
680 &lsa_acct_name,
681 &user_rids,
682 &name_types);
683 if (!NT_STATUS_IS_OK(result)) {
684 goto done;
685 }
686
687 /* Open domain user */
688 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
689 &domain_pol,
690 MAXIMUM_ALLOWED_ACCESS,
691 user_rids.ids[0],
692 &user_pol);
693
694 if (!NT_STATUS_IS_OK(result)) {
695 goto done;
696 }
697
698 /* Query user info */
699 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
700 &user_pol,
701 info_level,
702 &info);
703
704 if (!NT_STATUS_IS_OK(result)) {
705 goto done;
706 }
707
708 init_samr_user_info7(&info->info7, new_name);
709
710 /* Set new name */
711 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
712 &user_pol,
713 info_level,
714 info);
715
716 if (!NT_STATUS_IS_OK(result)) {
717 goto done;
718 }
719
720 done:
721 if (!NT_STATUS_IS_OK(result)) {
722 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
723 nt_errstr(result));
724 } else {
725 d_printf("Renamed user from %s to %s\n", old_name, new_name);
726 }
727 return result;
728}
729
730/**
731 * Rename a user on a remote RPC server.
732 *
733 * @param argc Standard main() style argc.
734 * @param argv Standard main() style argv. Initial components are already
735 * stripped.
736 *
737 * @return A shell status integer (0 for success).
738 **/
739
740static int rpc_user_rename(int argc, const char **argv)
741{
742 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
743 argc, argv);
744}
745
746/**
747 * Delete a user from a remote RPC server.
748 *
749 * @param argc Standard main() style argc.
750 * @param argv Standard main() style argv. Initial components are already
751 * stripped.
752 *
753 * @return A shell status integer (0 for success).
754 **/
755
756static int rpc_user_delete(int argc, const char **argv)
757{
758 NET_API_STATUS status;
759
760 if (argc < 1) {
761 d_printf("User must be specified\n");
762 rpc_user_usage(argc, argv);
763 return 0;
764 }
765
766 status = NetUserDel(opt_host, argv[0]);
767
768 if (status != 0) {
769 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
770 argv[0],
771 libnetapi_get_error_string(netapi_ctx, status));
772 return -1;
773 } else {
774 d_printf("Deleted user '%s'.\n", argv[0]);
775 }
776
777 return 0;
778}
779
780/**
781 * Set a password for a user on a remote RPC server.
782 *
783 * All parameters are provided by the run_rpc_command function, except for
784 * argc, argv which are passed through.
785 *
786 * @param domain_sid The domain sid acquired from the remote server.
787 * @param cli A cli_state connected to the server.
788 * @param mem_ctx Talloc context, destroyed on completion of the function.
789 * @param argc Standard main() style argc.
790 * @param argv Standard main() style argv. Initial components are already
791 * stripped.
792 *
793 * @return Normal NTSTATUS return.
794 **/
795
796static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
797 const char *domain_name,
798 struct cli_state *cli,
799 struct rpc_pipe_client *pipe_hnd,
800 TALLOC_CTX *mem_ctx,
801 int argc,
802 const char **argv)
803{
804 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
805 POLICY_HND connect_pol, domain_pol, user_pol;
806 const char *user;
807 const char *new_password;
808 char *prompt = NULL;
809 union samr_UserInfo info;
810 struct samr_CryptPassword crypt_pwd;
811
812 if (argc < 1) {
813 d_printf("User must be specified\n");
814 rpc_user_usage(argc, argv);
815 return NT_STATUS_OK;
816 }
817
818 user = argv[0];
819
820 if (argv[1]) {
821 new_password = argv[1];
822 } else {
823 asprintf(&prompt, "Enter new password for %s:", user);
824 new_password = getpass(prompt);
825 SAFE_FREE(prompt);
826 }
827
828 /* Get sam policy and domain handles */
829
830 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
831 pipe_hnd->cli->desthost,
832 MAXIMUM_ALLOWED_ACCESS,
833 &connect_pol);
834
835 if (!NT_STATUS_IS_OK(result)) {
836 goto done;
837 }
838
839 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
840 &connect_pol,
841 MAXIMUM_ALLOWED_ACCESS,
842 CONST_DISCARD(struct dom_sid2 *, domain_sid),
843 &domain_pol);
844
845 if (!NT_STATUS_IS_OK(result)) {
846 goto done;
847 }
848
849 /* Get handle on user */
850
851 {
852 struct samr_Ids user_rids, name_types;
853 struct lsa_String lsa_acct_name;
854
855 init_lsa_String(&lsa_acct_name, user);
856
857 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
858 &domain_pol,
859 1,
860 &lsa_acct_name,
861 &user_rids,
862 &name_types);
863 if (!NT_STATUS_IS_OK(result)) {
864 goto done;
865 }
866
867 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
868 &domain_pol,
869 MAXIMUM_ALLOWED_ACCESS,
870 user_rids.ids[0],
871 &user_pol);
872
873 if (!NT_STATUS_IS_OK(result)) {
874 goto done;
875 }
876 }
877
878 /* Set password on account */
879
880 init_samr_CryptPassword(new_password,
881 &cli->user_session_key,
882 &crypt_pwd);
883
884 init_samr_user_info24(&info.info24, &crypt_pwd,
885 PASS_DONT_CHANGE_AT_NEXT_LOGON);
886
887 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
888 &user_pol,
889 24,
890 &info);
891
892 if (!NT_STATUS_IS_OK(result)) {
893 goto done;
894 }
895
896 /* Display results */
897
898 done:
899 return result;
900
901}
902
903/**
904 * Set a user's password on a remote RPC server.
905 *
906 * @param argc Standard main() style argc.
907 * @param argv Standard main() style argv. Initial components are already
908 * stripped.
909 *
910 * @return A shell status integer (0 for success).
911 **/
912
913static int rpc_user_password(int argc, const char **argv)
914{
915 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
916 argc, argv);
917}
918
919/**
920 * List user's groups on a remote RPC server.
921 *
922 * All parameters are provided by the run_rpc_command function, except for
923 * argc, argv which are passed through.
924 *
925 * @param domain_sid The domain sid acquired from the remote server.
926 * @param cli A cli_state connected to the server.
927 * @param mem_ctx Talloc context, destroyed on completion of the function.
928 * @param argc Standard main() style argc.
929 * @param argv Standard main() style argv. Initial components are already
930 * stripped.
931 *
932 * @return Normal NTSTATUS return.
933 **/
934
935static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
936 const char *domain_name,
937 struct cli_state *cli,
938 struct rpc_pipe_client *pipe_hnd,
939 TALLOC_CTX *mem_ctx,
940 int argc,
941 const char **argv)
942{
943 POLICY_HND connect_pol, domain_pol, user_pol;
944 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
945 int i;
946 struct samr_RidWithAttributeArray *rid_array = NULL;
947 struct lsa_Strings names;
948 struct samr_Ids types;
949 uint32_t *lrids = NULL;
950 struct samr_Ids rids, name_types;
951 struct lsa_String lsa_acct_name;
952
953
954 if (argc < 1) {
955 d_printf("User must be specified\n");
956 rpc_user_usage(argc, argv);
957 return NT_STATUS_OK;
958 }
959 /* Get sam policy handle */
960
961 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
962 pipe_hnd->cli->desthost,
963 MAXIMUM_ALLOWED_ACCESS,
964 &connect_pol);
965 if (!NT_STATUS_IS_OK(result)) goto done;
966
967 /* Get domain policy handle */
968
969 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
970 &connect_pol,
971 MAXIMUM_ALLOWED_ACCESS,
972 CONST_DISCARD(struct dom_sid2 *, domain_sid),
973 &domain_pol);
974 if (!NT_STATUS_IS_OK(result)) goto done;
975
976 /* Get handle on user */
977
978 init_lsa_String(&lsa_acct_name, argv[0]);
979
980 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
981 &domain_pol,
982 1,
983 &lsa_acct_name,
984 &rids,
985 &name_types);
986
987 if (!NT_STATUS_IS_OK(result)) goto done;
988
989 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
990 &domain_pol,
991 MAXIMUM_ALLOWED_ACCESS,
992 rids.ids[0],
993 &user_pol);
994 if (!NT_STATUS_IS_OK(result)) goto done;
995
996 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
997 &user_pol,
998 &rid_array);
999
1000 if (!NT_STATUS_IS_OK(result)) goto done;
1001
1002 /* Look up rids */
1003
1004 if (rid_array->count) {
1005 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
1006 result = NT_STATUS_NO_MEMORY;
1007 goto done;
1008 }
1009
1010 for (i = 0; i < rid_array->count; i++)
1011 lrids[i] = rid_array->rids[i].rid;
1012
1013 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
1014 &domain_pol,
1015 rid_array->count,
1016 lrids,
1017 &names,
1018 &types);
1019
1020 if (!NT_STATUS_IS_OK(result)) {
1021 goto done;
1022 }
1023
1024 /* Display results */
1025
1026 for (i = 0; i < names.count; i++)
1027 printf("%s\n", names.names[i].string);
1028 }
1029 done:
1030 return result;
1031}
1032
1033/**
1034 * List a user's groups from a remote RPC server.
1035 *
1036 * @param argc Standard main() style argc.
1037 * @param argv Standard main() style argv. Initial components are already
1038 * stripped.
1039 *
1040 * @return A shell status integer (0 for success).
1041 **/
1042
1043static int rpc_user_info(int argc, const char **argv)
1044{
1045 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1046 argc, argv);
1047}
1048
1049/**
1050 * List users on a remote RPC server.
1051 *
1052 * All parameters are provided by the run_rpc_command function, except for
1053 * argc, argv which are passed through.
1054 *
1055 * @param domain_sid The domain sid acquired from the remote server.
1056 * @param cli A cli_state connected to the server.
1057 * @param mem_ctx Talloc context, destroyed on completion of the function.
1058 * @param argc Standard main() style argc.
1059 * @param argv Standard main() style argv. Initial components are already
1060 * stripped.
1061 *
1062 * @return Normal NTSTATUS return.
1063 **/
1064
1065static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1066 const char *domain_name,
1067 struct cli_state *cli,
1068 struct rpc_pipe_client *pipe_hnd,
1069 TALLOC_CTX *mem_ctx,
1070 int argc,
1071 const char **argv)
1072{
1073 POLICY_HND connect_pol, domain_pol;
1074 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1075 uint32 start_idx=0, num_entries, i, loop_count = 0;
1076
1077 /* Get sam policy handle */
1078
1079 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1080 pipe_hnd->cli->desthost,
1081 MAXIMUM_ALLOWED_ACCESS,
1082 &connect_pol);
1083 if (!NT_STATUS_IS_OK(result)) {
1084 goto done;
1085 }
1086
1087 /* Get domain policy handle */
1088
1089 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1090 &connect_pol,
1091 MAXIMUM_ALLOWED_ACCESS,
1092 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1093 &domain_pol);
1094 if (!NT_STATUS_IS_OK(result)) {
1095 goto done;
1096 }
1097
1098 /* Query domain users */
1099 if (opt_long_list_entries)
1100 d_printf("\nUser name Comment"\
1101 "\n-----------------------------\n");
1102 do {
1103 const char *user = NULL;
1104 const char *desc = NULL;
1105 uint32 max_entries, max_size;
1106 uint32_t total_size, returned_size;
1107 union samr_DispInfo info;
1108
1109 get_query_dispinfo_params(
1110 loop_count, &max_entries, &max_size);
1111
1112 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1113 &domain_pol,
1114 1,
1115 start_idx,
1116 max_entries,
1117 max_size,
1118 &total_size,
1119 &returned_size,
1120 &info);
1121 loop_count++;
1122 start_idx += info.info1.count;
1123 num_entries = info.info1.count;
1124
1125 for (i = 0; i < num_entries; i++) {
1126 user = info.info1.entries[i].account_name.string;
1127 if (opt_long_list_entries)
1128 desc = info.info1.entries[i].description.string;
1129 if (opt_long_list_entries)
1130 printf("%-21.21s %s\n", user, desc);
1131 else
1132 printf("%s\n", user);
1133 }
1134 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1135
1136 done:
1137 return result;
1138}
1139
1140/**
1141 * 'net rpc user' entrypoint.
1142 * @param argc Standard main() style argc.
1143 * @param argv Standard main() style argv. Initial components are already
1144 * stripped.
1145 **/
1146
1147int net_rpc_user(int argc, const char **argv)
1148{
1149 NET_API_STATUS status;
1150
1151 struct functable func[] = {
1152 {"add", rpc_user_add},
1153 {"info", rpc_user_info},
1154 {"delete", rpc_user_delete},
1155 {"password", rpc_user_password},
1156 {"rename", rpc_user_rename},
1157 {NULL, NULL}
1158 };
1159
1160 status = libnetapi_init(&netapi_ctx);
1161 if (status != 0) {
1162 return -1;
1163 }
1164 libnetapi_set_username(netapi_ctx, opt_user_name);
1165 libnetapi_set_password(netapi_ctx, opt_password);
1166
1167 if (argc == 0) {
1168 return run_rpc_command(NULL,PI_SAMR, 0,
1169 rpc_user_list_internals,
1170 argc, argv);
1171 }
1172
1173 return net_run_function(argc, argv, func, rpc_user_usage);
1174}
1175
1176static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1177 struct rpc_sh_ctx *ctx,
1178 struct rpc_pipe_client *pipe_hnd,
1179 int argc, const char **argv)
1180{
1181 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1182 ctx->cli, pipe_hnd, mem_ctx,
1183 argc, argv);
1184}
1185
1186static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1187 struct rpc_sh_ctx *ctx,
1188 struct rpc_pipe_client *pipe_hnd,
1189 int argc, const char **argv)
1190{
1191 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1192 ctx->cli, pipe_hnd, mem_ctx,
1193 argc, argv);
1194}
1195
1196static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1197 struct rpc_sh_ctx *ctx,
1198 struct rpc_pipe_client *pipe_hnd,
1199 int argc, const char **argv,
1200 NTSTATUS (*fn)(
1201 TALLOC_CTX *mem_ctx,
1202 struct rpc_sh_ctx *ctx,
1203 struct rpc_pipe_client *pipe_hnd,
1204 POLICY_HND *user_hnd,
1205 int argc, const char **argv))
1206{
1207 POLICY_HND connect_pol, domain_pol, user_pol;
1208 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1209 DOM_SID sid;
1210 uint32 rid;
1211 enum lsa_SidType type;
1212
1213 if (argc == 0) {
1214 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1215 return NT_STATUS_INVALID_PARAMETER;
1216 }
1217
1218 ZERO_STRUCT(connect_pol);
1219 ZERO_STRUCT(domain_pol);
1220 ZERO_STRUCT(user_pol);
1221
1222 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1223 NULL, NULL, &sid, &type);
1224 if (!NT_STATUS_IS_OK(result)) {
1225 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1226 nt_errstr(result));
1227 goto done;
1228 }
1229
1230 if (type != SID_NAME_USER) {
1231 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1232 sid_type_lookup(type));
1233 result = NT_STATUS_NO_SUCH_USER;
1234 goto done;
1235 }
1236
1237 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1238 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1239 result = NT_STATUS_NO_SUCH_USER;
1240 goto done;
1241 }
1242
1243 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1244 pipe_hnd->cli->desthost,
1245 MAXIMUM_ALLOWED_ACCESS,
1246 &connect_pol);
1247 if (!NT_STATUS_IS_OK(result)) {
1248 goto done;
1249 }
1250
1251 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1252 &connect_pol,
1253 MAXIMUM_ALLOWED_ACCESS,
1254 ctx->domain_sid,
1255 &domain_pol);
1256 if (!NT_STATUS_IS_OK(result)) {
1257 goto done;
1258 }
1259
1260 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1261 &domain_pol,
1262 MAXIMUM_ALLOWED_ACCESS,
1263 rid,
1264 &user_pol);
1265 if (!NT_STATUS_IS_OK(result)) {
1266 goto done;
1267 }
1268
1269 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1270
1271 done:
1272 if (is_valid_policy_hnd(&user_pol)) {
1273 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1274 }
1275 if (is_valid_policy_hnd(&domain_pol)) {
1276 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1277 }
1278 if (is_valid_policy_hnd(&connect_pol)) {
1279 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1280 }
1281 return result;
1282}
1283
1284static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1285 struct rpc_sh_ctx *ctx,
1286 struct rpc_pipe_client *pipe_hnd,
1287 POLICY_HND *user_hnd,
1288 int argc, const char **argv)
1289{
1290 NTSTATUS result;
1291 union samr_UserInfo *info = NULL;
1292
1293 if (argc != 0) {
1294 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1295 return NT_STATUS_INVALID_PARAMETER;
1296 }
1297
1298 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1299 user_hnd,
1300 21,
1301 &info);
1302 if (!NT_STATUS_IS_OK(result)) {
1303 return result;
1304 }
1305
1306 d_printf("user rid: %d, group rid: %d\n",
1307 info->info21.rid,
1308 info->info21.primary_gid);
1309
1310 return result;
1311}
1312
1313static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1314 struct rpc_sh_ctx *ctx,
1315 struct rpc_pipe_client *pipe_hnd,
1316 int argc, const char **argv)
1317{
1318 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1319 rpc_sh_user_show_internals);
1320}
1321
1322#define FETCHSTR(name, rec) \
1323do { if (strequal(ctx->thiscmd, name)) { \
1324 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1325} while (0);
1326
1327#define SETSTR(name, rec, flag) \
1328do { if (strequal(ctx->thiscmd, name)) { \
1329 init_lsa_String(&(info->info21.rec), argv[0]); \
1330 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1331} while (0);
1332
1333static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1334 struct rpc_sh_ctx *ctx,
1335 struct rpc_pipe_client *pipe_hnd,
1336 POLICY_HND *user_hnd,
1337 int argc, const char **argv)
1338{
1339 NTSTATUS result;
1340 const char *username;
1341 const char *oldval = "";
1342 union samr_UserInfo *info = NULL;
1343
1344 if (argc > 1) {
1345 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1346 ctx->whoami);
1347 return NT_STATUS_INVALID_PARAMETER;
1348 }
1349
1350 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1351 user_hnd,
1352 21,
1353 &info);
1354 if (!NT_STATUS_IS_OK(result)) {
1355 return result;
1356 }
1357
1358 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1359
1360 FETCHSTR("fullname", full_name);
1361 FETCHSTR("homedir", home_directory);
1362 FETCHSTR("homedrive", home_drive);
1363 FETCHSTR("logonscript", logon_script);
1364 FETCHSTR("profilepath", profile_path);
1365 FETCHSTR("description", description);
1366
1367 if (argc == 0) {
1368 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1369 goto done;
1370 }
1371
1372 if (strcmp(argv[0], "NULL") == 0) {
1373 argv[0] = "";
1374 }
1375
1376 ZERO_STRUCT(info->info21);
1377
1378 SETSTR("fullname", full_name, FULL_NAME);
1379 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1380 SETSTR("homedrive", home_drive, HOME_DRIVE);
1381 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1382 SETSTR("profilepath", profile_path, PROFILE_PATH);
1383 SETSTR("description", description, DESCRIPTION);
1384
1385 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1386 user_hnd,
1387 21,
1388 info);
1389
1390 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1391 ctx->thiscmd, oldval, argv[0]);
1392
1393 done:
1394
1395 return result;
1396}
1397
1398#define HANDLEFLG(name, rec) \
1399do { if (strequal(ctx->thiscmd, name)) { \
1400 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1401 if (newval) { \
1402 newflags = oldflags | ACB_##rec; \
1403 } else { \
1404 newflags = oldflags & ~ACB_##rec; \
1405 } } } while (0);
1406
1407static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1408 struct rpc_sh_ctx *ctx,
1409 struct rpc_pipe_client *pipe_hnd,
1410 int argc, const char **argv)
1411{
1412 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1413 rpc_sh_user_str_edit_internals);
1414}
1415
1416static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1417 struct rpc_sh_ctx *ctx,
1418 struct rpc_pipe_client *pipe_hnd,
1419 POLICY_HND *user_hnd,
1420 int argc, const char **argv)
1421{
1422 NTSTATUS result;
1423 const char *username;
1424 const char *oldval = "unknown";
1425 uint32 oldflags, newflags;
1426 bool newval;
1427 union samr_UserInfo *info = NULL;
1428
1429 if ((argc > 1) ||
1430 ((argc == 1) && !strequal(argv[0], "yes") &&
1431 !strequal(argv[0], "no"))) {
1432 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1433 ctx->whoami);
1434 return NT_STATUS_INVALID_PARAMETER;
1435 }
1436
1437 newval = strequal(argv[0], "yes");
1438
1439 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1440 user_hnd,
1441 21,
1442 &info);
1443 if (!NT_STATUS_IS_OK(result)) {
1444 return result;
1445 }
1446
1447 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1448 oldflags = info->info21.acct_flags;
1449 newflags = info->info21.acct_flags;
1450
1451 HANDLEFLG("disabled", DISABLED);
1452 HANDLEFLG("pwnotreq", PWNOTREQ);
1453 HANDLEFLG("autolock", AUTOLOCK);
1454 HANDLEFLG("pwnoexp", PWNOEXP);
1455
1456 if (argc == 0) {
1457 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1458 goto done;
1459 }
1460
1461 ZERO_STRUCT(info->info21);
1462
1463 info->info21.acct_flags = newflags;
1464 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1465
1466 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1467 user_hnd,
1468 21,
1469 info);
1470
1471 if (NT_STATUS_IS_OK(result)) {
1472 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1473 ctx->thiscmd, oldval, argv[0]);
1474 }
1475
1476 done:
1477
1478 return result;
1479}
1480
1481static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1482 struct rpc_sh_ctx *ctx,
1483 struct rpc_pipe_client *pipe_hnd,
1484 int argc, const char **argv)
1485{
1486 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1487 rpc_sh_user_flag_edit_internals);
1488}
1489
1490struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1491 struct rpc_sh_ctx *ctx)
1492{
1493 static struct rpc_sh_cmd cmds[] = {
1494
1495 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1496 "Show/Set a user's full name" },
1497
1498 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1499 "Show/Set a user's home directory" },
1500
1501 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1502 "Show/Set a user's home drive" },
1503
1504 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1505 "Show/Set a user's logon script" },
1506
1507 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1508 "Show/Set a user's profile path" },
1509
1510 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1511 "Show/Set a user's description" },
1512
1513 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1514 "Show/Set whether a user is disabled" },
1515
1516 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1517 "Show/Set whether a user locked out" },
1518
1519 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1520 "Show/Set whether a user does not need a password" },
1521
1522 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1523 "Show/Set whether a user's password does not expire" },
1524
1525 { NULL, NULL, 0, NULL, NULL }
1526 };
1527
1528 return cmds;
1529}
1530
1531struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1532 struct rpc_sh_ctx *ctx)
1533{
1534 static struct rpc_sh_cmd cmds[] = {
1535
1536 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1537 "List available users" },
1538
1539 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1540 "List the domain groups a user is member of" },
1541
1542 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1543 "Show info about a user" },
1544
1545 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1546 "Show/Modify a user's fields" },
1547
1548 { NULL, NULL, 0, NULL, NULL }
1549 };
1550
1551 return cmds;
1552}
1553
1554/****************************************************************************/
1555
1556/**
1557 * Basic usage function for 'net rpc group'.
1558 * @param argc Standard main() style argc.
1559 * @param argv Standard main() style argv. Initial components are already
1560 * stripped.
1561 **/
1562
1563static int rpc_group_usage(int argc, const char **argv)
1564{
1565 return net_help_group(argc, argv);
1566}
1567
1568/**
1569 * Delete group on a remote RPC server.
1570 *
1571 * All parameters are provided by the run_rpc_command function, except for
1572 * argc, argv which are passed through.
1573 *
1574 * @param domain_sid The domain sid acquired from the remote server.
1575 * @param cli A cli_state connected to the server.
1576 * @param mem_ctx Talloc context, destroyed on completion of the function.
1577 * @param argc Standard main() style argc.
1578 * @param argv Standard main() style argv. Initial components are already
1579 * stripped.
1580 *
1581 * @return Normal NTSTATUS return.
1582 **/
1583
1584static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1585 const char *domain_name,
1586 struct cli_state *cli,
1587 struct rpc_pipe_client *pipe_hnd,
1588 TALLOC_CTX *mem_ctx,
1589 int argc,
1590 const char **argv)
1591{
1592 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1593 bool group_is_primary = False;
1594 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1595 uint32_t group_rid;
1596 struct samr_RidTypeArray *rids = NULL;
1597 /* char **names; */
1598 int i;
1599 /* DOM_GID *user_gids; */
1600
1601 struct samr_Ids group_rids, name_types;
1602 struct lsa_String lsa_acct_name;
1603 union samr_UserInfo *info = NULL;
1604
1605 if (argc < 1) {
1606 d_printf("specify group\n");
1607 rpc_group_usage(argc,argv);
1608 return NT_STATUS_OK; /* ok? */
1609 }
1610
1611 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1612 pipe_hnd->cli->desthost,
1613 MAXIMUM_ALLOWED_ACCESS,
1614 &connect_pol);
1615
1616 if (!NT_STATUS_IS_OK(result)) {
1617 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1618 goto done;
1619 }
1620
1621 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1622 &connect_pol,
1623 MAXIMUM_ALLOWED_ACCESS,
1624 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1625 &domain_pol);
1626
1627 if (!NT_STATUS_IS_OK(result)) {
1628 d_fprintf(stderr, "Request open_domain failed\n");
1629 goto done;
1630 }
1631
1632 init_lsa_String(&lsa_acct_name, argv[0]);
1633
1634 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1635 &domain_pol,
1636 1,
1637 &lsa_acct_name,
1638 &group_rids,
1639 &name_types);
1640 if (!NT_STATUS_IS_OK(result)) {
1641 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1642 goto done;
1643 }
1644
1645 switch (name_types.ids[0])
1646 {
1647 case SID_NAME_DOM_GRP:
1648 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1649 &domain_pol,
1650 MAXIMUM_ALLOWED_ACCESS,
1651 group_rids.ids[0],
1652 &group_pol);
1653 if (!NT_STATUS_IS_OK(result)) {
1654 d_fprintf(stderr, "Request open_group failed");
1655 goto done;
1656 }
1657
1658 group_rid = group_rids.ids[0];
1659
1660 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1661 &group_pol,
1662 &rids);
1663
1664 if (!NT_STATUS_IS_OK(result)) {
1665 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1666 goto done;
1667 }
1668
1669 if (opt_verbose) {
1670 d_printf("Domain Group %s (rid: %d) has %d members\n",
1671 argv[0],group_rid, rids->count);
1672 }
1673
1674 /* Check if group is anyone's primary group */
1675 for (i = 0; i < rids->count; i++)
1676 {
1677 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1678 &domain_pol,
1679 MAXIMUM_ALLOWED_ACCESS,
1680 rids->rids[i],
1681 &user_pol);
1682
1683 if (!NT_STATUS_IS_OK(result)) {
1684 d_fprintf(stderr, "Unable to open group member %d\n",
1685 rids->rids[i]);
1686 goto done;
1687 }
1688
1689 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1690 &user_pol,
1691 21,
1692 &info);
1693
1694 if (!NT_STATUS_IS_OK(result)) {
1695 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1696 rids->rids[i]);
1697 goto done;
1698 }
1699
1700 if (info->info21.primary_gid == group_rid) {
1701 if (opt_verbose) {
1702 d_printf("Group is primary group of %s\n",
1703 info->info21.account_name.string);
1704 }
1705 group_is_primary = True;
1706 }
1707
1708 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1709 }
1710
1711 if (group_is_primary) {
1712 d_fprintf(stderr, "Unable to delete group because some "
1713 "of it's members have it as primary group\n");
1714 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1715 goto done;
1716 }
1717
1718 /* remove all group members */
1719 for (i = 0; i < rids->count; i++)
1720 {
1721 if (opt_verbose)
1722 d_printf("Remove group member %d...",
1723 rids->rids[i]);
1724 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1725 &group_pol,
1726 rids->rids[i]);
1727
1728 if (NT_STATUS_IS_OK(result)) {
1729 if (opt_verbose)
1730 d_printf("ok\n");
1731 } else {
1732 if (opt_verbose)
1733 d_printf("failed\n");
1734 goto done;
1735 }
1736 }
1737
1738 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1739 &group_pol);
1740
1741 break;
1742 /* removing a local group is easier... */
1743 case SID_NAME_ALIAS:
1744 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1745 &domain_pol,
1746 MAXIMUM_ALLOWED_ACCESS,
1747 group_rids.ids[0],
1748 &group_pol);
1749
1750 if (!NT_STATUS_IS_OK(result)) {
1751 d_fprintf(stderr, "Request open_alias failed\n");
1752 goto done;
1753 }
1754
1755 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1756 &group_pol);
1757 break;
1758 default:
1759 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1760 argv[0],sid_type_lookup(name_types.ids[0]));
1761 result = NT_STATUS_UNSUCCESSFUL;
1762 goto done;
1763 }
1764
1765
1766 if (NT_STATUS_IS_OK(result)) {
1767 if (opt_verbose)
1768 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1769 } else {
1770 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1771 get_friendly_nt_error_msg(result));
1772 }
1773
1774 done:
1775 return result;
1776
1777}
1778
1779static int rpc_group_delete(int argc, const char **argv)
1780{
1781 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1782 argc,argv);
1783}
1784
1785static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1786 const char *domain_name,
1787 struct cli_state *cli,
1788 struct rpc_pipe_client *pipe_hnd,
1789 TALLOC_CTX *mem_ctx,
1790 int argc,
1791 const char **argv)
1792{
1793 POLICY_HND connect_pol, domain_pol, group_pol;
1794 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1795 union samr_GroupInfo group_info;
1796 struct lsa_String grp_name;
1797 uint32_t rid = 0;
1798
1799 if (argc != 1) {
1800 d_printf("Group name must be specified\n");
1801 rpc_group_usage(argc, argv);
1802 return NT_STATUS_OK;
1803 }
1804
1805 init_lsa_String(&grp_name, argv[0]);
1806
1807 /* Get sam policy handle */
1808
1809 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1810 pipe_hnd->cli->desthost,
1811 MAXIMUM_ALLOWED_ACCESS,
1812 &connect_pol);
1813 if (!NT_STATUS_IS_OK(result)) goto done;
1814
1815 /* Get domain policy handle */
1816
1817 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1818 &connect_pol,
1819 MAXIMUM_ALLOWED_ACCESS,
1820 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1821 &domain_pol);
1822 if (!NT_STATUS_IS_OK(result)) goto done;
1823
1824 /* Create the group */
1825
1826 result = rpccli_samr_CreateDomainGroup(pipe_hnd, mem_ctx,
1827 &domain_pol,
1828 &grp_name,
1829 MAXIMUM_ALLOWED_ACCESS,
1830 &group_pol,
1831 &rid);
1832 if (!NT_STATUS_IS_OK(result)) goto done;
1833
1834 if (strlen(opt_comment) == 0) goto done;
1835
1836 /* We've got a comment to set */
1837
1838 init_lsa_String(&group_info.description, opt_comment);
1839
1840 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
1841 &group_pol,
1842 4,
1843 &group_info);
1844 if (!NT_STATUS_IS_OK(result)) goto done;
1845
1846 done:
1847 if (NT_STATUS_IS_OK(result))
1848 DEBUG(5, ("add group succeeded\n"));
1849 else
1850 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1851
1852 return result;
1853}
1854
1855static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1856 const char *domain_name,
1857 struct cli_state *cli,
1858 struct rpc_pipe_client *pipe_hnd,
1859 TALLOC_CTX *mem_ctx,
1860 int argc,
1861 const char **argv)
1862{
1863 POLICY_HND connect_pol, domain_pol, alias_pol;
1864 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1865 union samr_AliasInfo alias_info;
1866 struct lsa_String alias_name;
1867 uint32_t rid = 0;
1868
1869 if (argc != 1) {
1870 d_printf("Alias name must be specified\n");
1871 rpc_group_usage(argc, argv);
1872 return NT_STATUS_OK;
1873 }
1874
1875 init_lsa_String(&alias_name, argv[0]);
1876
1877 /* Get sam policy handle */
1878
1879 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1880 pipe_hnd->cli->desthost,
1881 MAXIMUM_ALLOWED_ACCESS,
1882 &connect_pol);
1883 if (!NT_STATUS_IS_OK(result)) goto done;
1884
1885 /* Get domain policy handle */
1886
1887 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1888 &connect_pol,
1889 MAXIMUM_ALLOWED_ACCESS,
1890 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1891 &domain_pol);
1892 if (!NT_STATUS_IS_OK(result)) goto done;
1893
1894 /* Create the group */
1895
1896 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
1897 &domain_pol,
1898 &alias_name,
1899 MAXIMUM_ALLOWED_ACCESS,
1900 &alias_pol,
1901 &rid);
1902 if (!NT_STATUS_IS_OK(result)) goto done;
1903
1904 if (strlen(opt_comment) == 0) goto done;
1905
1906 /* We've got a comment to set */
1907
1908 init_lsa_String(&alias_info.description, opt_comment);
1909
1910 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
1911 &alias_pol,
1912 3,
1913 &alias_info);
1914
1915 if (!NT_STATUS_IS_OK(result)) goto done;
1916
1917 done:
1918 if (NT_STATUS_IS_OK(result))
1919 DEBUG(5, ("add alias succeeded\n"));
1920 else
1921 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1922
1923 return result;
1924}
1925
1926static int rpc_group_add(int argc, const char **argv)
1927{
1928 if (opt_localgroup)
1929 return run_rpc_command(NULL, PI_SAMR, 0,
1930 rpc_alias_add_internals,
1931 argc, argv);
1932
1933 return run_rpc_command(NULL, PI_SAMR, 0,
1934 rpc_group_add_internals,
1935 argc, argv);
1936}
1937
1938static NTSTATUS get_sid_from_name(struct cli_state *cli,
1939 TALLOC_CTX *mem_ctx,
1940 const char *name,
1941 DOM_SID *sid,
1942 enum lsa_SidType *type)
1943{
1944 DOM_SID *sids = NULL;
1945 enum lsa_SidType *types = NULL;
1946 struct rpc_pipe_client *pipe_hnd;
1947 POLICY_HND lsa_pol;
1948 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1949
1950 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1951 if (!pipe_hnd) {
1952 goto done;
1953 }
1954
1955 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
1956 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1957
1958 if (!NT_STATUS_IS_OK(result)) {
1959 goto done;
1960 }
1961
1962 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1963 &name, NULL, 1, &sids, &types);
1964
1965 if (NT_STATUS_IS_OK(result)) {
1966 sid_copy(sid, &sids[0]);
1967 *type = types[0];
1968 }
1969
1970 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1971
1972 done:
1973 if (pipe_hnd) {
1974 cli_rpc_pipe_close(pipe_hnd);
1975 }
1976
1977 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1978
1979 /* Try as S-1-5-whatever */
1980
1981 DOM_SID tmp_sid;
1982
1983 if (string_to_sid(&tmp_sid, name)) {
1984 sid_copy(sid, &tmp_sid);
1985 *type = SID_NAME_UNKNOWN;
1986 result = NT_STATUS_OK;
1987 }
1988 }
1989
1990 return result;
1991}
1992
1993static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1994 TALLOC_CTX *mem_ctx,
1995 const DOM_SID *group_sid,
1996 const char *member)
1997{
1998 POLICY_HND connect_pol, domain_pol;
1999 NTSTATUS result;
2000 uint32 group_rid;
2001 POLICY_HND group_pol;
2002
2003 struct samr_Ids rids, rid_types;
2004 struct lsa_String lsa_acct_name;
2005
2006 DOM_SID sid;
2007
2008 sid_copy(&sid, group_sid);
2009
2010 if (!sid_split_rid(&sid, &group_rid)) {
2011 return NT_STATUS_UNSUCCESSFUL;
2012 }
2013
2014 /* Get sam policy handle */
2015 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2016 pipe_hnd->cli->desthost,
2017 MAXIMUM_ALLOWED_ACCESS,
2018 &connect_pol);
2019 if (!NT_STATUS_IS_OK(result)) {
2020 return result;
2021 }
2022
2023 /* Get domain policy handle */
2024 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2025 &connect_pol,
2026 MAXIMUM_ALLOWED_ACCESS,
2027 &sid,
2028 &domain_pol);
2029 if (!NT_STATUS_IS_OK(result)) {
2030 return result;
2031 }
2032
2033 init_lsa_String(&lsa_acct_name, member);
2034
2035 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2036 &domain_pol,
2037 1,
2038 &lsa_acct_name,
2039 &rids,
2040 &rid_types);
2041
2042 if (!NT_STATUS_IS_OK(result)) {
2043 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2044 goto done;
2045 }
2046
2047 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2048 &domain_pol,
2049 MAXIMUM_ALLOWED_ACCESS,
2050 group_rid,
2051 &group_pol);
2052
2053 if (!NT_STATUS_IS_OK(result)) {
2054 goto done;
2055 }
2056
2057 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2058 &group_pol,
2059 rids.ids[0],
2060 0x0005); /* unknown flags */
2061
2062 done:
2063 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2064 return result;
2065}
2066
2067static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2068 TALLOC_CTX *mem_ctx,
2069 const DOM_SID *alias_sid,
2070 const char *member)
2071{
2072 POLICY_HND connect_pol, domain_pol;
2073 NTSTATUS result;
2074 uint32 alias_rid;
2075 POLICY_HND alias_pol;
2076
2077 DOM_SID member_sid;
2078 enum lsa_SidType member_type;
2079
2080 DOM_SID sid;
2081
2082 sid_copy(&sid, alias_sid);
2083
2084 if (!sid_split_rid(&sid, &alias_rid)) {
2085 return NT_STATUS_UNSUCCESSFUL;
2086 }
2087
2088 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2089 &member_sid, &member_type);
2090
2091 if (!NT_STATUS_IS_OK(result)) {
2092 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2093 return result;
2094 }
2095
2096 /* Get sam policy handle */
2097 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2098 pipe_hnd->cli->desthost,
2099 MAXIMUM_ALLOWED_ACCESS,
2100 &connect_pol);
2101 if (!NT_STATUS_IS_OK(result)) {
2102 goto done;
2103 }
2104
2105 /* Get domain policy handle */
2106 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2107 &connect_pol,
2108 MAXIMUM_ALLOWED_ACCESS,
2109 &sid,
2110 &domain_pol);
2111 if (!NT_STATUS_IS_OK(result)) {
2112 goto done;
2113 }
2114
2115 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2116 &domain_pol,
2117 MAXIMUM_ALLOWED_ACCESS,
2118 alias_rid,
2119 &alias_pol);
2120
2121 if (!NT_STATUS_IS_OK(result)) {
2122 return result;
2123 }
2124
2125 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2126 &alias_pol,
2127 &member_sid);
2128
2129 if (!NT_STATUS_IS_OK(result)) {
2130 return result;
2131 }
2132
2133 done:
2134 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2135 return result;
2136}
2137
2138static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2139 const char *domain_name,
2140 struct cli_state *cli,
2141 struct rpc_pipe_client *pipe_hnd,
2142 TALLOC_CTX *mem_ctx,
2143 int argc,
2144 const char **argv)
2145{
2146 DOM_SID group_sid;
2147 enum lsa_SidType group_type;
2148
2149 if (argc != 2) {
2150 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2151 return NT_STATUS_UNSUCCESSFUL;
2152 }
2153
2154 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2155 &group_sid, &group_type))) {
2156 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2157 return NT_STATUS_UNSUCCESSFUL;
2158 }
2159
2160 if (group_type == SID_NAME_DOM_GRP) {
2161 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2162 &group_sid, argv[1]);
2163
2164 if (!NT_STATUS_IS_OK(result)) {
2165 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2166 argv[1], argv[0], nt_errstr(result));
2167 }
2168 return result;
2169 }
2170
2171 if (group_type == SID_NAME_ALIAS) {
2172 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2173 &group_sid, argv[1]);
2174
2175 if (!NT_STATUS_IS_OK(result)) {
2176 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2177 argv[1], argv[0], nt_errstr(result));
2178 }
2179 return result;
2180 }
2181
2182 d_fprintf(stderr, "Can only add members to global or local groups "
2183 "which %s is not\n", argv[0]);
2184
2185 return NT_STATUS_UNSUCCESSFUL;
2186}
2187
2188static int rpc_group_addmem(int argc, const char **argv)
2189{
2190 return run_rpc_command(NULL, PI_SAMR, 0,
2191 rpc_group_addmem_internals,
2192 argc, argv);
2193}
2194
2195static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2196 TALLOC_CTX *mem_ctx,
2197 const DOM_SID *group_sid,
2198 const char *member)
2199{
2200 POLICY_HND connect_pol, domain_pol;
2201 NTSTATUS result;
2202 uint32 group_rid;
2203 POLICY_HND group_pol;
2204
2205 struct samr_Ids rids, rid_types;
2206 struct lsa_String lsa_acct_name;
2207
2208 DOM_SID sid;
2209
2210 sid_copy(&sid, group_sid);
2211
2212 if (!sid_split_rid(&sid, &group_rid))
2213 return NT_STATUS_UNSUCCESSFUL;
2214
2215 /* Get sam policy handle */
2216 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2217 pipe_hnd->cli->desthost,
2218 MAXIMUM_ALLOWED_ACCESS,
2219 &connect_pol);
2220 if (!NT_STATUS_IS_OK(result))
2221 return result;
2222
2223 /* Get domain policy handle */
2224 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2225 &connect_pol,
2226 MAXIMUM_ALLOWED_ACCESS,
2227 &sid,
2228 &domain_pol);
2229 if (!NT_STATUS_IS_OK(result))
2230 return result;
2231
2232 init_lsa_String(&lsa_acct_name, member);
2233
2234 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2235 &domain_pol,
2236 1,
2237 &lsa_acct_name,
2238 &rids,
2239 &rid_types);
2240 if (!NT_STATUS_IS_OK(result)) {
2241 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2242 goto done;
2243 }
2244
2245 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2246 &domain_pol,
2247 MAXIMUM_ALLOWED_ACCESS,
2248 group_rid,
2249 &group_pol);
2250
2251 if (!NT_STATUS_IS_OK(result))
2252 goto done;
2253
2254 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2255 &group_pol,
2256 rids.ids[0]);
2257
2258 done:
2259 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2260 return result;
2261}
2262
2263static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2264 TALLOC_CTX *mem_ctx,
2265 const DOM_SID *alias_sid,
2266 const char *member)
2267{
2268 POLICY_HND connect_pol, domain_pol;
2269 NTSTATUS result;
2270 uint32 alias_rid;
2271 POLICY_HND alias_pol;
2272
2273 DOM_SID member_sid;
2274 enum lsa_SidType member_type;
2275
2276 DOM_SID sid;
2277
2278 sid_copy(&sid, alias_sid);
2279
2280 if (!sid_split_rid(&sid, &alias_rid))
2281 return NT_STATUS_UNSUCCESSFUL;
2282
2283 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2284 &member_sid, &member_type);
2285
2286 if (!NT_STATUS_IS_OK(result)) {
2287 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2288 return result;
2289 }
2290
2291 /* Get sam policy handle */
2292 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2293 pipe_hnd->cli->desthost,
2294 MAXIMUM_ALLOWED_ACCESS,
2295 &connect_pol);
2296 if (!NT_STATUS_IS_OK(result)) {
2297 goto done;
2298 }
2299
2300 /* Get domain policy handle */
2301 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2302 &connect_pol,
2303 MAXIMUM_ALLOWED_ACCESS,
2304 &sid,
2305 &domain_pol);
2306 if (!NT_STATUS_IS_OK(result)) {
2307 goto done;
2308 }
2309
2310 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2311 &domain_pol,
2312 MAXIMUM_ALLOWED_ACCESS,
2313 alias_rid,
2314 &alias_pol);
2315
2316 if (!NT_STATUS_IS_OK(result))
2317 return result;
2318
2319 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2320 &alias_pol,
2321 &member_sid);
2322
2323 if (!NT_STATUS_IS_OK(result))
2324 return result;
2325
2326 done:
2327 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2328 return result;
2329}
2330
2331static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2332 const char *domain_name,
2333 struct cli_state *cli,
2334 struct rpc_pipe_client *pipe_hnd,
2335 TALLOC_CTX *mem_ctx,
2336 int argc,
2337 const char **argv)
2338{
2339 DOM_SID group_sid;
2340 enum lsa_SidType group_type;
2341
2342 if (argc != 2) {
2343 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2344 return NT_STATUS_UNSUCCESSFUL;
2345 }
2346
2347 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2348 &group_sid, &group_type))) {
2349 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2350 return NT_STATUS_UNSUCCESSFUL;
2351 }
2352
2353 if (group_type == SID_NAME_DOM_GRP) {
2354 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2355 &group_sid, argv[1]);
2356
2357 if (!NT_STATUS_IS_OK(result)) {
2358 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2359 argv[1], argv[0], nt_errstr(result));
2360 }
2361 return result;
2362 }
2363
2364 if (group_type == SID_NAME_ALIAS) {
2365 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2366 &group_sid, argv[1]);
2367
2368 if (!NT_STATUS_IS_OK(result)) {
2369 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2370 argv[1], argv[0], nt_errstr(result));
2371 }
2372 return result;
2373 }
2374
2375 d_fprintf(stderr, "Can only delete members from global or local groups "
2376 "which %s is not\n", argv[0]);
2377
2378 return NT_STATUS_UNSUCCESSFUL;
2379}
2380
2381static int rpc_group_delmem(int argc, const char **argv)
2382{
2383 return run_rpc_command(NULL, PI_SAMR, 0,
2384 rpc_group_delmem_internals,
2385 argc, argv);
2386}
2387
2388/**
2389 * List groups on a remote RPC server.
2390 *
2391 * All parameters are provided by the run_rpc_command function, except for
2392 * argc, argv which are passed through.
2393 *
2394 * @param domain_sid The domain sid acquired from the remote server.
2395 * @param cli A cli_state connected to the server.
2396 * @param mem_ctx Talloc context, destroyed on completion of the function.
2397 * @param argc Standard main() style argc.
2398 * @param argv Standard main() style argv. Initial components are already
2399 * stripped.
2400 *
2401 * @return Normal NTSTATUS return.
2402 **/
2403
2404static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2405 const char *domain_name,
2406 struct cli_state *cli,
2407 struct rpc_pipe_client *pipe_hnd,
2408 TALLOC_CTX *mem_ctx,
2409 int argc,
2410 const char **argv)
2411{
2412 POLICY_HND connect_pol, domain_pol;
2413 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2414 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2415 struct samr_SamArray *groups = NULL;
2416 bool global = False;
2417 bool local = False;
2418 bool builtin = False;
2419
2420 if (argc == 0) {
2421 global = True;
2422 local = True;
2423 builtin = True;
2424 }
2425
2426 for (i=0; i<argc; i++) {
2427 if (strequal(argv[i], "global"))
2428 global = True;
2429
2430 if (strequal(argv[i], "local"))
2431 local = True;
2432
2433 if (strequal(argv[i], "builtin"))
2434 builtin = True;
2435 }
2436
2437 /* Get sam policy handle */
2438
2439 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2440 pipe_hnd->cli->desthost,
2441 MAXIMUM_ALLOWED_ACCESS,
2442 &connect_pol);
2443 if (!NT_STATUS_IS_OK(result)) {
2444 goto done;
2445 }
2446
2447 /* Get domain policy handle */
2448
2449 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2450 &connect_pol,
2451 MAXIMUM_ALLOWED_ACCESS,
2452 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2453 &domain_pol);
2454 if (!NT_STATUS_IS_OK(result)) {
2455 goto done;
2456 }
2457
2458 /* Query domain groups */
2459 if (opt_long_list_entries)
2460 d_printf("\nGroup name Comment"\
2461 "\n-----------------------------\n");
2462 do {
2463 uint32_t max_size, total_size, returned_size;
2464 union samr_DispInfo info;
2465
2466 if (!global) break;
2467
2468 get_query_dispinfo_params(
2469 loop_count, &max_entries, &max_size);
2470
2471 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2472 &domain_pol,
2473 3,
2474 start_idx,
2475 max_entries,
2476 max_size,
2477 &total_size,
2478 &returned_size,
2479 &info);
2480 num_entries = info.info3.count;
2481 start_idx += info.info3.count;
2482
2483 if (!NT_STATUS_IS_OK(result) &&
2484 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2485 break;
2486
2487 for (i = 0; i < num_entries; i++) {
2488
2489 const char *group = NULL;
2490 const char *desc = NULL;
2491
2492 group = info.info3.entries[i].account_name.string;
2493 desc = info.info3.entries[i].description.string;
2494
2495 if (opt_long_list_entries)
2496 printf("%-21.21s %-50.50s\n",
2497 group, desc);
2498 else
2499 printf("%s\n", group);
2500 }
2501 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2502 /* query domain aliases */
2503 start_idx = 0;
2504 do {
2505 if (!local) break;
2506
2507 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2508 &domain_pol,
2509 &start_idx,
2510 &groups,
2511 0xffff,
2512 &num_entries);
2513 if (!NT_STATUS_IS_OK(result) &&
2514 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2515 break;
2516
2517 for (i = 0; i < num_entries; i++) {
2518
2519 const char *description = NULL;
2520
2521 if (opt_long_list_entries) {
2522
2523 POLICY_HND alias_pol;
2524 union samr_AliasInfo *info = NULL;
2525
2526 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2527 &domain_pol,
2528 0x8,
2529 groups->entries[i].idx,
2530 &alias_pol))) &&
2531 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2532 &alias_pol,
2533 3,
2534 &info))) &&
2535 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2536 &alias_pol)))) {
2537 description = info->description.string;
2538 }
2539 }
2540
2541 if (description != NULL) {
2542 printf("%-21.21s %-50.50s\n",
2543 groups->entries[i].name.string,
2544 description);
2545 } else {
2546 printf("%s\n", groups->entries[i].name.string);
2547 }
2548 }
2549 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2550 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2551 /* Get builtin policy handle */
2552
2553 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2554 &connect_pol,
2555 MAXIMUM_ALLOWED_ACCESS,
2556 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2557 &domain_pol);
2558 if (!NT_STATUS_IS_OK(result)) {
2559 goto done;
2560 }
2561 /* query builtin aliases */
2562 start_idx = 0;
2563 do {
2564 if (!builtin) break;
2565
2566 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2567 &domain_pol,
2568 &start_idx,
2569 &groups,
2570 max_entries,
2571 &num_entries);
2572 if (!NT_STATUS_IS_OK(result) &&
2573 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2574 break;
2575
2576 for (i = 0; i < num_entries; i++) {
2577
2578 const char *description = NULL;
2579
2580 if (opt_long_list_entries) {
2581
2582 POLICY_HND alias_pol;
2583 union samr_AliasInfo *info = NULL;
2584
2585 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2586 &domain_pol,
2587 0x8,
2588 groups->entries[i].idx,
2589 &alias_pol))) &&
2590 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2591 &alias_pol,
2592 3,
2593 &info))) &&
2594 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2595 &alias_pol)))) {
2596 description = info->description.string;
2597 }
2598 }
2599
2600 if (description != NULL) {
2601 printf("%-21.21s %-50.50s\n",
2602 groups->entries[i].name.string,
2603 description);
2604 } else {
2605 printf("%s\n", groups->entries[i].name.string);
2606 }
2607 }
2608 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2609
2610 done:
2611 return result;
2612}
2613
2614static int rpc_group_list(int argc, const char **argv)
2615{
2616 return run_rpc_command(NULL, PI_SAMR, 0,
2617 rpc_group_list_internals,
2618 argc, argv);
2619}
2620
2621static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2622 TALLOC_CTX *mem_ctx,
2623 const char *domain_name,
2624 const DOM_SID *domain_sid,
2625 POLICY_HND *domain_pol,
2626 uint32 rid)
2627{
2628 NTSTATUS result;
2629 POLICY_HND group_pol;
2630 uint32 num_members, *group_rids;
2631 int i;
2632 struct samr_RidTypeArray *rids = NULL;
2633 struct lsa_Strings names;
2634 struct samr_Ids types;
2635
2636 fstring sid_str;
2637 sid_to_fstring(sid_str, domain_sid);
2638
2639 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2640 domain_pol,
2641 MAXIMUM_ALLOWED_ACCESS,
2642 rid,
2643 &group_pol);
2644
2645 if (!NT_STATUS_IS_OK(result))
2646 return result;
2647
2648 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2649 &group_pol,
2650 &rids);
2651
2652 if (!NT_STATUS_IS_OK(result))
2653 return result;
2654
2655 num_members = rids->count;
2656 group_rids = rids->rids;
2657
2658 while (num_members > 0) {
2659 int this_time = 512;
2660
2661 if (num_members < this_time)
2662 this_time = num_members;
2663
2664 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2665 domain_pol,
2666 this_time,
2667 group_rids,
2668 &names,
2669 &types);
2670
2671 if (!NT_STATUS_IS_OK(result))
2672 return result;
2673
2674 /* We only have users as members, but make the output
2675 the same as the output of alias members */
2676
2677 for (i = 0; i < this_time; i++) {
2678
2679 if (opt_long_list_entries) {
2680 printf("%s-%d %s\\%s %d\n", sid_str,
2681 group_rids[i], domain_name,
2682 names.names[i].string,
2683 SID_NAME_USER);
2684 } else {
2685 printf("%s\\%s\n", domain_name,
2686 names.names[i].string);
2687 }
2688 }
2689
2690 num_members -= this_time;
2691 group_rids += 512;
2692 }
2693
2694 return NT_STATUS_OK;
2695}
2696
2697static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2698 TALLOC_CTX *mem_ctx,
2699 POLICY_HND *domain_pol,
2700 uint32 rid)
2701{
2702 NTSTATUS result;
2703 struct rpc_pipe_client *lsa_pipe;
2704 POLICY_HND alias_pol, lsa_pol;
2705 uint32 num_members;
2706 DOM_SID *alias_sids;
2707 char **domains;
2708 char **names;
2709 enum lsa_SidType *types;
2710 int i;
2711 struct lsa_SidArray sid_array;
2712
2713 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2714 domain_pol,
2715 MAXIMUM_ALLOWED_ACCESS,
2716 rid,
2717 &alias_pol);
2718
2719 if (!NT_STATUS_IS_OK(result))
2720 return result;
2721
2722 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2723 &alias_pol,
2724 &sid_array);
2725
2726 if (!NT_STATUS_IS_OK(result)) {
2727 d_fprintf(stderr, "Couldn't list alias members\n");
2728 return result;
2729 }
2730
2731 num_members = sid_array.num_sids;
2732
2733 if (num_members == 0) {
2734 return NT_STATUS_OK;
2735 }
2736
2737 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2738 if (!lsa_pipe) {
2739 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2740 nt_errstr(result) );
2741 return result;
2742 }
2743
2744 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2745 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2746
2747 if (!NT_STATUS_IS_OK(result)) {
2748 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2749 cli_rpc_pipe_close(lsa_pipe);
2750 return result;
2751 }
2752
2753 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2754 if (!alias_sids) {
2755 d_fprintf(stderr, "Out of memory\n");
2756 cli_rpc_pipe_close(lsa_pipe);
2757 return NT_STATUS_NO_MEMORY;
2758 }
2759
2760 for (i=0; i<num_members; i++) {
2761 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2762 }
2763
2764 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2765 alias_sids,
2766 &domains, &names, &types);
2767
2768 if (!NT_STATUS_IS_OK(result) &&
2769 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2770 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2771 cli_rpc_pipe_close(lsa_pipe);
2772 return result;
2773 }
2774
2775 for (i = 0; i < num_members; i++) {
2776 fstring sid_str;
2777 sid_to_fstring(sid_str, &alias_sids[i]);
2778
2779 if (opt_long_list_entries) {
2780 printf("%s %s\\%s %d\n", sid_str,
2781 domains[i] ? domains[i] : "*unknown*",
2782 names[i] ? names[i] : "*unknown*", types[i]);
2783 } else {
2784 if (domains[i])
2785 printf("%s\\%s\n", domains[i], names[i]);
2786 else
2787 printf("%s\n", sid_str);
2788 }
2789 }
2790
2791 cli_rpc_pipe_close(lsa_pipe);
2792 return NT_STATUS_OK;
2793}
2794
2795static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2796 const char *domain_name,
2797 struct cli_state *cli,
2798 struct rpc_pipe_client *pipe_hnd,
2799 TALLOC_CTX *mem_ctx,
2800 int argc,
2801 const char **argv)
2802{
2803 NTSTATUS result;
2804 POLICY_HND connect_pol, domain_pol;
2805 struct samr_Ids rids, rid_types;
2806 struct lsa_String lsa_acct_name;
2807
2808 /* Get sam policy handle */
2809
2810 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2811 pipe_hnd->cli->desthost,
2812 MAXIMUM_ALLOWED_ACCESS,
2813 &connect_pol);
2814
2815 if (!NT_STATUS_IS_OK(result))
2816 return result;
2817
2818 /* Get domain policy handle */
2819
2820 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2821 &connect_pol,
2822 MAXIMUM_ALLOWED_ACCESS,
2823 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2824 &domain_pol);
2825
2826 if (!NT_STATUS_IS_OK(result))
2827 return result;
2828
2829 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2830
2831 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2832 &domain_pol,
2833 1,
2834 &lsa_acct_name,
2835 &rids,
2836 &rid_types);
2837
2838 if (!NT_STATUS_IS_OK(result)) {
2839
2840 /* Ok, did not find it in the global sam, try with builtin */
2841
2842 DOM_SID sid_Builtin;
2843
2844 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2845
2846 sid_copy(&sid_Builtin, &global_sid_Builtin);
2847
2848 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2849 &connect_pol,
2850 MAXIMUM_ALLOWED_ACCESS,
2851 &sid_Builtin,
2852 &domain_pol);
2853
2854 if (!NT_STATUS_IS_OK(result)) {
2855 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2856 return result;
2857 }
2858
2859 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2860 &domain_pol,
2861 1,
2862 &lsa_acct_name,
2863 &rids,
2864 &rid_types);
2865
2866 if (!NT_STATUS_IS_OK(result)) {
2867 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2868 return result;
2869 }
2870 }
2871
2872 if (rids.count != 1) {
2873 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2874 return result;
2875 }
2876
2877 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2878 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2879 domain_sid, &domain_pol,
2880 rids.ids[0]);
2881 }
2882
2883 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2884 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2885 rids.ids[0]);
2886 }
2887
2888 return NT_STATUS_NO_SUCH_GROUP;
2889}
2890
2891static int rpc_group_members(int argc, const char **argv)
2892{
2893 if (argc != 1) {
2894 return rpc_group_usage(argc, argv);
2895 }
2896
2897 return run_rpc_command(NULL, PI_SAMR, 0,
2898 rpc_group_members_internals,
2899 argc, argv);
2900}
2901
2902static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2903 const char *domain_name,
2904 struct cli_state *cli,
2905 struct rpc_pipe_client *pipe_hnd,
2906 TALLOC_CTX *mem_ctx,
2907 int argc,
2908 const char **argv)
2909{
2910 NTSTATUS result;
2911 POLICY_HND connect_pol, domain_pol, group_pol;
2912 union samr_GroupInfo group_info;
2913 struct samr_Ids rids, rid_types;
2914 struct lsa_String lsa_acct_name;
2915
2916 if (argc != 2) {
2917 d_printf("Usage: 'net rpc group rename group newname'\n");
2918 return NT_STATUS_UNSUCCESSFUL;
2919 }
2920
2921 /* Get sam policy handle */
2922
2923 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2924 pipe_hnd->cli->desthost,
2925 MAXIMUM_ALLOWED_ACCESS,
2926 &connect_pol);
2927
2928 if (!NT_STATUS_IS_OK(result))
2929 return result;
2930
2931 /* Get domain policy handle */
2932
2933 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2934 &connect_pol,
2935 MAXIMUM_ALLOWED_ACCESS,
2936 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2937 &domain_pol);
2938
2939 if (!NT_STATUS_IS_OK(result))
2940 return result;
2941
2942 init_lsa_String(&lsa_acct_name, argv[0]);
2943
2944 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2945 &domain_pol,
2946 1,
2947 &lsa_acct_name,
2948 &rids,
2949 &rid_types);
2950
2951 if (rids.count != 1) {
2952 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2953 return result;
2954 }
2955
2956 if (rid_types.ids[0] != SID_NAME_DOM_GRP) {
2957 d_fprintf(stderr, "Can only rename domain groups\n");
2958 return NT_STATUS_UNSUCCESSFUL;
2959 }
2960
2961 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2962 &domain_pol,
2963 MAXIMUM_ALLOWED_ACCESS,
2964 rids.ids[0],
2965 &group_pol);
2966
2967 if (!NT_STATUS_IS_OK(result))
2968 return result;
2969
2970 init_lsa_String(&group_info.name, argv[1]);
2971
2972 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
2973 &group_pol,
2974 2,
2975 &group_info);
2976
2977 if (!NT_STATUS_IS_OK(result))
2978 return result;
2979
2980 return NT_STATUS_NO_SUCH_GROUP;
2981}
2982
2983static int rpc_group_rename(int argc, const char **argv)
2984{
2985 if (argc != 2) {
2986 return rpc_group_usage(argc, argv);
2987 }
2988
2989 return run_rpc_command(NULL, PI_SAMR, 0,
2990 rpc_group_rename_internals,
2991 argc, argv);
2992}
2993
2994/**
2995 * 'net rpc group' entrypoint.
2996 * @param argc Standard main() style argc.
2997 * @param argv Standard main() style argv. Initial components are already
2998 * stripped.
2999 **/
3000
3001int net_rpc_group(int argc, const char **argv)
3002{
3003 struct functable func[] = {
3004 {"add", rpc_group_add},
3005 {"delete", rpc_group_delete},
3006 {"addmem", rpc_group_addmem},
3007 {"delmem", rpc_group_delmem},
3008 {"list", rpc_group_list},
3009 {"members", rpc_group_members},
3010 {"rename", rpc_group_rename},
3011 {NULL, NULL}
3012 };
3013
3014 if (argc == 0) {
3015 return run_rpc_command(NULL, PI_SAMR, 0,
3016 rpc_group_list_internals,
3017 argc, argv);
3018 }
3019
3020 return net_run_function(argc, argv, func, rpc_group_usage);
3021}
3022
3023/****************************************************************************/
3024
3025static int rpc_share_usage(int argc, const char **argv)
3026{
3027 return net_help_share(argc, argv);
3028}
3029
3030/**
3031 * Add a share on a remote RPC server.
3032 *
3033 * All parameters are provided by the run_rpc_command function, except for
3034 * argc, argv which are passed through.
3035 *
3036 * @param domain_sid The domain sid acquired from the remote server.
3037 * @param cli A cli_state connected to the server.
3038 * @param mem_ctx Talloc context, destroyed on completion of the function.
3039 * @param argc Standard main() style argc.
3040 * @param argv Standard main() style argv. Initial components are already
3041 * stripped.
3042 *
3043 * @return Normal NTSTATUS return.
3044 **/
3045static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3046 const char *domain_name,
3047 struct cli_state *cli,
3048 struct rpc_pipe_client *pipe_hnd,
3049 TALLOC_CTX *mem_ctx,int argc,
3050 const char **argv)
3051{
3052 WERROR result;
3053 NTSTATUS status;
3054 char *sharename;
3055 char *path;
3056 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3057 uint32 num_users=0, perms=0;
3058 char *password=NULL; /* don't allow a share password */
3059 uint32 level = 2;
3060 union srvsvc_NetShareInfo info;
3061 struct srvsvc_NetShareInfo2 info2;
3062 uint32_t parm_error = 0;
3063
3064 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3065 return NT_STATUS_NO_MEMORY;
3066 }
3067
3068 path = strchr(sharename, '=');
3069 if (!path)
3070 return NT_STATUS_UNSUCCESSFUL;
3071 *path++ = '\0';
3072
3073 info2.name = sharename;
3074 info2.type = type;
3075 info2.comment = opt_comment;
3076 info2.permissions = perms;
3077 info2.max_users = opt_maxusers;
3078 info2.current_users = num_users;
3079 info2.path = path;
3080 info2.password = password;
3081
3082 info.info2 = &info2;
3083
3084 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3085 pipe_hnd->cli->desthost,
3086 level,
3087 &info,
3088 &parm_error,
3089 &result);
3090 return status;
3091}
3092
3093static int rpc_share_add(int argc, const char **argv)
3094{
3095 if ((argc < 1) || !strchr(argv[0], '=')) {
3096 DEBUG(1,("Sharename or path not specified on add\n"));
3097 return rpc_share_usage(argc, argv);
3098 }
3099 return run_rpc_command(NULL, PI_SRVSVC, 0,
3100 rpc_share_add_internals,
3101 argc, argv);
3102}
3103
3104/**
3105 * Delete a share on a remote RPC server.
3106 *
3107 * All parameters are provided by the run_rpc_command function, except for
3108 * argc, argv which are passed through.
3109 *
3110 * @param domain_sid The domain sid acquired from the remote server.
3111 * @param cli A cli_state connected to the server.
3112 * @param mem_ctx Talloc context, destroyed on completion of the function.
3113 * @param argc Standard main() style argc.
3114 * @param argv Standard main() style argv. Initial components are already
3115 * stripped.
3116 *
3117 * @return Normal NTSTATUS return.
3118 **/
3119static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3120 const char *domain_name,
3121 struct cli_state *cli,
3122 struct rpc_pipe_client *pipe_hnd,
3123 TALLOC_CTX *mem_ctx,
3124 int argc,
3125 const char **argv)
3126{
3127 WERROR result;
3128
3129 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3130 pipe_hnd->cli->desthost,
3131 argv[0],
3132 0,
3133 &result);
3134}
3135
3136/**
3137 * Delete a share on a remote RPC server.
3138 *
3139 * @param domain_sid The domain sid acquired from the remote server.
3140 * @param argc Standard main() style argc.
3141 * @param argv Standard main() style argv. Initial components are already
3142 * stripped.
3143 *
3144 * @return A shell status integer (0 for success).
3145 **/
3146static int rpc_share_delete(int argc, const char **argv)
3147{
3148 if (argc < 1) {
3149 DEBUG(1,("Sharename not specified on delete\n"));
3150 return rpc_share_usage(argc, argv);
3151 }
3152 return run_rpc_command(NULL, PI_SRVSVC, 0,
3153 rpc_share_del_internals,
3154 argc, argv);
3155}
3156
3157/**
3158 * Formatted print of share info
3159 *
3160 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3161 **/
3162
3163static void display_share_info_1(struct srvsvc_NetShareInfo1 *r)
3164{
3165 if (opt_long_list_entries) {
3166 d_printf("%-12s %-8.8s %-50s\n",
3167 r->name,
3168 share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3169 r->comment);
3170 } else {
3171 d_printf("%s\n", r->name);
3172 }
3173}
3174
3175static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3176 TALLOC_CTX *mem_ctx,
3177 uint32 level,
3178 int argc,
3179 const char **argv,
3180 struct srvsvc_NetShareInfoCtr *info_ctr)
3181{
3182 WERROR result;
3183 NTSTATUS status;
3184 union srvsvc_NetShareInfo info;
3185
3186 /* no specific share requested, enumerate all */
3187 if (argc == 0) {
3188
3189 uint32_t preferred_len = 0xffffffff;
3190 uint32_t total_entries = 0;
3191 uint32_t resume_handle = 0;
3192
3193 info_ctr->level = level;
3194
3195 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3196 pipe_hnd->cli->desthost,
3197 info_ctr,
3198 preferred_len,
3199 &total_entries,
3200 &resume_handle,
3201 &result);
3202 return result;
3203 }
3204
3205 /* request just one share */
3206 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3207 pipe_hnd->cli->desthost,
3208 argv[0],
3209 level,
3210 &info,
3211 &result);
3212
3213 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3214 goto done;
3215 }
3216
3217 /* construct ctr */
3218 ZERO_STRUCTP(info_ctr);
3219
3220 info_ctr->level = level;
3221
3222 switch (level) {
3223 case 1:
3224 {
3225 struct srvsvc_NetShareCtr1 *ctr1;
3226
3227 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3228 W_ERROR_HAVE_NO_MEMORY(ctr1);
3229
3230 ctr1->count = 1;
3231 ctr1->array = info.info1;
3232
3233 info_ctr->ctr.ctr1 = ctr1;
3234 }
3235 case 2:
3236 {
3237 struct srvsvc_NetShareCtr2 *ctr2;
3238
3239 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3240 W_ERROR_HAVE_NO_MEMORY(ctr2);
3241
3242 ctr2->count = 1;
3243 ctr2->array = info.info2;
3244
3245 info_ctr->ctr.ctr2 = ctr2;
3246 }
3247 case 502:
3248 {
3249 struct srvsvc_NetShareCtr502 *ctr502;
3250
3251 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3252 W_ERROR_HAVE_NO_MEMORY(ctr502);
3253
3254 ctr502->count = 1;
3255 ctr502->array = info.info502;
3256
3257 info_ctr->ctr.ctr502 = ctr502;
3258 }
3259 } /* switch */
3260done:
3261 return result;
3262}
3263
3264/**
3265 * List shares on a remote RPC server.
3266 *
3267 * All parameters are provided by the run_rpc_command function, except for
3268 * argc, argv which are passed through.
3269 *
3270 * @param domain_sid The domain sid acquired from the remote server.
3271 * @param cli A cli_state connected to the server.
3272 * @param mem_ctx Talloc context, destroyed on completion of the function.
3273 * @param argc Standard main() style argc.
3274 * @param argv Standard main() style argv. Initial components are already
3275 * stripped.
3276 *
3277 * @return Normal NTSTATUS return.
3278 **/
3279
3280static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3281 const char *domain_name,
3282 struct cli_state *cli,
3283 struct rpc_pipe_client *pipe_hnd,
3284 TALLOC_CTX *mem_ctx,
3285 int argc,
3286 const char **argv)
3287{
3288 struct srvsvc_NetShareInfoCtr info_ctr;
3289 struct srvsvc_NetShareCtr1 ctr1;
3290 WERROR result;
3291 uint32 i, level = 1;
3292
3293 ZERO_STRUCT(info_ctr);
3294 ZERO_STRUCT(ctr1);
3295
3296 info_ctr.level = 1;
3297 info_ctr.ctr.ctr1 = &ctr1;
3298
3299 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &info_ctr);
3300 if (!W_ERROR_IS_OK(result))
3301 goto done;
3302
3303 /* Display results */
3304
3305 if (opt_long_list_entries) {
3306 d_printf(
3307 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3308 "\nShare name Type Description\n"\
3309 "---------- ---- -----------\n");
3310 }
3311 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3312 display_share_info_1(&info_ctr.ctr.ctr1->array[i]);
3313 done:
3314 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3315}
3316
3317/***
3318 * 'net rpc share list' entrypoint.
3319 * @param argc Standard main() style argc
3320 * @param argv Standard main() style argv. Initial components are already
3321 * stripped.
3322 **/
3323static int rpc_share_list(int argc, const char **argv)
3324{
3325 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3326}
3327
3328static bool check_share_availability(struct cli_state *cli, const char *netname)
3329{
3330 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3331 d_printf("skipping [%s]: not a file share.\n", netname);
3332 return False;
3333 }
3334
3335 if (!cli_tdis(cli))
3336 return False;
3337
3338 return True;
3339}
3340
3341static bool check_share_sanity(struct cli_state *cli, const char *netname, uint32 type)
3342{
3343 /* only support disk shares */
3344 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3345 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3346 return False;
3347 }
3348
3349 /* skip builtin shares */
3350 /* FIXME: should print$ be added too ? */
3351 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3352 strequal(netname,"global"))
3353 return False;
3354
3355 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3356 printf("excluding [%s]\n", netname);
3357 return False;
3358 }
3359
3360 return check_share_availability(cli, netname);
3361}
3362
3363/**
3364 * Migrate shares from a remote RPC server to the local RPC server.
3365 *
3366 * All parameters are provided by the run_rpc_command function, except for
3367 * argc, argv which are passed through.
3368 *
3369 * @param domain_sid The domain sid acquired from the remote server.
3370 * @param cli A cli_state connected to the server.
3371 * @param mem_ctx Talloc context, destroyed on completion of the function.
3372 * @param argc Standard main() style argc.
3373 * @param argv Standard main() style argv. Initial components are already
3374 * stripped.
3375 *
3376 * @return Normal NTSTATUS return.
3377 **/
3378
3379static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3380 const char *domain_name,
3381 struct cli_state *cli,
3382 struct rpc_pipe_client *pipe_hnd,
3383 TALLOC_CTX *mem_ctx,
3384 int argc,
3385 const char **argv)
3386{
3387 WERROR result;
3388 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3389 struct srvsvc_NetShareInfoCtr ctr_src;
3390 uint32 i;
3391 struct rpc_pipe_client *srvsvc_pipe = NULL;
3392 struct cli_state *cli_dst = NULL;
3393 uint32 level = 502; /* includes secdesc */
3394 uint32_t parm_error = 0;
3395
3396 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3397 if (!W_ERROR_IS_OK(result))
3398 goto done;
3399
3400 /* connect destination PI_SRVSVC */
3401 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3402 if (!NT_STATUS_IS_OK(nt_status))
3403 return nt_status;
3404
3405
3406 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3407
3408 union srvsvc_NetShareInfo info;
3409 struct srvsvc_NetShareInfo502 info502 =
3410 ctr_src.ctr.ctr502->array[i];
3411
3412 /* reset error-code */
3413 nt_status = NT_STATUS_UNSUCCESSFUL;
3414
3415 if (!check_share_sanity(cli, info502.name, info502.type))
3416 continue;
3417
3418 /* finally add the share on the dst server */
3419
3420 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3421 info502.name, info502.path, info502.comment);
3422
3423 info.info502 = &info502;
3424
3425 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3426 srvsvc_pipe->cli->desthost,
3427 502,
3428 &info,
3429 &parm_error,
3430 &result);
3431
3432 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3433 printf(" [%s] does already exist\n",
3434 info502.name);
3435 continue;
3436 }
3437
3438 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3439 printf("cannot add share: %s\n", dos_errstr(result));
3440 goto done;
3441 }
3442
3443 }
3444
3445 nt_status = NT_STATUS_OK;
3446
3447done:
3448 if (cli_dst) {
3449 cli_shutdown(cli_dst);
3450 }
3451
3452 return nt_status;
3453
3454}
3455
3456/**
3457 * Migrate shares from a rpc-server to another.
3458 *
3459 * @param argc Standard main() style argc.
3460 * @param argv Standard main() style argv. Initial components are already
3461 * stripped.
3462 *
3463 * @return A shell status integer (0 for success).
3464 **/
3465static int rpc_share_migrate_shares(int argc, const char **argv)
3466{
3467
3468 if (!opt_host) {
3469 printf("no server to migrate\n");
3470 return -1;
3471 }
3472
3473 return run_rpc_command(NULL, PI_SRVSVC, 0,
3474 rpc_share_migrate_shares_internals,
3475 argc, argv);
3476}
3477
3478/**
3479 * Copy a file/dir
3480 *
3481 * @param f file_info
3482 * @param mask current search mask
3483 * @param state arg-pointer
3484 *
3485 **/
3486static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3487{
3488 static NTSTATUS nt_status;
3489 static struct copy_clistate *local_state;
3490 static fstring filename, new_mask;
3491 fstring dir;
3492 char *old_dir;
3493
3494 local_state = (struct copy_clistate *)state;
3495 nt_status = NT_STATUS_UNSUCCESSFUL;
3496
3497 if (strequal(f->name, ".") || strequal(f->name, ".."))
3498 return;
3499
3500 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3501
3502 /* DIRECTORY */
3503 if (f->mode & aDIR) {
3504
3505 DEBUG(3,("got dir: %s\n", f->name));
3506
3507 fstrcpy(dir, local_state->cwd);
3508 fstrcat(dir, "\\");
3509 fstrcat(dir, f->name);
3510
3511 switch (net_mode_share)
3512 {
3513 case NET_MODE_SHARE_MIGRATE:
3514 /* create that directory */
3515 nt_status = net_copy_file(local_state->mem_ctx,
3516 local_state->cli_share_src,
3517 local_state->cli_share_dst,
3518 dir, dir,
3519 opt_acls? True : False,
3520 opt_attrs? True : False,
3521 opt_timestamps? True : False,
3522 False);
3523 break;
3524 default:
3525 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3526 return;
3527 }
3528
3529 if (!NT_STATUS_IS_OK(nt_status))
3530 printf("could not handle dir %s: %s\n",
3531 dir, nt_errstr(nt_status));
3532
3533 /* search below that directory */
3534 fstrcpy(new_mask, dir);
3535 fstrcat(new_mask, "\\*");
3536
3537 old_dir = local_state->cwd;
3538 local_state->cwd = dir;
3539 if (!sync_files(local_state, new_mask))
3540 printf("could not handle files\n");
3541 local_state->cwd = old_dir;
3542
3543 return;
3544 }
3545
3546
3547 /* FILE */
3548 fstrcpy(filename, local_state->cwd);
3549 fstrcat(filename, "\\");
3550 fstrcat(filename, f->name);
3551
3552 DEBUG(3,("got file: %s\n", filename));
3553
3554 switch (net_mode_share)
3555 {
3556 case NET_MODE_SHARE_MIGRATE:
3557 nt_status = net_copy_file(local_state->mem_ctx,
3558 local_state->cli_share_src,
3559 local_state->cli_share_dst,
3560 filename, filename,
3561 opt_acls? True : False,
3562 opt_attrs? True : False,
3563 opt_timestamps? True: False,
3564 True);
3565 break;
3566 default:
3567 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3568 return;
3569 }
3570
3571 if (!NT_STATUS_IS_OK(nt_status))
3572 printf("could not handle file %s: %s\n",
3573 filename, nt_errstr(nt_status));
3574
3575}
3576
3577/**
3578 * sync files, can be called recursivly to list files
3579 * and then call copy_fn for each file
3580 *
3581 * @param cp_clistate pointer to the copy_clistate we work with
3582 * @param mask the current search mask
3583 *
3584 * @return Boolean result
3585 **/
3586static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3587{
3588 struct cli_state *targetcli;
3589 char *targetpath = NULL;
3590
3591 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3592
3593 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3594 mask, &targetcli, &targetpath ) ) {
3595 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3596 mask, cli_errstr(cp_clistate->cli_share_src));
3597 return False;
3598 }
3599
3600 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3601 d_fprintf(stderr, "listing %s failed with error: %s\n",
3602 mask, cli_errstr(targetcli));
3603 return False;
3604 }
3605
3606 return True;
3607}
3608
3609
3610/**
3611 * Set the top level directory permissions before we do any further copies.
3612 * Should set up ACL inheritance.
3613 **/
3614
3615bool copy_top_level_perms(struct copy_clistate *cp_clistate,
3616 const char *sharename)
3617{
3618 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3619
3620 switch (net_mode_share) {
3621 case NET_MODE_SHARE_MIGRATE:
3622 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3623 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3624 cp_clistate->cli_share_src,
3625 cp_clistate->cli_share_dst,
3626 "\\", "\\",
3627 opt_acls? True : False,
3628 opt_attrs? True : False,
3629 opt_timestamps? True: False,
3630 False);
3631 break;
3632 default:
3633 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3634 break;
3635 }
3636
3637 if (!NT_STATUS_IS_OK(nt_status)) {
3638 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3639 sharename, nt_errstr(nt_status));
3640 return False;
3641 }
3642
3643 return True;
3644}
3645
3646/**
3647 * Sync all files inside a remote share to another share (over smb).
3648 *
3649 * All parameters are provided by the run_rpc_command function, except for
3650 * argc, argv which are passed through.
3651 *
3652 * @param domain_sid The domain sid acquired from the remote server.
3653 * @param cli A cli_state connected to the server.
3654 * @param mem_ctx Talloc context, destroyed on completion of the function.
3655 * @param argc Standard main() style argc.
3656 * @param argv Standard main() style argv. Initial components are already
3657 * stripped.
3658 *
3659 * @return Normal NTSTATUS return.
3660 **/
3661
3662static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3663 const char *domain_name,
3664 struct cli_state *cli,
3665 struct rpc_pipe_client *pipe_hnd,
3666 TALLOC_CTX *mem_ctx,
3667 int argc,
3668 const char **argv)
3669{
3670 WERROR result;
3671 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3672 struct srvsvc_NetShareInfoCtr ctr_src;
3673 uint32 i;
3674 uint32 level = 502;
3675 struct copy_clistate cp_clistate;
3676 bool got_src_share = False;
3677 bool got_dst_share = False;
3678 const char *mask = "\\*";
3679 char *dst = NULL;
3680
3681 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3682 if (dst == NULL) {
3683 nt_status = NT_STATUS_NO_MEMORY;
3684 goto done;
3685 }
3686
3687 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3688
3689 if (!W_ERROR_IS_OK(result))
3690 goto done;
3691
3692 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3693
3694 struct srvsvc_NetShareInfo502 info502 =
3695 ctr_src.ctr.ctr502->array[i];
3696
3697 if (!check_share_sanity(cli, info502.name, info502.type))
3698 continue;
3699
3700 /* one might not want to mirror whole discs :) */
3701 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3702 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3703 continue;
3704 }
3705
3706 switch (net_mode_share)
3707 {
3708 case NET_MODE_SHARE_MIGRATE:
3709 printf("syncing");
3710 break;
3711 default:
3712 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3713 break;
3714 }
3715 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3716 info502.name,
3717 opt_acls ? "including" : "without",
3718 opt_attrs ? "including" : "without",
3719 opt_timestamps ? "(preserving timestamps)" : "");
3720
3721 cp_clistate.mem_ctx = mem_ctx;
3722 cp_clistate.cli_share_src = NULL;
3723 cp_clistate.cli_share_dst = NULL;
3724 cp_clistate.cwd = NULL;
3725 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3726
3727 /* open share source */
3728 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3729 &cli->dest_ss, cli->desthost,
3730 info502.name, "A:");
3731 if (!NT_STATUS_IS_OK(nt_status))
3732 goto done;
3733
3734 got_src_share = True;
3735
3736 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3737 /* open share destination */
3738 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3739 NULL, dst, info502.name, "A:");
3740 if (!NT_STATUS_IS_OK(nt_status))
3741 goto done;
3742
3743 got_dst_share = True;
3744 }
3745
3746 if (!copy_top_level_perms(&cp_clistate, info502.name)) {
3747 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3748 nt_status = NT_STATUS_UNSUCCESSFUL;
3749 goto done;
3750 }
3751
3752 if (!sync_files(&cp_clistate, mask)) {
3753 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3754 nt_status = NT_STATUS_UNSUCCESSFUL;
3755 goto done;
3756 }
3757 }
3758
3759 nt_status = NT_STATUS_OK;
3760
3761done:
3762
3763 if (got_src_share)
3764 cli_shutdown(cp_clistate.cli_share_src);
3765
3766 if (got_dst_share)
3767 cli_shutdown(cp_clistate.cli_share_dst);
3768
3769 SAFE_FREE(dst);
3770 return nt_status;
3771
3772}
3773
3774static int rpc_share_migrate_files(int argc, const char **argv)
3775{
3776
3777 if (!opt_host) {
3778 printf("no server to migrate\n");
3779 return -1;
3780 }
3781
3782 return run_rpc_command(NULL, PI_SRVSVC, 0,
3783 rpc_share_migrate_files_internals,
3784 argc, argv);
3785}
3786
3787/**
3788 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3789 *
3790 * All parameters are provided by the run_rpc_command function, except for
3791 * argc, argv which are passed through.
3792 *
3793 * @param domain_sid The domain sid acquired from the remote server.
3794 * @param cli A cli_state connected to the server.
3795 * @param mem_ctx Talloc context, destroyed on completion of the function.
3796 * @param argc Standard main() style argc.
3797 * @param argv Standard main() style argv. Initial components are already
3798 * stripped.
3799 *
3800 * @return Normal NTSTATUS return.
3801 **/
3802
3803static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3804 const char *domain_name,
3805 struct cli_state *cli,
3806 struct rpc_pipe_client *pipe_hnd,
3807 TALLOC_CTX *mem_ctx,
3808 int argc,
3809 const char **argv)
3810{
3811 WERROR result;
3812 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3813 struct srvsvc_NetShareInfoCtr ctr_src;
3814 union srvsvc_NetShareInfo info;
3815 uint32 i;
3816 struct rpc_pipe_client *srvsvc_pipe = NULL;
3817 struct cli_state *cli_dst = NULL;
3818 uint32 level = 502; /* includes secdesc */
3819 uint32_t parm_error = 0;
3820
3821 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3822
3823 if (!W_ERROR_IS_OK(result))
3824 goto done;
3825
3826 /* connect destination PI_SRVSVC */
3827 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3828 if (!NT_STATUS_IS_OK(nt_status))
3829 return nt_status;
3830
3831
3832 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3833
3834 struct srvsvc_NetShareInfo502 info502 =
3835 ctr_src.ctr.ctr502->array[i];
3836
3837 /* reset error-code */
3838 nt_status = NT_STATUS_UNSUCCESSFUL;
3839
3840 if (!check_share_sanity(cli, info502.name, info502.type))
3841 continue;
3842
3843 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3844 info502.name, info502.path, info502.comment);
3845
3846 if (opt_verbose)
3847 display_sec_desc(info502.sd_buf.sd);
3848
3849 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3850 info.info502 = &info502;
3851
3852 /* finally modify the share on the dst server */
3853 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3854 srvsvc_pipe->cli->desthost,
3855 info502.name,
3856 level,
3857 &info,
3858 &parm_error,
3859 &result);
3860 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3861 printf("cannot set share-acl: %s\n", dos_errstr(result));
3862 goto done;
3863 }
3864
3865 }
3866
3867 nt_status = NT_STATUS_OK;
3868
3869done:
3870 if (cli_dst) {
3871 cli_shutdown(cli_dst);
3872 }
3873
3874 return nt_status;
3875
3876}
3877
3878/**
3879 * Migrate share-acls from a rpc-server to another.
3880 *
3881 * @param argc Standard main() style argc.
3882 * @param argv Standard main() style argv. Initial components are already
3883 * stripped.
3884 *
3885 * @return A shell status integer (0 for success)
3886 **/
3887static int rpc_share_migrate_security(int argc, const char **argv)
3888{
3889
3890 if (!opt_host) {
3891 printf("no server to migrate\n");
3892 return -1;
3893 }
3894
3895 return run_rpc_command(NULL, PI_SRVSVC, 0,
3896 rpc_share_migrate_security_internals,
3897 argc, argv);
3898}
3899
3900/**
3901 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3902 * from one server to another
3903 *
3904 * @param argc Standard main() style argc.
3905 * @param argv Standard main() style argv. Initial components are already
3906 * stripped.
3907 *
3908 * @return A shell status integer (0 for success)
3909 *
3910 **/
3911static int rpc_share_migrate_all(int argc, const char **argv)
3912{
3913 int ret;
3914
3915 if (!opt_host) {
3916 printf("no server to migrate\n");
3917 return -1;
3918 }
3919
3920 /* order is important. we don't want to be locked out by the share-acl
3921 * before copying files - gd */
3922
3923 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3924 if (ret)
3925 return ret;
3926
3927 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3928 if (ret)
3929 return ret;
3930
3931 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3932}
3933
3934
3935/**
3936 * 'net rpc share migrate' entrypoint.
3937 * @param argc Standard main() style argc.
3938 * @param argv Standard main() style argv. Initial components are already
3939 * stripped.
3940 **/
3941static int rpc_share_migrate(int argc, const char **argv)
3942{
3943
3944 struct functable func[] = {
3945 {"all", rpc_share_migrate_all},
3946 {"files", rpc_share_migrate_files},
3947 {"help", rpc_share_usage},
3948 {"security", rpc_share_migrate_security},
3949 {"shares", rpc_share_migrate_shares},
3950 {NULL, NULL}
3951 };
3952
3953 net_mode_share = NET_MODE_SHARE_MIGRATE;
3954
3955 return net_run_function(argc, argv, func, rpc_share_usage);
3956}
3957
3958struct full_alias {
3959 DOM_SID sid;
3960 uint32 num_members;
3961 DOM_SID *members;
3962};
3963
3964static int num_server_aliases;
3965static struct full_alias *server_aliases;
3966
3967/*
3968 * Add an alias to the static list.
3969 */
3970static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3971{
3972 if (server_aliases == NULL)
3973 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3974
3975 server_aliases[num_server_aliases] = *alias;
3976 num_server_aliases += 1;
3977}
3978
3979/*
3980 * For a specific domain on the server, fetch all the aliases
3981 * and their members. Add all of them to the server_aliases.
3982 */
3983
3984static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3985 TALLOC_CTX *mem_ctx,
3986 POLICY_HND *connect_pol,
3987 const DOM_SID *domain_sid)
3988{
3989 uint32 start_idx, max_entries, num_entries, i;
3990 struct samr_SamArray *groups = NULL;
3991 NTSTATUS result;
3992 POLICY_HND domain_pol;
3993
3994 /* Get domain policy handle */
3995
3996 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3997 connect_pol,
3998 MAXIMUM_ALLOWED_ACCESS,
3999 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4000 &domain_pol);
4001 if (!NT_STATUS_IS_OK(result))
4002 return result;
4003
4004 start_idx = 0;
4005 max_entries = 250;
4006
4007 do {
4008 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4009 &domain_pol,
4010 &start_idx,
4011 &groups,
4012 max_entries,
4013 &num_entries);
4014 for (i = 0; i < num_entries; i++) {
4015
4016 POLICY_HND alias_pol;
4017 struct full_alias alias;
4018 struct lsa_SidArray sid_array;
4019 int j;
4020
4021 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4022 &domain_pol,
4023 MAXIMUM_ALLOWED_ACCESS,
4024 groups->entries[i].idx,
4025 &alias_pol);
4026 if (!NT_STATUS_IS_OK(result))
4027 goto done;
4028
4029 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4030 &alias_pol,
4031 &sid_array);
4032 if (!NT_STATUS_IS_OK(result))
4033 goto done;
4034
4035 alias.num_members = sid_array.num_sids;
4036
4037 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4038 if (!NT_STATUS_IS_OK(result))
4039 goto done;
4040
4041 alias.members = NULL;
4042
4043 if (alias.num_members > 0) {
4044 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4045
4046 for (j = 0; j < alias.num_members; j++)
4047 sid_copy(&alias.members[j],
4048 sid_array.sids[j].sid);
4049 }
4050
4051 sid_copy(&alias.sid, domain_sid);
4052 sid_append_rid(&alias.sid, groups->entries[i].idx);
4053
4054 push_alias(mem_ctx, &alias);
4055 }
4056 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4057
4058 result = NT_STATUS_OK;
4059
4060 done:
4061 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4062
4063 return result;
4064}
4065
4066/*
4067 * Dump server_aliases as names for debugging purposes.
4068 */
4069
4070static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
4071 const char *domain_name,
4072 struct cli_state *cli,
4073 struct rpc_pipe_client *pipe_hnd,
4074 TALLOC_CTX *mem_ctx,
4075 int argc,
4076 const char **argv)
4077{
4078 int i;
4079 NTSTATUS result;
4080 POLICY_HND lsa_pol;
4081
4082 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
4083 SEC_RIGHTS_MAXIMUM_ALLOWED,
4084 &lsa_pol);
4085 if (!NT_STATUS_IS_OK(result))
4086 return result;
4087
4088 for (i=0; i<num_server_aliases; i++) {
4089 char **names;
4090 char **domains;
4091 enum lsa_SidType *types;
4092 int j;
4093
4094 struct full_alias *alias = &server_aliases[i];
4095
4096 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4097 &alias->sid,
4098 &domains, &names, &types);
4099 if (!NT_STATUS_IS_OK(result))
4100 continue;
4101
4102 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4103
4104 if (alias->num_members == 0) {
4105 DEBUG(1, ("\n"));
4106 continue;
4107 }
4108
4109 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4110 alias->num_members,
4111 alias->members,
4112 &domains, &names, &types);
4113
4114 if (!NT_STATUS_IS_OK(result) &&
4115 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4116 continue;
4117
4118 for (j=0; j<alias->num_members; j++)
4119 DEBUG(1, ("%s\\%s (%d); ",
4120 domains[j] ? domains[j] : "*unknown*",
4121 names[j] ? names[j] : "*unknown*",types[j]));
4122 DEBUG(1, ("\n"));
4123 }
4124
4125 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4126
4127 return NT_STATUS_OK;
4128}
4129
4130/*
4131 * Fetch a list of all server aliases and their members into
4132 * server_aliases.
4133 */
4134
4135static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4136 const char *domain_name,
4137 struct cli_state *cli,
4138 struct rpc_pipe_client *pipe_hnd,
4139 TALLOC_CTX *mem_ctx,
4140 int argc,
4141 const char **argv)
4142{
4143 NTSTATUS result;
4144 POLICY_HND connect_pol;
4145
4146 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4147 pipe_hnd->cli->desthost,
4148 MAXIMUM_ALLOWED_ACCESS,
4149 &connect_pol);
4150
4151 if (!NT_STATUS_IS_OK(result))
4152 goto done;
4153
4154 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4155 &global_sid_Builtin);
4156
4157 if (!NT_STATUS_IS_OK(result))
4158 goto done;
4159
4160 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4161 domain_sid);
4162
4163 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4164 done:
4165 return result;
4166}
4167
4168static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4169{
4170 token->num_sids = 4;
4171
4172 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4173 d_fprintf(stderr, "malloc failed\n");
4174 token->num_sids = 0;
4175 return;
4176 }
4177
4178 token->user_sids[0] = *user_sid;
4179 sid_copy(&token->user_sids[1], &global_sid_World);
4180 sid_copy(&token->user_sids[2], &global_sid_Network);
4181 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4182}
4183
4184static void free_user_token(NT_USER_TOKEN *token)
4185{
4186 SAFE_FREE(token->user_sids);
4187}
4188
4189static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4190{
4191 if (is_sid_in_token(token, sid))
4192 return;
4193
4194 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4195 if (!token->user_sids) {
4196 return;
4197 }
4198
4199 sid_copy(&token->user_sids[token->num_sids], sid);
4200
4201 token->num_sids += 1;
4202}
4203
4204struct user_token {
4205 fstring name;
4206 NT_USER_TOKEN token;
4207};
4208
4209static void dump_user_token(struct user_token *token)
4210{
4211 int i;
4212
4213 d_printf("%s\n", token->name);
4214
4215 for (i=0; i<token->token.num_sids; i++) {
4216 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4217 }
4218}
4219
4220static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4221{
4222 int i;
4223
4224 for (i=0; i<alias->num_members; i++) {
4225 if (sid_compare(sid, &alias->members[i]) == 0)
4226 return True;
4227 }
4228
4229 return False;
4230}
4231
4232static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4233{
4234 int i;
4235
4236 for (i=0; i<num_server_aliases; i++) {
4237 if (is_alias_member(&sid, &server_aliases[i]))
4238 add_sid_to_token(token, &server_aliases[i].sid);
4239 }
4240}
4241
4242/*
4243 * We got a user token with all the SIDs we can know about without asking the
4244 * server directly. These are the user and domain group sids. All of these can
4245 * be members of aliases. So scan the list of aliases for each of the SIDs and
4246 * add them to the token.
4247 */
4248
4249static void collect_alias_memberships(NT_USER_TOKEN *token)
4250{
4251 int num_global_sids = token->num_sids;
4252 int i;
4253
4254 for (i=0; i<num_global_sids; i++) {
4255 collect_sid_memberships(token, token->user_sids[i]);
4256 }
4257}
4258
4259static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4260{
4261 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4262 enum wbcSidType type;
4263 fstring full_name;
4264 struct wbcDomainSid wsid;
4265 char *sid_str = NULL;
4266 DOM_SID user_sid;
4267 uint32_t num_groups;
4268 gid_t *groups = NULL;
4269 uint32_t i;
4270
4271 fstr_sprintf(full_name, "%s%c%s",
4272 domain, *lp_winbind_separator(), user);
4273
4274 /* First let's find out the user sid */
4275
4276 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4277
4278 if (!WBC_ERROR_IS_OK(wbc_status)) {
4279 DEBUG(1, ("winbind could not find %s: %s\n",
4280 full_name, wbcErrorString(wbc_status)));
4281 return false;
4282 }
4283
4284 wbc_status = wbcSidToString(&wsid, &sid_str);
4285 if (!WBC_ERROR_IS_OK(wbc_status)) {
4286 return false;
4287 }
4288
4289 if (type != SID_NAME_USER) {
4290 wbcFreeMemory(sid_str);
4291 DEBUG(1, ("%s is not a user\n", full_name));
4292 return false;
4293 }
4294
4295 string_to_sid(&user_sid, sid_str);
4296 wbcFreeMemory(sid_str);
4297 sid_str = NULL;
4298
4299 init_user_token(token, &user_sid);
4300
4301 /* And now the groups winbind knows about */
4302
4303 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4304 if (!WBC_ERROR_IS_OK(wbc_status)) {
4305 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4306 full_name, wbcErrorString(wbc_status)));
4307 return false;
4308 }
4309
4310 for (i = 0; i < num_groups; i++) {
4311 gid_t gid = groups[i];
4312 DOM_SID sid;
4313
4314 wbc_status = wbcGidToSid(gid, &wsid);
4315 if (!WBC_ERROR_IS_OK(wbc_status)) {
4316 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4317 gid, wbcErrorString(wbc_status)));
4318 wbcFreeMemory(groups);
4319 return false;
4320 }
4321
4322 wbc_status = wbcSidToString(&wsid, &sid_str);
4323 if (!WBC_ERROR_IS_OK(wbc_status)) {
4324 wbcFreeMemory(groups);
4325 return false;
4326 }
4327
4328 DEBUG(3, (" %s\n", sid_str));
4329
4330 string_to_sid(&sid, sid_str);
4331 wbcFreeMemory(sid_str);
4332 sid_str = NULL;
4333
4334 add_sid_to_token(token, &sid);
4335 }
4336 wbcFreeMemory(groups);
4337
4338 return true;
4339}
4340
4341/**
4342 * Get a list of all user tokens we want to look at
4343 **/
4344
4345static bool get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4346{
4347 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4348 uint32_t i, num_users;
4349 const char **users;
4350 struct user_token *result;
4351 TALLOC_CTX *frame = NULL;
4352
4353 if (lp_winbind_use_default_domain() &&
4354 (opt_target_workgroup == NULL)) {
4355 d_fprintf(stderr, "winbind use default domain = yes set, "
4356 "please specify a workgroup\n");
4357 return false;
4358 }
4359
4360 /* Send request to winbind daemon */
4361
4362 wbc_status = wbcListUsers(NULL, &num_users, &users);
4363 if (!WBC_ERROR_IS_OK(wbc_status)) {
4364 DEBUG(1, ("winbind could not list users: %s\n",
4365 wbcErrorString(wbc_status)));
4366 return false;
4367 }
4368
4369 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4370
4371 if (result == NULL) {
4372 DEBUG(1, ("Could not malloc sid array\n"));
4373 wbcFreeMemory(users);
4374 return false;
4375 }
4376
4377 frame = talloc_stackframe();
4378 for (i=0; i < num_users; i++) {
4379 fstring domain, user;
4380 char *p;
4381
4382 fstrcpy(result[i].name, users[i]);
4383
4384 p = strchr(users[i], *lp_winbind_separator());
4385
4386 DEBUG(3, ("%s\n", users[i]));
4387
4388 if (p == NULL) {
4389 fstrcpy(domain, opt_target_workgroup);
4390 fstrcpy(user, users[i]);
4391 } else {
4392 *p++ = '\0';
4393 fstrcpy(domain, users[i]);
4394 strupper_m(domain);
4395 fstrcpy(user, p);
4396 }
4397
4398 get_user_sids(domain, user, &(result[i].token));
4399 i+=1;
4400 }
4401 TALLOC_FREE(frame);
4402 wbcFreeMemory(users);
4403
4404 *num_tokens = num_users;
4405 *user_tokens = result;
4406
4407 return true;
4408}
4409
4410static bool get_user_tokens_from_file(FILE *f,
4411 int *num_tokens,
4412 struct user_token **tokens)
4413{
4414 struct user_token *token = NULL;
4415
4416 while (!feof(f)) {
4417 fstring line;
4418
4419 if (fgets(line, sizeof(line)-1, f) == NULL) {
4420 return True;
4421 }
4422
4423 if (line[strlen(line)-1] == '\n')
4424 line[strlen(line)-1] = '\0';
4425
4426 if (line[0] == ' ') {
4427 /* We have a SID */
4428
4429 DOM_SID sid;
4430 string_to_sid(&sid, &line[1]);
4431
4432 if (token == NULL) {
4433 DEBUG(0, ("File does not begin with username"));
4434 return False;
4435 }
4436
4437 add_sid_to_token(&token->token, &sid);
4438 continue;
4439 }
4440
4441 /* And a new user... */
4442
4443 *num_tokens += 1;
4444 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4445 if (*tokens == NULL) {
4446 DEBUG(0, ("Could not realloc tokens\n"));
4447 return False;
4448 }
4449
4450 token = &((*tokens)[*num_tokens-1]);
4451
4452 fstrcpy(token->name, line);
4453 token->token.num_sids = 0;
4454 token->token.user_sids = NULL;
4455 continue;
4456 }
4457
4458 return False;
4459}
4460
4461
4462/*
4463 * Show the list of all users that have access to a share
4464 */
4465
4466static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4467 TALLOC_CTX *mem_ctx,
4468 const char *netname,
4469 int num_tokens,
4470 struct user_token *tokens)
4471{
4472 int fnum;
4473 SEC_DESC *share_sd = NULL;
4474 SEC_DESC *root_sd = NULL;
4475 struct cli_state *cli = pipe_hnd->cli;
4476 int i;
4477 union srvsvc_NetShareInfo info;
4478 WERROR result;
4479 NTSTATUS status;
4480 uint16 cnum;
4481
4482 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4483 pipe_hnd->cli->desthost,
4484 netname,
4485 502,
4486 &info,
4487 &result);
4488
4489 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4490 DEBUG(1, ("Coult not query secdesc for share %s\n",
4491 netname));
4492 return;
4493 }
4494
4495 share_sd = info.info502->sd_buf.sd;
4496 if (share_sd == NULL) {
4497 DEBUG(1, ("Got no secdesc for share %s\n",
4498 netname));
4499 }
4500
4501 cnum = cli->cnum;
4502
4503 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4504 return;
4505 }
4506
4507 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4508
4509 if (fnum != -1) {
4510 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4511 }
4512
4513 for (i=0; i<num_tokens; i++) {
4514 uint32 acc_granted;
4515
4516 if (share_sd != NULL) {
4517 if (!se_access_check(share_sd, &tokens[i].token,
4518 1, &acc_granted, &status)) {
4519 DEBUG(1, ("Could not check share_sd for "
4520 "user %s\n",
4521 tokens[i].name));
4522 continue;
4523 }
4524
4525 if (!NT_STATUS_IS_OK(status))
4526 continue;
4527 }
4528
4529 if (root_sd == NULL) {
4530 d_printf(" %s\n", tokens[i].name);
4531 continue;
4532 }
4533
4534 if (!se_access_check(root_sd, &tokens[i].token,
4535 1, &acc_granted, &status)) {
4536 DEBUG(1, ("Could not check root_sd for user %s\n",
4537 tokens[i].name));
4538 continue;
4539 }
4540
4541 if (!NT_STATUS_IS_OK(status))
4542 continue;
4543
4544 d_printf(" %s\n", tokens[i].name);
4545 }
4546
4547 if (fnum != -1)
4548 cli_close(cli, fnum);
4549 cli_tdis(cli);
4550 cli->cnum = cnum;
4551
4552 return;
4553}
4554
4555struct share_list {
4556 int num_shares;
4557 char **shares;
4558};
4559
4560static void collect_share(const char *name, uint32 m,
4561 const char *comment, void *state)
4562{
4563 struct share_list *share_list = (struct share_list *)state;
4564
4565 if (m != STYPE_DISKTREE)
4566 return;
4567
4568 share_list->num_shares += 1;
4569 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4570 if (!share_list->shares) {
4571 share_list->num_shares = 0;
4572 return;
4573 }
4574 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4575}
4576
4577static void rpc_share_userlist_usage(void)
4578{
4579 return;
4580}
4581
4582/**
4583 * List shares on a remote RPC server, including the security descriptors.
4584 *
4585 * All parameters are provided by the run_rpc_command function, except for
4586 * argc, argv which are passed through.
4587 *
4588 * @param domain_sid The domain sid acquired from the remote server.
4589 * @param cli A cli_state connected to the server.
4590 * @param mem_ctx Talloc context, destroyed on completion of the function.
4591 * @param argc Standard main() style argc.
4592 * @param argv Standard main() style argv. Initial components are already
4593 * stripped.
4594 *
4595 * @return Normal NTSTATUS return.
4596 **/
4597
4598static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4599 const char *domain_name,
4600 struct cli_state *cli,
4601 struct rpc_pipe_client *pipe_hnd,
4602 TALLOC_CTX *mem_ctx,
4603 int argc,
4604 const char **argv)
4605{
4606 int ret;
4607 bool r;
4608 uint32 i;
4609 FILE *f;
4610
4611 struct user_token *tokens = NULL;
4612 int num_tokens = 0;
4613
4614 struct share_list share_list;
4615
4616 if (argc > 1) {
4617 rpc_share_userlist_usage();
4618 return NT_STATUS_UNSUCCESSFUL;
4619 }
4620
4621 if (argc == 0) {
4622 f = stdin;
4623 } else {
4624 f = fopen(argv[0], "r");
4625 }
4626
4627 if (f == NULL) {
4628 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4629 return NT_STATUS_UNSUCCESSFUL;
4630 }
4631
4632 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4633
4634 if (f != stdin)
4635 fclose(f);
4636
4637 if (!r) {
4638 DEBUG(0, ("Could not read users from file\n"));
4639 return NT_STATUS_UNSUCCESSFUL;
4640 }
4641
4642 for (i=0; i<num_tokens; i++)
4643 collect_alias_memberships(&tokens[i].token);
4644
4645 share_list.num_shares = 0;
4646 share_list.shares = NULL;
4647
4648 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4649
4650 if (ret == -1) {
4651 DEBUG(0, ("Error returning browse list: %s\n",
4652 cli_errstr(cli)));
4653 goto done;
4654 }
4655
4656 for (i = 0; i < share_list.num_shares; i++) {
4657 char *netname = share_list.shares[i];
4658
4659 if (netname[strlen(netname)-1] == '$')
4660 continue;
4661
4662 d_printf("%s\n", netname);
4663
4664 show_userlist(pipe_hnd, mem_ctx, netname,
4665 num_tokens, tokens);
4666 }
4667 done:
4668 for (i=0; i<num_tokens; i++) {
4669 free_user_token(&tokens[i].token);
4670 }
4671 SAFE_FREE(tokens);
4672 SAFE_FREE(share_list.shares);
4673
4674 return NT_STATUS_OK;
4675}
4676
4677static int rpc_share_allowedusers(int argc, const char **argv)
4678{
4679 int result;
4680
4681 result = run_rpc_command(NULL, PI_SAMR, 0,
4682 rpc_aliaslist_internals,
4683 argc, argv);
4684 if (result != 0)
4685 return result;
4686
4687 result = run_rpc_command(NULL, PI_LSARPC, 0,
4688 rpc_aliaslist_dump,
4689 argc, argv);
4690 if (result != 0)
4691 return result;
4692
4693 return run_rpc_command(NULL, PI_SRVSVC, 0,
4694 rpc_share_allowedusers_internals,
4695 argc, argv);
4696}
4697
4698int net_usersidlist(int argc, const char **argv)
4699{
4700 int num_tokens = 0;
4701 struct user_token *tokens = NULL;
4702 int i;
4703
4704 if (argc != 0) {
4705 net_usersidlist_usage(argc, argv);
4706 return 0;
4707 }
4708
4709 if (!get_user_tokens(&num_tokens, &tokens)) {
4710 DEBUG(0, ("Could not get the user/sid list\n"));
4711 return 0;
4712 }
4713
4714 for (i=0; i<num_tokens; i++) {
4715 dump_user_token(&tokens[i]);
4716 free_user_token(&tokens[i].token);
4717 }
4718
4719 SAFE_FREE(tokens);
4720 return 1;
4721}
4722
4723int net_usersidlist_usage(int argc, const char **argv)
4724{
4725 d_printf("net usersidlist\n"
4726 "\tprints out a list of all users the running winbind knows\n"
4727 "\tabout, together with all their SIDs. This is used as\n"
4728 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4729
4730 net_common_flags_usage(argc, argv);
4731 return -1;
4732}
4733
4734/**
4735 * 'net rpc share' entrypoint.
4736 * @param argc Standard main() style argc.
4737 * @param argv Standard main() style argv. Initial components are already
4738 * stripped.
4739 **/
4740
4741int net_rpc_share(int argc, const char **argv)
4742{
4743 struct functable func[] = {
4744 {"add", rpc_share_add},
4745 {"delete", rpc_share_delete},
4746 {"allowedusers", rpc_share_allowedusers},
4747 {"migrate", rpc_share_migrate},
4748 {"list", rpc_share_list},
4749 {NULL, NULL}
4750 };
4751
4752 if (argc == 0)
4753 return run_rpc_command(NULL, PI_SRVSVC, 0,
4754 rpc_share_list_internals,
4755 argc, argv);
4756
4757 return net_run_function(argc, argv, func, rpc_share_usage);
4758}
4759
4760static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4761 struct rpc_sh_ctx *ctx,
4762 struct rpc_pipe_client *pipe_hnd,
4763 int argc, const char **argv)
4764{
4765 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4766 ctx->cli, pipe_hnd, mem_ctx,
4767 argc, argv);
4768}
4769
4770static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4771 struct rpc_sh_ctx *ctx,
4772 struct rpc_pipe_client *pipe_hnd,
4773 int argc, const char **argv)
4774{
4775 WERROR result;
4776 NTSTATUS status;
4777 uint32_t parm_err = 0;
4778 union srvsvc_NetShareInfo info;
4779 struct srvsvc_NetShareInfo2 info2;
4780
4781 if ((argc < 2) || (argc > 3)) {
4782 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4783 ctx->whoami);
4784 return NT_STATUS_INVALID_PARAMETER;
4785 }
4786
4787 info2.name = argv[0];
4788 info2.type = STYPE_DISKTREE;
4789 info2.comment = (argc == 3) ? argv[2] : "";
4790 info2.permissions = 0;
4791 info2.max_users = 0;
4792 info2.current_users = 0;
4793 info2.path = argv[1];
4794 info2.password = NULL;
4795
4796 info.info2 = &info2;
4797
4798 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
4799 pipe_hnd->cli->desthost,
4800 2,
4801 &info,
4802 &parm_err,
4803 &result);
4804
4805 return status;
4806}
4807
4808static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4809 struct rpc_sh_ctx *ctx,
4810 struct rpc_pipe_client *pipe_hnd,
4811 int argc, const char **argv)
4812{
4813 WERROR result;
4814 NTSTATUS status;
4815
4816 if (argc != 1) {
4817 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4818 return NT_STATUS_INVALID_PARAMETER;
4819 }
4820
4821 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
4822 pipe_hnd->cli->desthost,
4823 argv[0],
4824 0,
4825 &result);
4826
4827 return status;
4828}
4829
4830static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4831 struct rpc_sh_ctx *ctx,
4832 struct rpc_pipe_client *pipe_hnd,
4833 int argc, const char **argv)
4834{
4835 union srvsvc_NetShareInfo info;
4836 WERROR result;
4837 NTSTATUS status;
4838
4839 if (argc != 1) {
4840 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4841 return NT_STATUS_INVALID_PARAMETER;
4842 }
4843
4844 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4845 pipe_hnd->cli->desthost,
4846 argv[0],
4847 2,
4848 &info,
4849 &result);
4850 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4851 goto done;
4852 }
4853
4854 d_printf("Name: %s\n", info.info2->name);
4855 d_printf("Comment: %s\n", info.info2->comment);
4856 d_printf("Path: %s\n", info.info2->path);
4857 d_printf("Password: %s\n", info.info2->password);
4858
4859 done:
4860 return werror_to_ntstatus(result);
4861}
4862
4863struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4864 struct rpc_sh_ctx *ctx)
4865{
4866 static struct rpc_sh_cmd cmds[] = {
4867
4868 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4869 "List available shares" },
4870
4871 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4872 "Add a share" },
4873
4874 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4875 "Delete a share" },
4876
4877 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4878 "Get information about a share" },
4879
4880 { NULL, NULL, 0, NULL, NULL }
4881 };
4882
4883 return cmds;
4884}
4885
4886/****************************************************************************/
4887
4888static int rpc_file_usage(int argc, const char **argv)
4889{
4890 return net_help_file(argc, argv);
4891}
4892
4893/**
4894 * Close a file on a remote RPC server.
4895 *
4896 * All parameters are provided by the run_rpc_command function, except for
4897 * argc, argv which are passed through.
4898 *
4899 * @param domain_sid The domain sid acquired from the remote server.
4900 * @param cli A cli_state connected to the server.
4901 * @param mem_ctx Talloc context, destroyed on completion of the function.
4902 * @param argc Standard main() style argc.
4903 * @param argv Standard main() style argv. Initial components are already
4904 * stripped.
4905 *
4906 * @return Normal NTSTATUS return.
4907 **/
4908static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4909 const char *domain_name,
4910 struct cli_state *cli,
4911 struct rpc_pipe_client *pipe_hnd,
4912 TALLOC_CTX *mem_ctx,
4913 int argc,
4914 const char **argv)
4915{
4916 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
4917 pipe_hnd->cli->desthost,
4918 atoi(argv[0]), NULL);
4919}
4920
4921/**
4922 * Close a file on a remote RPC server.
4923 *
4924 * @param argc Standard main() style argc.
4925 * @param argv Standard main() style argv. Initial components are already
4926 * stripped.
4927 *
4928 * @return A shell status integer (0 for success).
4929 **/
4930static int rpc_file_close(int argc, const char **argv)
4931{
4932 if (argc < 1) {
4933 DEBUG(1, ("No fileid given on close\n"));
4934 return(rpc_file_usage(argc, argv));
4935 }
4936
4937 return run_rpc_command(NULL, PI_SRVSVC, 0,
4938 rpc_file_close_internals,
4939 argc, argv);
4940}
4941
4942/**
4943 * Formatted print of open file info
4944 *
4945 * @param r struct srvsvc_NetFileInfo3 contents
4946 **/
4947
4948static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
4949{
4950 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4951 r->fid, r->user, r->permissions, r->num_locks, r->path);
4952}
4953
4954/**
4955 * List open files on a remote RPC server.
4956 *
4957 * All parameters are provided by the run_rpc_command function, except for
4958 * argc, argv which are passed through.
4959 *
4960 * @param domain_sid The domain sid acquired from the remote server.
4961 * @param cli A cli_state connected to the server.
4962 * @param mem_ctx Talloc context, destroyed on completion of the function.
4963 * @param argc Standard main() style argc.
4964 * @param argv Standard main() style argv. Initial components are already
4965 * stripped.
4966 *
4967 * @return Normal NTSTATUS return.
4968 **/
4969
4970static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4971 const char *domain_name,
4972 struct cli_state *cli,
4973 struct rpc_pipe_client *pipe_hnd,
4974 TALLOC_CTX *mem_ctx,
4975 int argc,
4976 const char **argv)
4977{
4978 struct srvsvc_NetFileInfoCtr info_ctr;
4979 struct srvsvc_NetFileCtr3 ctr3;
4980 WERROR result;
4981 NTSTATUS status;
4982 uint32 preferred_len = 0xffffffff, i;
4983 const char *username=NULL;
4984 uint32_t total_entries = 0;
4985 uint32_t resume_handle = 0;
4986
4987 /* if argc > 0, must be user command */
4988 if (argc > 0)
4989 username = smb_xstrdup(argv[0]);
4990
4991 ZERO_STRUCT(info_ctr);
4992 ZERO_STRUCT(ctr3);
4993
4994 info_ctr.level = 3;
4995 info_ctr.ctr.ctr3 = &ctr3;
4996
4997 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
4998 pipe_hnd->cli->desthost,
4999 NULL,
5000 username,
5001 &info_ctr,
5002 preferred_len,
5003 &total_entries,
5004 &resume_handle,
5005 &result);
5006
5007 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5008 goto done;
5009
5010 /* Display results */
5011
5012 d_printf(
5013 "\nEnumerating open files on remote server:\n\n"\
5014 "\nFileId Opened by Perms Locks Path"\
5015 "\n------ --------- ----- ----- ---- \n");
5016 for (i = 0; i < total_entries; i++)
5017 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5018 done:
5019 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5020}
5021
5022/**
5023 * List files for a user on a remote RPC server.
5024 *
5025 * @param argc Standard main() style argc.
5026 * @param argv Standard main() style argv. Initial components are already
5027 * stripped.
5028 *
5029 * @return A shell status integer (0 for success).
5030 **/
5031
5032static int rpc_file_user(int argc, const char **argv)
5033{
5034 if (argc < 1) {
5035 DEBUG(1, ("No username given\n"));
5036 return(rpc_file_usage(argc, argv));
5037 }
5038
5039 return run_rpc_command(NULL, PI_SRVSVC, 0,
5040 rpc_file_list_internals,
5041 argc, argv);
5042}
5043
5044/**
5045 * 'net rpc file' entrypoint.
5046 * @param argc Standard main() style argc.
5047 * @param argv Standard main() style argv. Initial components are already
5048 * stripped.
5049 **/
5050
5051int net_rpc_file(int argc, const char **argv)
5052{
5053 struct functable func[] = {
5054 {"close", rpc_file_close},
5055 {"user", rpc_file_user},
5056#if 0
5057 {"info", rpc_file_info},
5058#endif
5059 {NULL, NULL}
5060 };
5061
5062 if (argc == 0)
5063 return run_rpc_command(NULL, PI_SRVSVC, 0,
5064 rpc_file_list_internals,
5065 argc, argv);
5066
5067 return net_run_function(argc, argv, func, rpc_file_usage);
5068}
5069
5070/**
5071 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5072 *
5073 * All parameters are provided by the run_rpc_command function, except for
5074 * argc, argv which are passed through.
5075 *
5076 * @param domain_sid The domain sid acquired from the remote server.
5077 * @param cli A cli_state connected to the server.
5078 * @param mem_ctx Talloc context, destroyed on completion of the function.
5079 * @param argc Standard main() style argc.
5080 * @param argv Standard main() style argv. Initial components are already
5081 * stripped.
5082 *
5083 * @return Normal NTSTATUS return.
5084 **/
5085
5086static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
5087 const char *domain_name,
5088 struct cli_state *cli,
5089 struct rpc_pipe_client *pipe_hnd,
5090 TALLOC_CTX *mem_ctx,
5091 int argc,
5092 const char **argv)
5093{
5094 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5095
5096 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5097
5098 if (NT_STATUS_IS_OK(result)) {
5099 d_printf("\nShutdown successfully aborted\n");
5100 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5101 } else
5102 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5103
5104 return result;
5105}
5106
5107/**
5108 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5109 *
5110 * All parameters are provided by the run_rpc_command function, except for
5111 * argc, argv which are passed through.
5112 *
5113 * @param domain_sid The domain sid acquired from the remote server.
5114 * @param cli A cli_state connected to the server.
5115 * @param mem_ctx Talloc context, destroyed on completion of the function.
5116 * @param argc Standard main() style argc.
5117 * @param argv Standard main() style argv. Initial components are already
5118 * stripped.
5119 *
5120 * @return Normal NTSTATUS return.
5121 **/
5122
5123static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5124 const char *domain_name,
5125 struct cli_state *cli,
5126 struct rpc_pipe_client *pipe_hnd,
5127 TALLOC_CTX *mem_ctx,
5128 int argc,
5129 const char **argv)
5130{
5131 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5132
5133 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5134
5135 if (NT_STATUS_IS_OK(result)) {
5136 d_printf("\nShutdown successfully aborted\n");
5137 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5138 } else
5139 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5140
5141 return result;
5142}
5143
5144/**
5145 * ABORT the Shut down of a remote RPC server.
5146 *
5147 * @param argc Standard main() style argc.
5148 * @param argv Standard main() style argv. Initial components are already
5149 * stripped.
5150 *
5151 * @return A shell status integer (0 for success).
5152 **/
5153
5154static int rpc_shutdown_abort(int argc, const char **argv)
5155{
5156 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5157 rpc_shutdown_abort_internals,
5158 argc, argv);
5159
5160 if (rc == 0)
5161 return rc;
5162
5163 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5164
5165 return run_rpc_command(NULL, PI_WINREG, 0,
5166 rpc_reg_shutdown_abort_internals,
5167 argc, argv);
5168}
5169
5170/**
5171 * Shut down a remote RPC Server via initshutdown pipe.
5172 *
5173 * All parameters are provided by the run_rpc_command function, except for
5174 * argc, argv which are passed through.
5175 *
5176 * @param domain_sid The domain sid acquired from the remote server.
5177 * @param cli A cli_state connected to the server.
5178 * @param mem_ctx Talloc context, destroyed on completion of the function.
5179 * @param argc Standard main() style argc.
5180 * @param argv Standard main() style argv. Initial components are already
5181 * stripped.
5182 *
5183 * @return Normal NTSTATUS return.
5184 **/
5185
5186NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5187 const char *domain_name,
5188 struct cli_state *cli,
5189 struct rpc_pipe_client *pipe_hnd,
5190 TALLOC_CTX *mem_ctx,
5191 int argc,
5192 const char **argv)
5193{
5194 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5195 const char *msg = "This machine will be shutdown shortly";
5196 uint32 timeout = 20;
5197 struct initshutdown_String msg_string;
5198 struct initshutdown_String_sub s;
5199
5200 if (opt_comment && strlen(opt_comment)) {
5201 msg = opt_comment;
5202 }
5203 if (opt_timeout) {
5204 timeout = opt_timeout;
5205 }
5206
5207 s.name = msg;
5208 msg_string.name = &s;
5209
5210 /* create an entry */
5211 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5212 &msg_string, timeout, opt_force, opt_reboot, NULL);
5213
5214 if (NT_STATUS_IS_OK(result)) {
5215 d_printf("\nShutdown of remote machine succeeded\n");
5216 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5217 } else {
5218 DEBUG(1,("Shutdown of remote machine failed!\n"));
5219 }
5220 return result;
5221}
5222
5223/**
5224 * Shut down a remote RPC Server via winreg pipe.
5225 *
5226 * All parameters are provided by the run_rpc_command function, except for
5227 * argc, argv which are passed through.
5228 *
5229 * @param domain_sid The domain sid acquired from the remote server.
5230 * @param cli A cli_state connected to the server.
5231 * @param mem_ctx Talloc context, destroyed on completion of the function.
5232 * @param argc Standard main() style argc.
5233 * @param argv Standard main() style argv. Initial components are already
5234 * stripped.
5235 *
5236 * @return Normal NTSTATUS return.
5237 **/
5238
5239NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5240 const char *domain_name,
5241 struct cli_state *cli,
5242 struct rpc_pipe_client *pipe_hnd,
5243 TALLOC_CTX *mem_ctx,
5244 int argc,
5245 const char **argv)
5246{
5247 const char *msg = "This machine will be shutdown shortly";
5248 uint32 timeout = 20;
5249 struct initshutdown_String msg_string;
5250 struct initshutdown_String_sub s;
5251 NTSTATUS result;
5252 WERROR werr;
5253
5254 if (opt_comment && strlen(opt_comment)) {
5255 msg = opt_comment;
5256 }
5257 s.name = msg;
5258 msg_string.name = &s;
5259
5260 if (opt_timeout) {
5261 timeout = opt_timeout;
5262 }
5263
5264 /* create an entry */
5265 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5266 &msg_string, timeout, opt_force, opt_reboot, &werr);
5267
5268 if (NT_STATUS_IS_OK(result)) {
5269 d_printf("\nShutdown of remote machine succeeded\n");
5270 } else {
5271 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5272 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5273 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5274 else
5275 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5276 }
5277
5278 return result;
5279}
5280
5281/**
5282 * Shut down a remote RPC server.
5283 *
5284 * @param argc Standard main() style argc.
5285 * @param argv Standard main() style argv. Initial components are already
5286 * stripped.
5287 *
5288 * @return A shell status integer (0 for success).
5289 **/
5290
5291static int rpc_shutdown(int argc, const char **argv)
5292{
5293 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5294 rpc_init_shutdown_internals,
5295 argc, argv);
5296
5297 if (rc) {
5298 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5299 rc = run_rpc_command(NULL, PI_WINREG, 0,
5300 rpc_reg_shutdown_internals, argc, argv);
5301 }
5302
5303 return rc;
5304}
5305
5306/***************************************************************************
5307 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5308
5309 ***************************************************************************/
5310
5311/**
5312 * Add interdomain trust account to the RPC server.
5313 * All parameters (except for argc and argv) are passed by run_rpc_command
5314 * function.
5315 *
5316 * @param domain_sid The domain sid acquired from the server.
5317 * @param cli A cli_state connected to the server.
5318 * @param mem_ctx Talloc context, destroyed on completion of the function.
5319 * @param argc Standard main() style argc.
5320 * @param argv Standard main() style argv. Initial components are already
5321 * stripped.
5322 *
5323 * @return normal NTSTATUS return code.
5324 */
5325
5326static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5327 const char *domain_name,
5328 struct cli_state *cli,
5329 struct rpc_pipe_client *pipe_hnd,
5330 TALLOC_CTX *mem_ctx,
5331 int argc,
5332 const char **argv)
5333{
5334 POLICY_HND connect_pol, domain_pol, user_pol;
5335 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5336 char *acct_name;
5337 struct lsa_String lsa_acct_name;
5338 uint32 acb_info;
5339 uint32 acct_flags=0;
5340 uint32 user_rid;
5341 uint32_t access_granted = 0;
5342 union samr_UserInfo info;
5343 unsigned int orig_timeout;
5344
5345 if (argc != 2) {
5346 d_printf("Usage: net rpc trustdom add <domain_name> <trust password>\n");
5347 return NT_STATUS_INVALID_PARAMETER;
5348 }
5349
5350 /*
5351 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5352 */
5353
5354 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5355 return NT_STATUS_NO_MEMORY;
5356 }
5357
5358 strupper_m(acct_name);
5359
5360 init_lsa_String(&lsa_acct_name, acct_name);
5361
5362 /* Get samr policy handle */
5363 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5364 pipe_hnd->cli->desthost,
5365 MAXIMUM_ALLOWED_ACCESS,
5366 &connect_pol);
5367 if (!NT_STATUS_IS_OK(result)) {
5368 goto done;
5369 }
5370
5371 /* Get domain policy handle */
5372 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5373 &connect_pol,
5374 MAXIMUM_ALLOWED_ACCESS,
5375 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5376 &domain_pol);
5377 if (!NT_STATUS_IS_OK(result)) {
5378 goto done;
5379 }
5380
5381 /* This call can take a long time - allow the server to time out.
5382 * 35 seconds should do it. */
5383
5384 orig_timeout = cli_set_timeout(pipe_hnd->cli, 35000);
5385
5386 /* Create trusting domain's account */
5387 acb_info = ACB_NORMAL;
5388 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5389 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5390 SAMR_USER_ACCESS_SET_PASSWORD |
5391 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5392 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5393
5394 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5395 &domain_pol,
5396 &lsa_acct_name,
5397 acb_info,
5398 acct_flags,
5399 &user_pol,
5400 &access_granted,
5401 &user_rid);
5402
5403 /* And restore our original timeout. */
5404 cli_set_timeout(pipe_hnd->cli, orig_timeout);
5405
5406 if (!NT_STATUS_IS_OK(result)) {
5407 d_printf("net rpc trustdom add: create user %s failed %s\n",
5408 acct_name, nt_errstr(result));
5409 goto done;
5410 }
5411
5412 {
5413 NTTIME notime;
5414 struct samr_LogonHours hours;
5415 struct lsa_BinaryString parameters;
5416 const int units_per_week = 168;
5417 struct samr_CryptPassword crypt_pwd;
5418
5419 ZERO_STRUCT(notime);
5420 ZERO_STRUCT(hours);
5421 ZERO_STRUCT(parameters);
5422
5423 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5424 if (!hours.bits) {
5425 result = NT_STATUS_NO_MEMORY;
5426 goto done;
5427 }
5428 hours.units_per_week = units_per_week;
5429 memset(hours.bits, 0xFF, units_per_week);
5430
5431 init_samr_CryptPassword(argv[1],
5432 &cli->user_session_key,
5433 &crypt_pwd);
5434
5435 init_samr_user_info23(&info.info23,
5436 notime, notime, notime,
5437 notime, notime, notime,
5438 NULL, NULL, NULL, NULL, NULL,
5439 NULL, NULL, NULL, NULL, &parameters,
5440 0, 0, ACB_DOMTRUST,
5441 SAMR_FIELD_ACCT_FLAGS | SAMR_FIELD_PASSWORD,
5442 hours,
5443 0, 0, 0, 0, 0, 0, 0,
5444 &crypt_pwd);
5445
5446 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5447 &user_pol,
5448 23,
5449 &info);
5450
5451 if (!NT_STATUS_IS_OK(result)) {
5452 DEBUG(0,("Could not set trust account password: %s\n",
5453 nt_errstr(result)));
5454 goto done;
5455 }
5456 }
5457
5458 done:
5459 SAFE_FREE(acct_name);
5460 return result;
5461}
5462
5463/**
5464 * Create interdomain trust account for a remote domain.
5465 *
5466 * @param argc Standard argc.
5467 * @param argv Standard argv without initial components.
5468 *
5469 * @return Integer status (0 means success)
5470 **/
5471
5472static int rpc_trustdom_add(int argc, const char **argv)
5473{
5474 if (argc > 0) {
5475 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5476 argc, argv);
5477 } else {
5478 d_printf("Usage: net rpc trustdom add <domain_name> <trust password>\n");
5479 return -1;
5480 }
5481}
5482
5483
5484/**
5485 * Remove interdomain trust account from the RPC server.
5486 * All parameters (except for argc and argv) are passed by run_rpc_command
5487 * function.
5488 *
5489 * @param domain_sid The domain sid acquired from the server.
5490 * @param cli A cli_state connected to the server.
5491 * @param mem_ctx Talloc context, destroyed on completion of the function.
5492 * @param argc Standard main() style argc.
5493 * @param argv Standard main() style argv. Initial components are already
5494 * stripped.
5495 *
5496 * @return normal NTSTATUS return code.
5497 */
5498
5499static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5500 const char *domain_name,
5501 struct cli_state *cli,
5502 struct rpc_pipe_client *pipe_hnd,
5503 TALLOC_CTX *mem_ctx,
5504 int argc,
5505 const char **argv)
5506{
5507 POLICY_HND connect_pol, domain_pol, user_pol;
5508 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5509 char *acct_name;
5510 DOM_SID trust_acct_sid;
5511 struct samr_Ids user_rids, name_types;
5512 struct lsa_String lsa_acct_name;
5513
5514 if (argc != 1) {
5515 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5516 return NT_STATUS_INVALID_PARAMETER;
5517 }
5518
5519 /*
5520 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5521 */
5522 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5523
5524 if (acct_name == NULL)
5525 return NT_STATUS_NO_MEMORY;
5526
5527 strupper_m(acct_name);
5528
5529 /* Get samr policy handle */
5530 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5531 pipe_hnd->cli->desthost,
5532 MAXIMUM_ALLOWED_ACCESS,
5533 &connect_pol);
5534 if (!NT_STATUS_IS_OK(result)) {
5535 goto done;
5536 }
5537
5538 /* Get domain policy handle */
5539 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5540 &connect_pol,
5541 MAXIMUM_ALLOWED_ACCESS,
5542 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5543 &domain_pol);
5544 if (!NT_STATUS_IS_OK(result)) {
5545 goto done;
5546 }
5547
5548 init_lsa_String(&lsa_acct_name, acct_name);
5549
5550 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5551 &domain_pol,
5552 1,
5553 &lsa_acct_name,
5554 &user_rids,
5555 &name_types);
5556
5557 if (!NT_STATUS_IS_OK(result)) {
5558 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5559 acct_name, nt_errstr(result) );
5560 goto done;
5561 }
5562
5563 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5564 &domain_pol,
5565 MAXIMUM_ALLOWED_ACCESS,
5566 user_rids.ids[0],
5567 &user_pol);
5568
5569 if (!NT_STATUS_IS_OK(result)) {
5570 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5571 acct_name, nt_errstr(result) );
5572 goto done;
5573 }
5574
5575 /* append the rid to the domain sid */
5576 sid_copy(&trust_acct_sid, domain_sid);
5577 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5578 goto done;
5579 }
5580
5581 /* remove the sid */
5582
5583 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5584 &user_pol,
5585 &trust_acct_sid);
5586 if (!NT_STATUS_IS_OK(result)) {
5587 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5588 acct_name, nt_errstr(result) );
5589 goto done;
5590 }
5591
5592 /* Delete user */
5593
5594 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5595 &user_pol);
5596
5597 if (!NT_STATUS_IS_OK(result)) {
5598 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5599 acct_name, nt_errstr(result) );
5600 goto done;
5601 }
5602
5603 if (!NT_STATUS_IS_OK(result)) {
5604 d_printf("Could not set trust account password: %s\n",
5605 nt_errstr(result));
5606 goto done;
5607 }
5608
5609 done:
5610 return result;
5611}
5612
5613/**
5614 * Delete interdomain trust account for a remote domain.
5615 *
5616 * @param argc Standard argc.
5617 * @param argv Standard argv without initial components.
5618 *
5619 * @return Integer status (0 means success).
5620 **/
5621
5622static int rpc_trustdom_del(int argc, const char **argv)
5623{
5624 if (argc > 0) {
5625 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5626 argc, argv);
5627 } else {
5628 d_printf("Usage: net rpc trustdom del <domain>\n");
5629 return -1;
5630 }
5631}
5632
5633static NTSTATUS rpc_trustdom_get_pdc(struct cli_state *cli,
5634 TALLOC_CTX *mem_ctx,
5635 const char *domain_name)
5636{
5637 char *dc_name = NULL;
5638 const char *buffer = NULL;
5639 struct rpc_pipe_client *netr;
5640 NTSTATUS status;
5641
5642 /* Use NetServerEnum2 */
5643
5644 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5645 SAFE_FREE(dc_name);
5646 return NT_STATUS_OK;
5647 }
5648
5649 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5650 for domain %s\n", domain_name));
5651
5652 /* Try netr_GetDcName */
5653
5654 netr = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &status);
5655 if (!netr) {
5656 return status;
5657 }
5658
5659 status = rpccli_netr_GetDcName(netr, mem_ctx,
5660 cli->desthost,
5661 domain_name,
5662 &buffer,
5663 NULL);
5664 cli_rpc_pipe_close(netr);
5665
5666 if (NT_STATUS_IS_OK(status)) {
5667 return status;
5668 }
5669
5670 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5671 for domain %s\n", domain_name));
5672
5673 return status;
5674}
5675
5676/**
5677 * Establish trust relationship to a trusting domain.
5678 * Interdomain account must already be created on remote PDC.
5679 *
5680 * @param argc Standard argc.
5681 * @param argv Standard argv without initial components.
5682 *
5683 * @return Integer status (0 means success).
5684 **/
5685
5686static int rpc_trustdom_establish(int argc, const char **argv)
5687{
5688 struct cli_state *cli = NULL;
5689 struct sockaddr_storage server_ss;
5690 struct rpc_pipe_client *pipe_hnd = NULL;
5691 POLICY_HND connect_hnd;
5692 TALLOC_CTX *mem_ctx;
5693 NTSTATUS nt_status;
5694 DOM_SID *domain_sid;
5695
5696 char* domain_name;
5697 char* acct_name;
5698 fstring pdc_name;
5699 union lsa_PolicyInformation *info = NULL;
5700
5701 /*
5702 * Connect to \\server\ipc$ as 'our domain' account with password
5703 */
5704
5705 if (argc != 1) {
5706 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5707 return -1;
5708 }
5709
5710 domain_name = smb_xstrdup(argv[0]);
5711 strupper_m(domain_name);
5712
5713 /* account name used at first is our domain's name with '$' */
5714 asprintf(&acct_name, "%s$", lp_workgroup());
5715 strupper_m(acct_name);
5716
5717 /*
5718 * opt_workgroup will be used by connection functions further,
5719 * hence it should be set to remote domain name instead of ours
5720 */
5721 if (opt_workgroup) {
5722 opt_workgroup = smb_xstrdup(domain_name);
5723 };
5724
5725 opt_user_name = acct_name;
5726
5727 /* find the domain controller */
5728 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5729 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5730 return -1;
5731 }
5732
5733 /* connect to ipc$ as username/password */
5734 nt_status = connect_to_ipc(&cli, &server_ss, pdc_name);
5735 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5736
5737 /* Is it trusting domain account for sure ? */
5738 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5739 nt_errstr(nt_status)));
5740 return -1;
5741 }
5742
5743 /* store who we connected to */
5744
5745 saf_store( domain_name, pdc_name );
5746
5747 /*
5748 * Connect to \\server\ipc$ again (this time anonymously)
5749 */
5750
5751 nt_status = connect_to_ipc_anonymous(&cli, &server_ss, (char*)pdc_name);
5752
5753 if (NT_STATUS_IS_ERR(nt_status)) {
5754 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5755 domain_name, nt_errstr(nt_status)));
5756 return -1;
5757 }
5758
5759 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5760 "domain %s", domain_name))) {
5761 DEBUG(0, ("talloc_init() failed\n"));
5762 cli_shutdown(cli);
5763 return -1;
5764 }
5765
5766 /* Make sure we're talking to a proper server */
5767
5768 nt_status = rpc_trustdom_get_pdc(cli, mem_ctx, domain_name);
5769 if (!NT_STATUS_IS_OK(nt_status)) {
5770 cli_shutdown(cli);
5771 talloc_destroy(mem_ctx);
5772 return -1;
5773 }
5774
5775 /*
5776 * Call LsaOpenPolicy and LsaQueryInfo
5777 */
5778
5779 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5780 if (!pipe_hnd) {
5781 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5782 cli_shutdown(cli);
5783 talloc_destroy(mem_ctx);
5784 return -1;
5785 }
5786
5787 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5788 &connect_hnd);
5789 if (NT_STATUS_IS_ERR(nt_status)) {
5790 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5791 nt_errstr(nt_status)));
5792 cli_shutdown(cli);
5793 talloc_destroy(mem_ctx);
5794 return -1;
5795 }
5796
5797 /* Querying info level 5 */
5798
5799 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5800 &connect_hnd,
5801 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5802 &info);
5803 if (NT_STATUS_IS_ERR(nt_status)) {
5804 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5805 nt_errstr(nt_status)));
5806 cli_shutdown(cli);
5807 talloc_destroy(mem_ctx);
5808 return -1;
5809 }
5810
5811 domain_sid = info->account_domain.sid;
5812
5813 /* There should be actually query info level 3 (following nt serv behaviour),
5814 but I still don't know if it's _really_ necessary */
5815
5816 /*
5817 * Store the password in secrets db
5818 */
5819
5820 if (!pdb_set_trusteddom_pw(domain_name, opt_password, domain_sid)) {
5821 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5822 cli_shutdown(cli);
5823 talloc_destroy(mem_ctx);
5824 return -1;
5825 }
5826
5827 /*
5828 * Close the pipes and clean up
5829 */
5830
5831 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5832 if (NT_STATUS_IS_ERR(nt_status)) {
5833 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5834 nt_errstr(nt_status)));
5835 cli_shutdown(cli);
5836 talloc_destroy(mem_ctx);
5837 return -1;
5838 }
5839
5840 cli_shutdown(cli);
5841
5842 talloc_destroy(mem_ctx);
5843
5844 d_printf("Trust to domain %s established\n", domain_name);
5845 return 0;
5846}
5847
5848/**
5849 * Revoke trust relationship to the remote domain.
5850 *
5851 * @param argc Standard argc.
5852 * @param argv Standard argv without initial components.
5853 *
5854 * @return Integer status (0 means success).
5855 **/
5856
5857static int rpc_trustdom_revoke(int argc, const char **argv)
5858{
5859 char* domain_name;
5860 int rc = -1;
5861
5862 if (argc < 1) return -1;
5863
5864 /* generate upper cased domain name */
5865 domain_name = smb_xstrdup(argv[0]);
5866 strupper_m(domain_name);
5867
5868 /* delete password of the trust */
5869 if (!pdb_del_trusteddom_pw(domain_name)) {
5870 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5871 domain_name));
5872 goto done;
5873 };
5874
5875 rc = 0;
5876done:
5877 SAFE_FREE(domain_name);
5878 return rc;
5879}
5880
5881/**
5882 * Usage for 'net rpc trustdom' command
5883 *
5884 * @param argc standard argc
5885 * @param argv standard argv without inital components
5886 *
5887 * @return Integer status returned to shell
5888 **/
5889
5890static int rpc_trustdom_usage(int argc, const char **argv)
5891{
5892 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5893 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5894 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5895 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5896 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5897 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5898 return -1;
5899}
5900
5901
5902static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5903 const char *domain_name,
5904 struct cli_state *cli,
5905 struct rpc_pipe_client *pipe_hnd,
5906 TALLOC_CTX *mem_ctx,
5907 int argc,
5908 const char **argv)
5909{
5910 fstring str_sid;
5911 sid_to_fstring(str_sid, domain_sid);
5912 d_printf("%s\n", str_sid);
5913 return NT_STATUS_OK;
5914}
5915
5916static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5917{
5918 fstring ascii_sid, padding;
5919 int pad_len, col_len = 20;
5920
5921 /* convert sid into ascii string */
5922 sid_to_fstring(ascii_sid, dom_sid);
5923
5924 /* calculate padding space for d_printf to look nicer */
5925 pad_len = col_len - strlen(trusted_dom_name);
5926 padding[pad_len] = 0;
5927 do padding[--pad_len] = ' '; while (pad_len);
5928
5929 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5930}
5931
5932static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5933 TALLOC_CTX *mem_ctx,
5934 POLICY_HND *pol,
5935 DOM_SID dom_sid,
5936 const char *trusted_dom_name)
5937{
5938 NTSTATUS nt_status;
5939 union lsa_TrustedDomainInfo *info = NULL;
5940 char *cleartextpwd = NULL;
5941 DATA_BLOB data = data_blob_null;
5942
5943 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5944 pol,
5945 &dom_sid,
5946 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5947 &info);
5948 if (NT_STATUS_IS_ERR(nt_status)) {
5949 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5950 nt_errstr(nt_status)));
5951 goto done;
5952 }
5953
5954 data = data_blob(info->password.password->data,
5955 info->password.password->length);
5956
5957 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password,
5958 &data);
5959
5960 if (cleartextpwd == NULL) {
5961 DEBUG(0,("retrieved NULL password\n"));
5962 nt_status = NT_STATUS_UNSUCCESSFUL;
5963 goto done;
5964 }
5965
5966 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5967 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5968 nt_status = NT_STATUS_UNSUCCESSFUL;
5969 goto done;
5970 }
5971
5972#ifdef DEBUG_PASSWORD
5973 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5974 "password: [%s]\n", trusted_dom_name,
5975 sid_string_dbg(&dom_sid), cleartextpwd));
5976#endif
5977
5978done:
5979 SAFE_FREE(cleartextpwd);
5980 data_blob_free(&data);
5981
5982 return nt_status;
5983}
5984
5985static int rpc_trustdom_vampire(int argc, const char **argv)
5986{
5987 /* common variables */
5988 TALLOC_CTX* mem_ctx;
5989 struct cli_state *cli = NULL;
5990 struct rpc_pipe_client *pipe_hnd = NULL;
5991 NTSTATUS nt_status;
5992 const char *domain_name = NULL;
5993 DOM_SID *queried_dom_sid;
5994 POLICY_HND connect_hnd;
5995 union lsa_PolicyInformation *info = NULL;
5996
5997 /* trusted domains listing variables */
5998 unsigned int enum_ctx = 0;
5999 int i;
6000 struct lsa_DomainList dom_list;
6001 fstring pdc_name;
6002
6003 /*
6004 * Listing trusted domains (stored in secrets.tdb, if local)
6005 */
6006
6007 mem_ctx = talloc_init("trust relationships vampire");
6008
6009 /*
6010 * set domain and pdc name to local samba server (default)
6011 * or to remote one given in command line
6012 */
6013
6014 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6015 domain_name = opt_workgroup;
6016 opt_target_workgroup = opt_workgroup;
6017 } else {
6018 fstrcpy(pdc_name, global_myname());
6019 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6020 opt_target_workgroup = domain_name;
6021 };
6022
6023 /* open \PIPE\lsarpc and open policy handle */
6024 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6025 if (!NT_STATUS_IS_OK(nt_status)) {
6026 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6027 nt_errstr(nt_status)));
6028 talloc_destroy(mem_ctx);
6029 return -1;
6030 };
6031
6032 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6033 if (!pipe_hnd) {
6034 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6035 nt_errstr(nt_status) ));
6036 cli_shutdown(cli);
6037 talloc_destroy(mem_ctx);
6038 return -1;
6039 };
6040
6041 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6042 &connect_hnd);
6043 if (NT_STATUS_IS_ERR(nt_status)) {
6044 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6045 nt_errstr(nt_status)));
6046 cli_shutdown(cli);
6047 talloc_destroy(mem_ctx);
6048 return -1;
6049 };
6050
6051 /* query info level 5 to obtain sid of a domain being queried */
6052 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6053 &connect_hnd,
6054 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6055 &info);
6056
6057 if (NT_STATUS_IS_ERR(nt_status)) {
6058 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6059 nt_errstr(nt_status)));
6060 cli_shutdown(cli);
6061 talloc_destroy(mem_ctx);
6062 return -1;
6063 }
6064
6065 queried_dom_sid = info->account_domain.sid;
6066
6067 /*
6068 * Keep calling LsaEnumTrustdom over opened pipe until
6069 * the end of enumeration is reached
6070 */
6071
6072 d_printf("Vampire trusted domains:\n\n");
6073
6074 do {
6075 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6076 &connect_hnd,
6077 &enum_ctx,
6078 &dom_list,
6079 (uint32_t)-1);
6080 if (NT_STATUS_IS_ERR(nt_status)) {
6081 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6082 nt_errstr(nt_status)));
6083 cli_shutdown(cli);
6084 talloc_destroy(mem_ctx);
6085 return -1;
6086 };
6087
6088 for (i = 0; i < dom_list.count; i++) {
6089
6090 print_trusted_domain(dom_list.domains[i].sid,
6091 dom_list.domains[i].name.string);
6092
6093 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6094 *dom_list.domains[i].sid,
6095 dom_list.domains[i].name.string);
6096 if (!NT_STATUS_IS_OK(nt_status)) {
6097 cli_shutdown(cli);
6098 talloc_destroy(mem_ctx);
6099 return -1;
6100 }
6101 };
6102
6103 /*
6104 * in case of no trusted domains say something rather
6105 * than just display blank line
6106 */
6107 if (!dom_list.count) d_printf("none\n");
6108
6109 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6110
6111 /* close this connection before doing next one */
6112 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6113 if (NT_STATUS_IS_ERR(nt_status)) {
6114 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6115 nt_errstr(nt_status)));
6116 cli_shutdown(cli);
6117 talloc_destroy(mem_ctx);
6118 return -1;
6119 };
6120
6121 /* close lsarpc pipe and connection to IPC$ */
6122 cli_shutdown(cli);
6123
6124 talloc_destroy(mem_ctx);
6125 return 0;
6126}
6127
6128static int rpc_trustdom_list(int argc, const char **argv)
6129{
6130 /* common variables */
6131 TALLOC_CTX* mem_ctx;
6132 struct cli_state *cli = NULL, *remote_cli = NULL;
6133 struct rpc_pipe_client *pipe_hnd = NULL;
6134 NTSTATUS nt_status;
6135 const char *domain_name = NULL;
6136 DOM_SID *queried_dom_sid;
6137 fstring padding;
6138 int ascii_dom_name_len;
6139 POLICY_HND connect_hnd;
6140 union lsa_PolicyInformation *info = NULL;
6141
6142 /* trusted domains listing variables */
6143 unsigned int num_domains, enum_ctx = 0;
6144 int i, pad_len, col_len = 20;
6145 struct lsa_DomainList dom_list;
6146 fstring pdc_name;
6147
6148 /* trusting domains listing variables */
6149 POLICY_HND domain_hnd;
6150 struct samr_SamArray *trusts = NULL;
6151
6152 /*
6153 * Listing trusted domains (stored in secrets.tdb, if local)
6154 */
6155
6156 mem_ctx = talloc_init("trust relationships listing");
6157
6158 /*
6159 * set domain and pdc name to local samba server (default)
6160 * or to remote one given in command line
6161 */
6162
6163 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
6164 domain_name = opt_workgroup;
6165 opt_target_workgroup = opt_workgroup;
6166 } else {
6167 fstrcpy(pdc_name, global_myname());
6168 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6169 opt_target_workgroup = domain_name;
6170 };
6171
6172 /* open \PIPE\lsarpc and open policy handle */
6173 nt_status = net_make_ipc_connection(NET_FLAGS_PDC, &cli);
6174 if (!NT_STATUS_IS_OK(nt_status)) {
6175 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6176 nt_errstr(nt_status)));
6177 talloc_destroy(mem_ctx);
6178 return -1;
6179 };
6180
6181 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6182 if (!pipe_hnd) {
6183 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6184 nt_errstr(nt_status) ));
6185 cli_shutdown(cli);
6186 talloc_destroy(mem_ctx);
6187 return -1;
6188 };
6189
6190 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
6191 &connect_hnd);
6192 if (NT_STATUS_IS_ERR(nt_status)) {
6193 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6194 nt_errstr(nt_status)));
6195 cli_shutdown(cli);
6196 talloc_destroy(mem_ctx);
6197 return -1;
6198 };
6199
6200 /* query info level 5 to obtain sid of a domain being queried */
6201 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6202 &connect_hnd,
6203 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6204 &info);
6205
6206 if (NT_STATUS_IS_ERR(nt_status)) {
6207 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6208 nt_errstr(nt_status)));
6209 cli_shutdown(cli);
6210 talloc_destroy(mem_ctx);
6211 return -1;
6212 }
6213
6214 queried_dom_sid = info->account_domain.sid;
6215
6216 /*
6217 * Keep calling LsaEnumTrustdom over opened pipe until
6218 * the end of enumeration is reached
6219 */
6220
6221 d_printf("Trusted domains list:\n\n");
6222
6223 do {
6224 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6225 &connect_hnd,
6226 &enum_ctx,
6227 &dom_list,
6228 (uint32_t)-1);
6229 if (NT_STATUS_IS_ERR(nt_status)) {
6230 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6231 nt_errstr(nt_status)));
6232 cli_shutdown(cli);
6233 talloc_destroy(mem_ctx);
6234 return -1;
6235 };
6236
6237 for (i = 0; i < dom_list.count; i++) {
6238 print_trusted_domain(dom_list.domains[i].sid,
6239 dom_list.domains[i].name.string);
6240 };
6241
6242 /*
6243 * in case of no trusted domains say something rather
6244 * than just display blank line
6245 */
6246 if (!dom_list.count) d_printf("none\n");
6247
6248 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6249
6250 /* close this connection before doing next one */
6251 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6252 if (NT_STATUS_IS_ERR(nt_status)) {
6253 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6254 nt_errstr(nt_status)));
6255 cli_shutdown(cli);
6256 talloc_destroy(mem_ctx);
6257 return -1;
6258 };
6259
6260 cli_rpc_pipe_close(pipe_hnd);
6261
6262 /*
6263 * Listing trusting domains (stored in passdb backend, if local)
6264 */
6265
6266 d_printf("\nTrusting domains list:\n\n");
6267
6268 /*
6269 * Open \PIPE\samr and get needed policy handles
6270 */
6271 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6272 if (!pipe_hnd) {
6273 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6274 cli_shutdown(cli);
6275 talloc_destroy(mem_ctx);
6276 return -1;
6277 };
6278
6279 /* SamrConnect2 */
6280 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6281 pipe_hnd->cli->desthost,
6282 SA_RIGHT_SAM_LOOKUP_DOMAIN,
6283 &connect_hnd);
6284 if (!NT_STATUS_IS_OK(nt_status)) {
6285 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6286 nt_errstr(nt_status)));
6287 cli_shutdown(cli);
6288 talloc_destroy(mem_ctx);
6289 return -1;
6290 };
6291
6292 /* SamrOpenDomain - we have to open domain policy handle in order to be
6293 able to enumerate accounts*/
6294 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6295 &connect_hnd,
6296 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6297 queried_dom_sid,
6298 &domain_hnd);
6299 if (!NT_STATUS_IS_OK(nt_status)) {
6300 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6301 nt_errstr(nt_status)));
6302 cli_shutdown(cli);
6303 talloc_destroy(mem_ctx);
6304 return -1;
6305 };
6306
6307 /*
6308 * perform actual enumeration
6309 */
6310
6311 enum_ctx = 0; /* reset enumeration context from last enumeration */
6312 do {
6313
6314 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6315 &domain_hnd,
6316 &enum_ctx,
6317 ACB_DOMTRUST,
6318 &trusts,
6319 0xffff,
6320 &num_domains);
6321 if (NT_STATUS_IS_ERR(nt_status)) {
6322 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6323 nt_errstr(nt_status)));
6324 cli_shutdown(cli);
6325 talloc_destroy(mem_ctx);
6326 return -1;
6327 };
6328
6329 for (i = 0; i < num_domains; i++) {
6330
6331 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6332
6333 /*
6334 * get each single domain's sid (do we _really_ need this ?):
6335 * 1) connect to domain's pdc
6336 * 2) query the pdc for domain's sid
6337 */
6338
6339 /* get rid of '$' tail */
6340 ascii_dom_name_len = strlen(str);
6341 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6342 str[ascii_dom_name_len - 1] = '\0';
6343
6344 /* calculate padding space for d_printf to look nicer */
6345 pad_len = col_len - strlen(str);
6346 padding[pad_len] = 0;
6347 do padding[--pad_len] = ' '; while (pad_len);
6348
6349 /* set opt_* variables to remote domain */
6350 strupper_m(str);
6351 opt_workgroup = talloc_strdup(mem_ctx, str);
6352 opt_target_workgroup = opt_workgroup;
6353
6354 d_printf("%s%s", str, padding);
6355
6356 /* connect to remote domain controller */
6357 nt_status = net_make_ipc_connection(
6358 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6359 &remote_cli);
6360 if (NT_STATUS_IS_OK(nt_status)) {
6361 /* query for domain's sid */
6362 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6363 d_fprintf(stderr, "couldn't get domain's sid\n");
6364
6365 cli_shutdown(remote_cli);
6366
6367 } else {
6368 d_fprintf(stderr, "domain controller is not "
6369 "responding: %s\n",
6370 nt_errstr(nt_status));
6371 };
6372 };
6373
6374 if (!num_domains) d_printf("none\n");
6375
6376 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6377
6378 /* close opened samr and domain policy handles */
6379 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6380 if (!NT_STATUS_IS_OK(nt_status)) {
6381 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6382 };
6383
6384 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6385 if (!NT_STATUS_IS_OK(nt_status)) {
6386 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6387 };
6388
6389 /* close samr pipe and connection to IPC$ */
6390 cli_shutdown(cli);
6391
6392 talloc_destroy(mem_ctx);
6393 return 0;
6394}
6395
6396/**
6397 * Entrypoint for 'net rpc trustdom' code.
6398 *
6399 * @param argc Standard argc.
6400 * @param argv Standard argv without initial components.
6401 *
6402 * @return Integer status (0 means success).
6403 */
6404
6405static int rpc_trustdom(int argc, const char **argv)
6406{
6407 struct functable func[] = {
6408 {"add", rpc_trustdom_add},
6409 {"del", rpc_trustdom_del},
6410 {"establish", rpc_trustdom_establish},
6411 {"revoke", rpc_trustdom_revoke},
6412 {"help", rpc_trustdom_usage},
6413 {"list", rpc_trustdom_list},
6414 {"vampire", rpc_trustdom_vampire},
6415 {NULL, NULL}
6416 };
6417
6418 if (argc == 0) {
6419 rpc_trustdom_usage(argc, argv);
6420 return -1;
6421 }
6422
6423 return (net_run_function(argc, argv, func, rpc_trustdom_usage));
6424}
6425
6426/**
6427 * Check if a server will take rpc commands
6428 * @param flags Type of server to connect to (PDC, DMB, localhost)
6429 * if the host is not explicitly specified
6430 * @return bool (true means rpc supported)
6431 */
6432bool net_rpc_check(unsigned flags)
6433{
6434 struct cli_state *cli;
6435 bool ret = False;
6436 struct sockaddr_storage server_ss;
6437 char *server_name = NULL;
6438 NTSTATUS status;
6439
6440 /* flags (i.e. server type) may depend on command */
6441 if (!net_find_server(NULL, flags, &server_ss, &server_name))
6442 return False;
6443
6444 if ((cli = cli_initialise()) == NULL) {
6445 return False;
6446 }
6447
6448 status = cli_connect(cli, server_name, &server_ss);
6449 if (!NT_STATUS_IS_OK(status))
6450 goto done;
6451 if (!attempt_netbios_session_request(&cli, global_myname(),
6452 server_name, &server_ss))
6453 goto done;
6454 if (!cli_negprot(cli))
6455 goto done;
6456 if (cli->protocol < PROTOCOL_NT1)
6457 goto done;
6458
6459 ret = True;
6460 done:
6461 cli_shutdown(cli);
6462 return ret;
6463}
6464
6465/* dump sam database via samsync rpc calls */
6466static int rpc_samdump(int argc, const char **argv) {
6467 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6468 argc, argv);
6469}
6470
6471/* syncronise sam database via samsync rpc calls */
6472static int rpc_vampire(int argc, const char **argv) {
6473 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6474 argc, argv);
6475}
6476
6477/**
6478 * Migrate everything from a print-server.
6479 *
6480 * @param argc Standard main() style argc.
6481 * @param argv Standard main() style argv. Initial components are already
6482 * stripped.
6483 *
6484 * @return A shell status integer (0 for success).
6485 *
6486 * The order is important !
6487 * To successfully add drivers the print-queues have to exist !
6488 * Applying ACLs should be the last step, because you're easily locked out.
6489 *
6490 **/
6491static int rpc_printer_migrate_all(int argc, const char **argv)
6492{
6493 int ret;
6494
6495 if (!opt_host) {
6496 printf("no server to migrate\n");
6497 return -1;
6498 }
6499
6500 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6501 if (ret)
6502 return ret;
6503
6504 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6505 if (ret)
6506 return ret;
6507
6508 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6509 if (ret)
6510 return ret;
6511
6512 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6513 if (ret)
6514 return ret;
6515
6516 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6517
6518}
6519
6520/**
6521 * Migrate print-drivers from a print-server.
6522 *
6523 * @param argc Standard main() style argc.
6524 * @param argv Standard main() style argv. Initial components are already
6525 * stripped.
6526 *
6527 * @return A shell status integer (0 for success).
6528 **/
6529static int rpc_printer_migrate_drivers(int argc, const char **argv)
6530{
6531 if (!opt_host) {
6532 printf("no server to migrate\n");
6533 return -1;
6534 }
6535
6536 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6537 rpc_printer_migrate_drivers_internals,
6538 argc, argv);
6539}
6540
6541/**
6542 * Migrate print-forms from a print-server.
6543 *
6544 * @param argc Standard main() style argc.
6545 * @param argv Standard main() style argv. Initial components are already
6546 * stripped.
6547 *
6548 * @return A shell status integer (0 for success).
6549 **/
6550static int rpc_printer_migrate_forms(int argc, const char **argv)
6551{
6552 if (!opt_host) {
6553 printf("no server to migrate\n");
6554 return -1;
6555 }
6556
6557 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6558 rpc_printer_migrate_forms_internals,
6559 argc, argv);
6560}
6561
6562/**
6563 * Migrate printers from a print-server.
6564 *
6565 * @param argc Standard main() style argc.
6566 * @param argv Standard main() style argv. Initial components are already
6567 * stripped.
6568 *
6569 * @return A shell status integer (0 for success).
6570 **/
6571static int rpc_printer_migrate_printers(int argc, const char **argv)
6572{
6573 if (!opt_host) {
6574 printf("no server to migrate\n");
6575 return -1;
6576 }
6577
6578 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6579 rpc_printer_migrate_printers_internals,
6580 argc, argv);
6581}
6582
6583/**
6584 * Migrate printer-ACLs from a print-server.
6585 *
6586 * @param argc Standard main() style argc.
6587 * @param argv Standard main() style argv. Initial components are already
6588 * stripped.
6589 *
6590 * @return A shell status integer (0 for success).
6591 **/
6592static int rpc_printer_migrate_security(int argc, const char **argv)
6593{
6594 if (!opt_host) {
6595 printf("no server to migrate\n");
6596 return -1;
6597 }
6598
6599 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6600 rpc_printer_migrate_security_internals,
6601 argc, argv);
6602}
6603
6604/**
6605 * Migrate printer-settings from a print-server.
6606 *
6607 * @param argc Standard main() style argc.
6608 * @param argv Standard main() style argv. Initial components are already
6609 * stripped.
6610 *
6611 * @return A shell status integer (0 for success).
6612 **/
6613static int rpc_printer_migrate_settings(int argc, const char **argv)
6614{
6615 if (!opt_host) {
6616 printf("no server to migrate\n");
6617 return -1;
6618 }
6619
6620 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6621 rpc_printer_migrate_settings_internals,
6622 argc, argv);
6623}
6624
6625/**
6626 * 'net rpc printer' entrypoint.
6627 * @param argc Standard main() style argc.
6628 * @param argv Standard main() style argv. Initial components are already
6629 * stripped.
6630 **/
6631
6632int rpc_printer_migrate(int argc, const char **argv)
6633{
6634
6635 /* ouch: when addriver and setdriver are called from within
6636 rpc_printer_migrate_drivers_internals, the printer-queue already
6637 *has* to exist */
6638
6639 struct functable func[] = {
6640 {"all", rpc_printer_migrate_all},
6641 {"drivers", rpc_printer_migrate_drivers},
6642 {"forms", rpc_printer_migrate_forms},
6643 {"help", rpc_printer_usage},
6644 {"printers", rpc_printer_migrate_printers},
6645 {"security", rpc_printer_migrate_security},
6646 {"settings", rpc_printer_migrate_settings},
6647 {NULL, NULL}
6648 };
6649
6650 return net_run_function(argc, argv, func, rpc_printer_usage);
6651}
6652
6653
6654/**
6655 * List printers on a remote RPC server.
6656 *
6657 * @param argc Standard main() style argc.
6658 * @param argv Standard main() style argv. Initial components are already
6659 * stripped.
6660 *
6661 * @return A shell status integer (0 for success).
6662 **/
6663static int rpc_printer_list(int argc, const char **argv)
6664{
6665
6666 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6667 rpc_printer_list_internals,
6668 argc, argv);
6669}
6670
6671/**
6672 * List printer-drivers on a remote RPC server.
6673 *
6674 * @param argc Standard main() style argc.
6675 * @param argv Standard main() style argv. Initial components are already
6676 * stripped.
6677 *
6678 * @return A shell status integer (0 for success).
6679 **/
6680static int rpc_printer_driver_list(int argc, const char **argv)
6681{
6682
6683 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6684 rpc_printer_driver_list_internals,
6685 argc, argv);
6686}
6687
6688/**
6689 * Publish printer in ADS via MSRPC.
6690 *
6691 * @param argc Standard main() style argc.
6692 * @param argv Standard main() style argv. Initial components are already
6693 * stripped.
6694 *
6695 * @return A shell status integer (0 for success).
6696 **/
6697static int rpc_printer_publish_publish(int argc, const char **argv)
6698{
6699
6700 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6701 rpc_printer_publish_publish_internals,
6702 argc, argv);
6703}
6704
6705/**
6706 * Update printer in ADS via MSRPC.
6707 *
6708 * @param argc Standard main() style argc.
6709 * @param argv Standard main() style argv. Initial components are already
6710 * stripped.
6711 *
6712 * @return A shell status integer (0 for success).
6713 **/
6714static int rpc_printer_publish_update(int argc, const char **argv)
6715{
6716
6717 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6718 rpc_printer_publish_update_internals,
6719 argc, argv);
6720}
6721
6722/**
6723 * UnPublish printer in ADS via MSRPC
6724 *
6725 * @param argc Standard main() style argc.
6726 * @param argv Standard main() style argv. Initial components are already
6727 * stripped.
6728 *
6729 * @return A shell status integer (0 for success).
6730 **/
6731static int rpc_printer_publish_unpublish(int argc, const char **argv)
6732{
6733
6734 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6735 rpc_printer_publish_unpublish_internals,
6736 argc, argv);
6737}
6738
6739/**
6740 * List published printers via MSRPC.
6741 *
6742 * @param argc Standard main() style argc.
6743 * @param argv Standard main() style argv. Initial components are already
6744 * stripped.
6745 *
6746 * @return A shell status integer (0 for success).
6747 **/
6748static int rpc_printer_publish_list(int argc, const char **argv)
6749{
6750
6751 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6752 rpc_printer_publish_list_internals,
6753 argc, argv);
6754}
6755
6756
6757/**
6758 * Publish printer in ADS.
6759 *
6760 * @param argc Standard main() style argc.
6761 * @param argv Standard main() style argv. Initial components are already
6762 * stripped.
6763 *
6764 * @return A shell status integer (0 for success).
6765 **/
6766static int rpc_printer_publish(int argc, const char **argv)
6767{
6768
6769 struct functable func[] = {
6770 {"publish", rpc_printer_publish_publish},
6771 {"update", rpc_printer_publish_update},
6772 {"unpublish", rpc_printer_publish_unpublish},
6773 {"list", rpc_printer_publish_list},
6774 {"help", rpc_printer_usage},
6775 {NULL, NULL}
6776 };
6777
6778 if (argc == 0)
6779 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6780 rpc_printer_publish_list_internals,
6781 argc, argv);
6782
6783 return net_run_function(argc, argv, func, rpc_printer_usage);
6784
6785}
6786
6787
6788/**
6789 * Display rpc printer help page.
6790 * @param argc Standard main() style argc.
6791 * @param argv Standard main() style argv. Initial components are already
6792 * stripped.
6793 **/
6794int rpc_printer_usage(int argc, const char **argv)
6795{
6796 return net_help_printer(argc, argv);
6797}
6798
6799/**
6800 * 'net rpc printer' entrypoint.
6801 * @param argc Standard main() style argc.
6802 * @param argv Standard main() style argv. Initial components are already
6803 * stripped.
6804 **/
6805int net_rpc_printer(int argc, const char **argv)
6806{
6807 struct functable func[] = {
6808 {"list", rpc_printer_list},
6809 {"migrate", rpc_printer_migrate},
6810 {"driver", rpc_printer_driver_list},
6811 {"publish", rpc_printer_publish},
6812 {NULL, NULL}
6813 };
6814
6815 if (argc == 0)
6816 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6817 rpc_printer_list_internals,
6818 argc, argv);
6819
6820 return net_run_function(argc, argv, func, rpc_printer_usage);
6821}
6822
6823/****************************************************************************/
6824
6825
6826/**
6827 * Basic usage function for 'net rpc'.
6828 * @param argc Standard main() style argc.
6829 * @param argv Standard main() style argv. Initial components are already
6830 * stripped.
6831 **/
6832
6833int net_rpc_usage(int argc, const char **argv)
6834{
6835 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6836 d_printf(" net rpc join \t\t\tto join a domain \n");
6837 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
6838 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6839 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6840 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6841 d_printf(" net rpc group \t\tto list groups\n");
6842 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6843 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6844 d_printf(" net rpc file \t\t\tto list open files\n");
6845 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6846 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6847 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6848 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
6849 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6850 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6851 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6852 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6853 d_printf(" net rpc registry\t\tto manage registry hives\n");
6854 d_printf(" net rpc service\t\tto start, stop and query services\n");
6855 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6856 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6857 d_printf("\n");
6858 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6859 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6860 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6861 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6862 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6863 return -1;
6864}
6865
6866
6867/**
6868 * Help function for 'net rpc'. Calls command specific help if requested
6869 * or displays usage of net rpc.
6870 * @param argc Standard main() style argc.
6871 * @param argv Standard main() style argv. Initial components are already
6872 * stripped.
6873 **/
6874
6875int net_rpc_help(int argc, const char **argv)
6876{
6877 struct functable func[] = {
6878 {"join", rpc_join_usage},
6879 {"user", rpc_user_usage},
6880 {"group", rpc_group_usage},
6881 {"share", rpc_share_usage},
6882 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6883 {"trustdom", rpc_trustdom_usage},
6884 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6885 /*{"shutdown", rpc_shutdown_usage}, */
6886 {"vampire", rpc_vampire_usage},
6887 {NULL, NULL}
6888 };
6889
6890 if (argc == 0) {
6891 net_rpc_usage(argc, argv);
6892 return -1;
6893 }
6894
6895 return (net_run_function(argc, argv, func, rpc_user_usage));
6896}
6897
6898/**
6899 * 'net rpc' entrypoint.
6900 * @param argc Standard main() style argc.
6901 * @param argv Standard main() style argv. Initial components are already
6902 * stripped.
6903 **/
6904
6905int net_rpc(int argc, const char **argv)
6906{
6907 struct functable func[] = {
6908 {"audit", net_rpc_audit},
6909 {"info", net_rpc_info},
6910 {"join", net_rpc_join},
6911 {"oldjoin", net_rpc_oldjoin},
6912 {"testjoin", net_rpc_testjoin},
6913 {"user", net_rpc_user},
6914 {"password", rpc_user_password},
6915 {"group", net_rpc_group},
6916 {"share", net_rpc_share},
6917 {"file", net_rpc_file},
6918 {"printer", net_rpc_printer},
6919 {"changetrustpw", net_rpc_changetrustpw},
6920 {"trustdom", rpc_trustdom},
6921 {"abortshutdown", rpc_shutdown_abort},
6922 {"shutdown", rpc_shutdown},
6923 {"samdump", rpc_samdump},
6924 {"vampire", rpc_vampire},
6925 {"getsid", net_rpc_getsid},
6926 {"rights", net_rpc_rights},
6927 {"service", net_rpc_service},
6928 {"registry", net_rpc_registry},
6929 {"shell", net_rpc_shell},
6930 {"help", net_rpc_help},
6931 {NULL, NULL}
6932 };
6933 return net_run_function(argc, argv, func, net_rpc_usage);
6934}
Note: See TracBrowser for help on using the repository browser.