Updates from Samba HEAD:
[ira/wip.git] / source3 / utils / net_rpc.c
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
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20  
21 #include "includes.h"
22 #include "../utils/net.h"
23
24 extern pstring global_myname;
25
26 /**
27  * @file net_rpc.c
28  *
29  * @brief RPC based subcommands for the 'net' utility.
30  *
31  * This file should contain much of the functionality that used to
32  * be found in rpcclient, execpt that the commands should change 
33  * less often, and the fucntionality should be sane (the user is not 
34  * expected to know a rid/sid before they conduct an operation etc.)
35  *
36  * @todo Perhaps eventually these should be split out into a number
37  * of files, as this could get quite big.
38  **/
39
40
41 /* A function of this type is passed to the 'run_rpc_command' wrapper */
42 typedef NTSTATUS (*rpc_command_fn)(const DOM_SID *, struct cli_state *, TALLOC_CTX *, int, const char **);
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
53 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli)
54 {
55         DOM_SID *domain_sid;
56         POLICY_HND pol;
57         NTSTATUS result = NT_STATUS_OK;
58         uint32 info_class = 5;
59         fstring domain_name;
60         TALLOC_CTX *mem_ctx;
61         
62         if (!(domain_sid = malloc(sizeof(DOM_SID)))){
63                 DEBUG(0,("net_get_remote_domain_sid: malloc returned NULL!\n"));
64                 goto error;
65         }
66             
67         if (!(mem_ctx=talloc_init()))
68         {
69                 DEBUG(0,("net_get_remote_domain_sid: talloc_init returned NULL!\n"));
70                 goto error;
71         }
72
73
74         if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
75                 fprintf(stderr, "could not initialise lsa pipe\n");
76                 goto error;
77         }
78         
79         result = cli_lsa_open_policy(cli, mem_ctx, True, 
80                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
81                                      &pol);
82         if (!NT_STATUS_IS_OK(result)) {
83                 goto error;
84         }
85
86         result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class, 
87                                            domain_name, domain_sid);
88         if (!NT_STATUS_IS_OK(result)) {
89                 goto error;
90         }
91
92         cli_lsa_close(cli, mem_ctx, &pol);
93         cli_nt_session_close(cli);
94         talloc_destroy(mem_ctx);
95
96         return domain_sid;
97
98  error:
99         fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
100
101         if (!NT_STATUS_IS_OK(result)) {
102                 fprintf(stderr, "error: %s\n", nt_errstr(result));
103         }
104
105         exit(1);
106 }
107
108 /**
109  * Run a single RPC command, from start to finish.
110  *
111  * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
112  * @param conn_flag a NET_FLAG_ combination.  Passed to 
113  *                   net_make_ipc_connection.
114  * @param argc  Standard main() style argc
115  * @param argc  Standard main() style argv.  Initial components are already
116  *              stripped
117  * @return A shell status integer (0 for success)
118  */
119
120 static int run_rpc_command(struct cli_state *cli_arg, const char *pipe_name, int conn_flags,
121                            rpc_command_fn fn,
122                            int argc, const char **argv) 
123 {
124         struct cli_state *cli = NULL;
125         TALLOC_CTX *mem_ctx;
126         NTSTATUS nt_status;
127         DOM_SID *domain_sid;
128
129         /* make use of cli_state handed over as an argument, if possible */
130         if (!cli_arg)
131                 cli = net_make_ipc_connection(conn_flags);
132         else
133                 cli = cli_arg;
134
135         if (!cli) {
136                 return -1;
137         }
138
139         domain_sid = net_get_remote_domain_sid(cli);
140
141         /* Create mem_ctx */
142         
143         if (!(mem_ctx = talloc_init())) {
144                 DEBUG(0, ("talloc_init() failed\n"));
145                 cli_shutdown(cli);
146                 return -1;
147         }
148         
149         if (!cli_nt_session_open(cli, pipe_name)) {
150                 DEBUG(0, ("Could not initialise %s pipe\n", pipe_name));
151         }
152         
153         nt_status = fn(domain_sid, cli, mem_ctx, argc, argv);
154         
155         if (!NT_STATUS_IS_OK(nt_status)) {
156                 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
157         } else {
158                 DEBUG(5, ("rpc command function succedded\n"));
159         }
160                 
161             
162         if (cli->nt_pipe_fnum)
163                 cli_nt_session_close(cli);
164         
165         /* close the connection only if it was opened here */
166         if (!cli_arg)
167                 cli_shutdown(cli);
168         
169         talloc_destroy(mem_ctx);
170
171         return (!NT_STATUS_IS_OK(nt_status));
172 }
173
174
175 /****************************************************************************/
176
177
178 /** 
179  * Force a change of the trust acccount password.
180  *
181  * All parameters are provided by the run_rpc_command function, except for
182  * argc, argv which are passes through. 
183  *
184  * @param domain_sid The domain sid aquired from the remote server
185  * @param cli A cli_state connected to the server.
186  * @param mem_ctx Talloc context, destoyed on compleation of the function.
187  * @param argc  Standard main() style argc
188  * @param argc  Standard main() style argv.  Initial components are already
189  *              stripped
190  *
191  * @return Normal NTSTATUS return.
192  **/
193
194 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
195                                        int argc, const char **argv) {
196         
197         return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
198 }
199
200 /** 
201  * Force a change of the trust acccount password.
202  *
203  * @param argc  Standard main() style argc
204  * @param argc  Standard main() style argv.  Initial components are already
205  *              stripped
206  *
207  * @return A shell status integer (0 for success)
208  **/
209
210 static int rpc_changetrustpw(int argc, const char **argv) 
211 {
212         return run_rpc_command(NULL, PIPE_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_changetrustpw_internals,
213                                argc, argv);
214 }
215
216
217 /****************************************************************************/
218
219
220 /** 
221  * Join a domain, the old way.
222  *
223  * This uses 'machinename' as the inital password, and changes it. 
224  *
225  * The password should be created with 'server manager' or eqiv first.
226  *
227  * All parameters are provided by the run_rpc_command function, except for
228  * argc, argv which are passes through. 
229  *
230  * @param domain_sid The domain sid aquired from the remote server
231  * @param cli A cli_state connected to the server.
232  * @param mem_ctx Talloc context, destoyed on compleation of the function.
233  * @param argc  Standard main() style argc
234  * @param argc  Standard main() style argv.  Initial components are already
235  *              stripped
236  *
237  * @return Normal NTSTATUS return.
238  **/
239
240 static NTSTATUS rpc_join_oldstyle_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
241                                        int argc, const char **argv) {
242         
243         extern pstring global_myname;
244         fstring trust_passwd;
245         unsigned char orig_trust_passwd_hash[16];
246         NTSTATUS result;
247
248         fstrcpy(trust_passwd, global_myname);
249         strlower(trust_passwd);
250
251         /*
252          * Machine names can be 15 characters, but the max length on
253          * a password is 14.  --jerry
254          */
255
256         trust_passwd[14] = '\0';
257
258         E_md4hash(trust_passwd, orig_trust_passwd_hash);
259
260         result = trust_pw_change_and_store_it(cli, mem_ctx, orig_trust_passwd_hash);
261
262         if (NT_STATUS_IS_OK(result))
263                 printf("Joined domain %s.\n",lp_workgroup());
264
265         return result;
266 }
267
268 /** 
269  * Join a domain, the old way.
270  *
271  * @param argc  Standard main() style argc
272  * @param argc  Standard main() style argv.  Initial components are already
273  *              stripped
274  *
275  * @return A shell status integer (0 for success)
276  **/
277
278 static int net_rpc_join_oldstyle(int argc, const char **argv) 
279 {
280         return run_rpc_command(NULL, PIPE_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_join_oldstyle_internals,
281                                argc, argv);
282 }
283
284 /** 
285  * Basic usage function for 'net rpc join'
286  * @param argc  Standard main() style argc
287  * @param argc  Standard main() style argv.  Initial components are already
288  *              stripped
289  **/
290
291 static int rpc_join_usage(int argc, const char **argv) 
292 {       
293         d_printf("net rpc join -U <username>[%%password] [options]\n"\
294                  "\t to join a domain with admin username & password\n"\
295                  "\t\t password will be prompted if none is specified\n");
296         d_printf("net rpc join [options except -U]\n"\
297                  "\t to join a domain created in server manager\n\n\n");
298
299         net_common_flags_usage(argc, argv);
300         return -1;
301 }
302
303 /** 
304  * 'net rpc join' entrypoint.
305  * @param argc  Standard main() style argc
306  * @param argc  Standard main() style argv.  Initial components are already
307  *              stripped
308  *
309  * Main 'net_rpc_join()' (where the admain username/password is used) is 
310  * in net_rpc_join.c
311  * Assume if a -U is specified, it's the new style, otherwise it's the
312  * old style.  If 'oldstyle' is specfied explicity, do it and don't prompt.
313  **/
314
315 int net_rpc_join(int argc, const char **argv) 
316 {
317         struct functable func[] = {
318                 {"oldstyle", net_rpc_join_oldstyle},
319                 {NULL, NULL}
320         };
321
322         if (argc == 0) {
323                 if ((net_rpc_join_oldstyle(argc, argv) == 0))
324                         return 0;
325                 
326                 return net_rpc_join_newstyle(argc, argv);
327         }
328
329         return net_run_function(argc, argv, func, rpc_join_usage);
330 }
331
332
333
334 /** 
335  * display info about a rpc domain
336  *
337  * All parameters are provided by the run_rpc_command function, except for
338  * argc, argv which are passes through. 
339  *
340  * @param domain_sid The domain sid acquired from the remote server
341  * @param cli A cli_state connected to the server.
342  * @param mem_ctx Talloc context, destoyed on completion of the function.
343  * @param argc  Standard main() style argc
344  * @param argv  Standard main() style argv.  Initial components are already
345  *              stripped
346  *
347  * @return Normal NTSTATUS return.
348  **/
349
350 static NTSTATUS 
351 rpc_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
352                    TALLOC_CTX *mem_ctx, int argc, const char **argv)
353 {
354         POLICY_HND connect_pol, domain_pol;
355         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
356         SAM_UNK_CTR ctr;
357         fstring sid_str;
358
359         sid_to_string(sid_str, domain_sid);
360
361         /* Get sam policy handle */     
362         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
363                                   &connect_pol);
364         if (!NT_STATUS_IS_OK(result)) {
365                 goto done;
366         }
367         
368         /* Get domain policy handle */
369         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
370                                       MAXIMUM_ALLOWED_ACCESS,
371                                       domain_sid, &domain_pol);
372         if (!NT_STATUS_IS_OK(result)) {
373                 goto done;
374         }
375
376         ZERO_STRUCT(ctr);
377         result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
378                                          2, &ctr);
379         if (NT_STATUS_IS_OK(result)) {
380                 TALLOC_CTX *ctx = talloc_init();
381                 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
382                 d_printf("Domain SID: %s\n", sid_str);
383                 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
384                 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
385                 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
386                 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
387                 talloc_destroy(ctx);
388         }
389
390  done:
391         return result;
392 }
393
394
395 /** 
396  * 'net rpc info' entrypoint.
397  * @param argc  Standard main() style argc
398  * @param argc  Standard main() style argv.  Initial components are already
399  *              stripped
400  **/
401 int net_rpc_info(int argc, const char **argv) 
402 {
403         return run_rpc_command(NULL, PIPE_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, 
404                                rpc_info_internals,
405                                argc, argv);
406 }
407
408
409 /** 
410  * Fetch domain SID into the local secrets.tdb
411  *
412  * All parameters are provided by the run_rpc_command function, except for
413  * argc, argv which are passes through. 
414  *
415  * @param domain_sid The domain sid acquired from the remote server
416  * @param cli A cli_state connected to the server.
417  * @param mem_ctx Talloc context, destoyed on completion of the function.
418  * @param argc  Standard main() style argc
419  * @param argv  Standard main() style argv.  Initial components are already
420  *              stripped
421  *
422  * @return Normal NTSTATUS return.
423  **/
424
425 static NTSTATUS 
426 rpc_getsid_internals(const DOM_SID *domain_sid, struct cli_state *cli,
427                    TALLOC_CTX *mem_ctx, int argc, const char **argv)
428 {
429         fstring sid_str;
430
431         sid_to_string(sid_str, domain_sid);
432         d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
433                  sid_str, lp_workgroup());
434
435         if (!secrets_store_domain_sid(global_myname, domain_sid)) {
436                 DEBUG(0,("Can't store domain SID\n"));
437                 return NT_STATUS_UNSUCCESSFUL;
438         }
439
440         return NT_STATUS_OK;
441 }
442
443
444 /** 
445  * 'net rpc getsid' entrypoint.
446  * @param argc  Standard main() style argc
447  * @param argc  Standard main() style argv.  Initial components are already
448  *              stripped
449  **/
450 int net_rpc_getsid(int argc, const char **argv) 
451 {
452         return run_rpc_command(NULL, PIPE_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, 
453                                rpc_getsid_internals,
454                                argc, argv);
455 }
456
457
458 /****************************************************************************/
459
460 /**
461  * Basic usage function for 'net rpc user'
462  * @param argc  Standard main() style argc.
463  * @param argv  Standard main() style argv.  Initial components are already
464  *              stripped.
465  **/
466
467 static int rpc_user_usage(int argc, const char **argv)
468 {
469         return net_help_user(argc, argv);
470 }
471
472 /** 
473  * Add a new user to a remote RPC server
474  *
475  * All parameters are provided by the run_rpc_command function, except for
476  * argc, argv which are passes through. 
477  *
478  * @param domain_sid The domain sid acquired from the remote server
479  * @param cli A cli_state connected to the server.
480  * @param mem_ctx Talloc context, destoyed on completion of the function.
481  * @param argc  Standard main() style argc
482  * @param argv  Standard main() style argv.  Initial components are already
483  *              stripped
484  *
485  * @return Normal NTSTATUS return.
486  **/
487
488 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
489                                        int argc, const char **argv) {
490         
491         POLICY_HND connect_pol, domain_pol, user_pol;
492         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
493         const char *acct_name;
494         uint16 acb_info;
495         uint32 unknown, user_rid;
496
497         if (argc != 1) {
498                 d_printf("User must be specified\n");
499                 rpc_user_usage(argc, argv);
500                 return NT_STATUS_OK;
501         }
502
503         acct_name = argv[0];
504
505         /* Get sam policy handle */
506         
507         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
508                                   &connect_pol);
509         if (!NT_STATUS_IS_OK(result)) {
510                 goto done;
511         }
512         
513         /* Get domain policy handle */
514         
515         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
516                                       MAXIMUM_ALLOWED_ACCESS,
517                                       domain_sid, &domain_pol);
518         if (!NT_STATUS_IS_OK(result)) {
519                 goto done;
520         }
521
522         /* Create domain user */
523
524         acb_info = ACB_NORMAL;
525         unknown = 0xe005000b; /* No idea what this is - a permission mask? */
526
527         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
528                                           acct_name, acb_info, unknown,
529                                           &user_pol, &user_rid);
530         if (!NT_STATUS_IS_OK(result)) {
531                 goto done;
532         }
533
534  done:
535         if (!NT_STATUS_IS_OK(result)) {
536                 d_printf("Failed to add user %s - %s\n", acct_name, 
537                          nt_errstr(result));
538         } else {
539                 d_printf("Added user %s\n", acct_name);
540         }
541         return result;
542 }
543
544 /** 
545  * Add a new user to a remote RPC server
546  *
547  * @param argc  Standard main() style argc
548  * @param argv  Standard main() style argv.  Initial components are already
549  *              stripped
550  *
551  * @return A shell status integer (0 for success)
552  **/
553
554 static int rpc_user_add(int argc, const char **argv) 
555 {
556         return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_user_add_internals,
557                                argc, argv);
558 }
559
560 /** 
561  * Delete a user from a remote RPC server
562  *
563  * All parameters are provided by the run_rpc_command function, except for
564  * argc, argv which are passes through. 
565  *
566  * @param domain_sid The domain sid acquired from the remote server
567  * @param cli A cli_state connected to the server.
568  * @param mem_ctx Talloc context, destoyed on completion of the function.
569  * @param argc  Standard main() style argc
570  * @param argv  Standard main() style argv.  Initial components are already
571  *              stripped
572  *
573  * @return Normal NTSTATUS return.
574  **/
575
576 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid, 
577                                        struct cli_state *cli, 
578                                        TALLOC_CTX *mem_ctx, 
579                                        int argc, const char **argv)
580 {
581         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
582         POLICY_HND connect_pol, domain_pol, user_pol;
583
584         if (argc < 1) {
585                 d_printf("User must be specified\n");
586                 rpc_user_usage(argc, argv);
587                 return NT_STATUS_OK;
588         }
589         /* Get sam policy and domain handles */
590
591         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
592                                   &connect_pol);
593
594         if (!NT_STATUS_IS_OK(result)) {
595                 goto done;
596         }
597
598         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
599                                       MAXIMUM_ALLOWED_ACCESS,
600                                       domain_sid, &domain_pol);
601
602         if (!NT_STATUS_IS_OK(result)) {
603                 goto done;
604         }
605
606         /* Get handle on user */
607
608         {
609                 uint32 *user_rids, num_rids, *name_types;
610                 uint32 flags = 0x000003e8; /* Unknown */
611
612                 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
613                                                flags, 1, &argv[0],
614                                                &num_rids, &user_rids,
615                                                &name_types);
616
617                 if (!NT_STATUS_IS_OK(result)) {
618                         goto done;
619                 }
620
621                 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
622                                             MAXIMUM_ALLOWED_ACCESS,
623                                             user_rids[0], &user_pol);
624
625                 if (!NT_STATUS_IS_OK(result)) {
626                         goto done;
627                 }
628         }
629
630         /* Delete user */
631
632         result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
633
634         if (!NT_STATUS_IS_OK(result)) {
635                 goto done;
636         }
637
638         /* Display results */
639
640  done:
641         return result;
642
643 }       
644
645 /** 
646  * Delete a user from a remote RPC server
647  *
648  * @param argc  Standard main() style argc
649  * @param argv  Standard main() style argv.  Initial components are already
650  *              stripped
651  *
652  * @return A shell status integer (0 for success)
653  **/
654
655 static int rpc_user_delete(int argc, const char **argv) 
656 {
657         return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_user_del_internals,
658                                argc, argv);
659 }
660
661 /** 
662  * List user's groups on a remote RPC server
663  *
664  * All parameters are provided by the run_rpc_command function, except for
665  * argc, argv which are passes through. 
666  *
667  * @param domain_sid The domain sid acquired from the remote server
668  * @param cli A cli_state connected to the server.
669  * @param mem_ctx Talloc context, destoyed on completion of the function.
670  * @param argc  Standard main() style argc
671  * @param argv  Standard main() style argv.  Initial components are already
672  *              stripped
673  *
674  * @return Normal NTSTATUS return.
675  **/
676
677 static NTSTATUS 
678 rpc_user_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
679                         TALLOC_CTX *mem_ctx, int argc, const char **argv)
680 {
681         POLICY_HND connect_pol, domain_pol, user_pol;
682         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
683         uint32 *rids, num_rids, *name_types, num_names;
684         uint32 flags = 0x000003e8; /* Unknown */
685         int i;
686         char **names;
687         DOM_GID *user_gids;
688
689         if (argc < 1) {
690                 d_printf("User must be specified\n");
691                 rpc_user_usage(argc, argv);
692                 return NT_STATUS_OK;
693         }
694         /* Get sam policy handle */
695         
696         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
697                                   &connect_pol);
698         if (!NT_STATUS_IS_OK(result)) goto done;
699         
700         /* Get domain policy handle */
701         
702         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
703                                       MAXIMUM_ALLOWED_ACCESS,
704                                       domain_sid, &domain_pol);
705         if (!NT_STATUS_IS_OK(result)) goto done;
706
707         /* Get handle on user */
708
709         result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
710                                        flags, 1, &argv[0],
711                                        &num_rids, &rids, &name_types);
712
713         if (!NT_STATUS_IS_OK(result)) goto done;
714
715         result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
716                                     MAXIMUM_ALLOWED_ACCESS,
717                                     rids[0], &user_pol);
718         if (!NT_STATUS_IS_OK(result)) goto done;
719
720         result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
721                                            &num_rids, &user_gids);
722
723         /* Look up rids */
724
725         rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
726
727         for (i = 0; i < num_rids; i++)
728                 rids[i] = user_gids[i].g_rid;
729
730         result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
731                                       flags, num_rids, rids,
732                                       &num_names, &names, &name_types);
733
734         if (!NT_STATUS_IS_OK(result)) {
735                 goto done;
736         }
737
738         /* Display results */
739
740         for (i = 0; i < num_names; i++)
741                 printf("%s\n", names[i]);
742
743  done:
744         return result;
745 }
746
747 /** 
748  * List a user's groups from a remote RPC server
749  *
750  * @param argc  Standard main() style argc
751  * @param argv  Standard main() style argv.  Initial components are already
752  *              stripped
753  *
754  * @return A shell status integer (0 for success)
755  **/
756
757 static int rpc_user_info(int argc, const char **argv) 
758 {
759         return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_user_info_internals,
760                                argc, argv);
761 }
762
763 /** 
764  * List users on a remote RPC server
765  *
766  * All parameters are provided by the run_rpc_command function, except for
767  * argc, argv which are passes through. 
768  *
769  * @param domain_sid The domain sid acquired from the remote server
770  * @param cli A cli_state connected to the server.
771  * @param mem_ctx Talloc context, destoyed on completion of the function.
772  * @param argc  Standard main() style argc
773  * @param argv  Standard main() style argv.  Initial components are already
774  *              stripped
775  *
776  * @return Normal NTSTATUS return.
777  **/
778
779 static NTSTATUS 
780 rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
781                         TALLOC_CTX *mem_ctx, int argc, const char **argv)
782 {
783         POLICY_HND connect_pol, domain_pol;
784         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
785         uint32 start_idx=0, max_entries=250, num_entries, i;
786         SAM_DISPINFO_CTR ctr;
787         SAM_DISPINFO_1 info1;
788
789         /* Get sam policy handle */
790         
791         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
792                                   &connect_pol);
793         if (!NT_STATUS_IS_OK(result)) {
794                 goto done;
795         }
796         
797         /* Get domain policy handle */
798         
799         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
800                                       MAXIMUM_ALLOWED_ACCESS,
801                                       domain_sid, &domain_pol);
802         if (!NT_STATUS_IS_OK(result)) {
803                 goto done;
804         }
805
806         /* Query domain users */
807         ZERO_STRUCT(ctr);
808         ZERO_STRUCT(info1);
809         ctr.sam.info1 = &info1;
810         if (opt_long_list_entries)
811                 d_printf("\nUser name             Comment"\
812                          "\n-----------------------------\n");
813         do {
814                 fstring user, desc;
815                 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
816                                                  &start_idx, 1, &num_entries,
817                                                  max_entries, &ctr);
818                 for (i = 0; i < num_entries; i++) {
819                         unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
820                         if (opt_long_list_entries) 
821                                 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
822                         
823                         if (opt_long_list_entries)
824                                 printf("%-21.21s %-50.50s\n", user, desc);
825                         else
826                                 printf("%s\n", user);
827                 }
828         } while (!NT_STATUS_IS_OK(result));
829
830  done:
831         return result;
832 }
833
834 /** 
835  * 'net rpc user' entrypoint.
836  * @param argc  Standard main() style argc
837  * @param argc  Standard main() style argv.  Initial components are already
838  *              stripped
839  **/
840
841 int net_rpc_user(int argc, const char **argv) 
842 {
843         struct functable func[] = {
844                 {"add", rpc_user_add},
845                 {"info", rpc_user_info},
846                 {"delete", rpc_user_delete},
847                 {NULL, NULL}
848         };
849         
850         if (argc == 0) {
851                 if (opt_long_list_entries) {
852                 } else {
853                 }
854                         return run_rpc_command(NULL,PIPE_SAMR, 0, 
855                                                rpc_user_list_internals,
856                                                argc, argv);
857         }
858
859         return net_run_function(argc, argv, func, rpc_user_usage);
860 }
861
862
863 /****************************************************************************/
864
865 /**
866  * Basic usage function for 'net rpc group'
867  * @param argc  Standard main() style argc.
868  * @param argv  Standard main() style argv.  Initial components are already
869  *              stripped.
870  **/
871
872 static int rpc_group_usage(int argc, const char **argv)
873 {
874         return net_help_group(argc, argv);
875 }
876
877 /** 
878  * List groups on a remote RPC server
879  *
880  * All parameters are provided by the run_rpc_command function, except for
881  * argc, argv which are passes through. 
882  *
883  * @param domain_sid The domain sid acquired from the remote server
884  * @param cli A cli_state connected to the server.
885  * @param mem_ctx Talloc context, destoyed on completion of the function.
886  * @param argc  Standard main() style argc
887  * @param argv  Standard main() style argv.  Initial components are already
888  *              stripped
889  *
890  * @return Normal NTSTATUS return.
891  **/
892
893 static NTSTATUS 
894 rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
895                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
896 {
897         POLICY_HND connect_pol, domain_pol;
898         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
899         uint32 start_idx=0, max_entries=250, num_entries, i;
900         struct acct_info *groups;
901         DOM_SID global_sid_Builtin;
902
903         string_to_sid(&global_sid_Builtin, "S-1-5-32");
904
905         /* Get sam policy handle */
906         
907         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
908                                   &connect_pol);
909         if (!NT_STATUS_IS_OK(result)) {
910                 goto done;
911         }
912         
913         /* Get domain policy handle */
914         
915         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
916                                       MAXIMUM_ALLOWED_ACCESS,
917                                       domain_sid, &domain_pol);
918         if (!NT_STATUS_IS_OK(result)) {
919                 goto done;
920         }
921
922         /* Query domain groups */
923         if (opt_long_list_entries)
924                 d_printf("\nGroup name            Comment"\
925                          "\n-----------------------------\n");
926         do {
927                 result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol,
928                                                   &start_idx, max_entries,
929                                                   &groups, &num_entries);
930                                                  
931                 for (i = 0; i < num_entries; i++) {
932                         if (opt_long_list_entries)
933                                 printf("%-21.21s %-50.50s\n", 
934                                        groups[i].acct_name,
935                                        groups[i].acct_desc);
936                         else
937                                 printf("%-21.21s\n", groups[i].acct_name);
938                 }
939         } while (!NT_STATUS_IS_OK(result));
940         /* query domain aliases */
941         do {
942                 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
943                                                   &start_idx, max_entries,
944                                                   &groups, &num_entries);
945                                                  
946                 for (i = 0; i < num_entries; i++) {
947                         if (opt_long_list_entries)
948                                 printf("%-21.21s %-50.50s\n", 
949                                        groups[i].acct_name,
950                                        groups[i].acct_desc);
951                         else
952                                 printf("%-21.21s\n", groups[i].acct_name);
953                 }
954         } while (!NT_STATUS_IS_OK(result));
955         cli_samr_close(cli, mem_ctx, &domain_pol);
956         /* Get builtin policy handle */
957         
958         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
959                                       MAXIMUM_ALLOWED_ACCESS,
960                                       &global_sid_Builtin, &domain_pol);
961         if (!NT_STATUS_IS_OK(result)) {
962                 goto done;
963         }
964         /* query builtin aliases */
965         do {
966                 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
967                                                   &start_idx, max_entries,
968                                                   &groups, &num_entries);
969                                                  
970                 for (i = 0; i < num_entries; i++) {
971                         if (opt_long_list_entries)
972                                 printf("%-21.21s %-50.50s\n", 
973                                        groups[i].acct_name,
974                                        groups[i].acct_desc);
975                         else
976                                 printf("%s\n", groups[i].acct_name);
977                 }
978         } while (!NT_STATUS_IS_OK(result));
979
980  done:
981         return result;
982 }
983
984 /** 
985  * 'net rpc group' entrypoint.
986  * @param argc  Standard main() style argc
987  * @param argc  Standard main() style argv.  Initial components are already
988  *              stripped
989  **/
990
991 int net_rpc_group(int argc, const char **argv) 
992 {
993         struct functable func[] = {
994 #if 0
995                 {"add", rpc_group_add},
996                 {"delete", rpc_group_delete},
997 #endif
998                 {NULL, NULL}
999         };
1000         
1001         if (argc == 0) {
1002                 if (opt_long_list_entries) {
1003                 } else {
1004                 }
1005                 return run_rpc_command(NULL, PIPE_SAMR, 0, 
1006                                        rpc_group_list_internals,
1007                                        argc, argv);
1008         }
1009
1010         return net_run_function(argc, argv, func, rpc_group_usage);
1011 }
1012
1013 /****************************************************************************/
1014
1015 static int rpc_share_usage(int argc, const char **argv)
1016 {
1017         return net_help_share(argc, argv);
1018 }
1019
1020 /** 
1021  * Add a share on a remote RPC server
1022  *
1023  * All parameters are provided by the run_rpc_command function, except for
1024  * argc, argv which are passes through. 
1025  *
1026  * @param domain_sid The domain sid acquired from the remote server
1027  * @param cli A cli_state connected to the server.
1028  * @param mem_ctx Talloc context, destoyed on completion of the function.
1029  * @param argc  Standard main() style argc
1030  * @param argv  Standard main() style argv.  Initial components are already
1031  *              stripped
1032  *
1033  * @return Normal NTSTATUS return.
1034  **/
1035 static NTSTATUS 
1036 rpc_share_add_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1037                         TALLOC_CTX *mem_ctx,int argc, const char **argv)
1038 {
1039         WERROR result;
1040         char *sharename=talloc_strdup(mem_ctx, argv[0]);
1041         char *path;
1042         uint32 type=0; /* only allow disk shares to be added */
1043         uint32 num_users=0, perms=0;
1044         char *password=NULL; /* don't allow a share password */
1045
1046         path = strchr(sharename, '=');
1047         if (!path)
1048                 return NT_STATUS_UNSUCCESSFUL;
1049         *path++ = '\0';
1050
1051         result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1052                                           opt_comment, perms, opt_maxusers,
1053                                           num_users, path, password);
1054         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1055 }
1056
1057 static int rpc_share_add(int argc, const char **argv)
1058 {
1059         if ((argc < 1) || !strchr(argv[0], '=')) {
1060                 DEBUG(1,("Sharename or path not specified on add\n"));
1061                 return rpc_share_usage(argc, argv);
1062         }
1063         return run_rpc_command(NULL, PIPE_SRVSVC, 0, 
1064                                rpc_share_add_internals,
1065                                argc, argv);
1066 }
1067
1068 /** 
1069  * Delete a share on a remote RPC server
1070  *
1071  * All parameters are provided by the run_rpc_command function, except for
1072  * argc, argv which are passes through. 
1073  *
1074  * @param domain_sid The domain sid acquired from the remote server
1075  * @param cli A cli_state connected to the server.
1076  * @param mem_ctx Talloc context, destoyed on completion of the function.
1077  * @param argc  Standard main() style argc
1078  * @param argv  Standard main() style argv.  Initial components are already
1079  *              stripped
1080  *
1081  * @return Normal NTSTATUS return.
1082  **/
1083 static NTSTATUS 
1084 rpc_share_del_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1085                         TALLOC_CTX *mem_ctx,int argc, const char **argv)
1086 {
1087         WERROR result;
1088
1089         result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1090         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1091 }
1092
1093 /** 
1094  * Delete a share on a remote RPC server
1095  *
1096  * @param domain_sid The domain sid acquired from the remote server
1097  * @param argc  Standard main() style argc
1098  * @param argv  Standard main() style argv.  Initial components are already
1099  *              stripped
1100  *
1101  * @return A shell status integer (0 for success)
1102  **/
1103 static int rpc_share_delete(int argc, const char **argv)
1104 {
1105         if (argc < 1) {
1106                 DEBUG(1,("Sharename not specified on delete\n"));
1107                 return rpc_share_usage(argc, argv);
1108         }
1109         return run_rpc_command(NULL, PIPE_SRVSVC, 0, 
1110                                rpc_share_del_internals,
1111                                argc, argv);
1112 }
1113
1114 /**
1115  * Formatted print of share info
1116  *
1117  * @param info1  pointer to SRV_SHARE_INFO_1 to format
1118  **/
1119  
1120 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1121 {
1122         fstring netname = "", remark = "";
1123
1124         rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1125         rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1126
1127         if (opt_long_list_entries) {
1128                 d_printf("%-12.12s %-8.8s %-50.50s\n",
1129                          netname, share_type[info1->info_1.type], remark);
1130         } else {
1131                 d_printf("%-12.12s\n", netname);
1132         }
1133
1134 }
1135
1136 /** 
1137  * List shares on a remote RPC server
1138  *
1139  * All parameters are provided by the run_rpc_command function, except for
1140  * argc, argv which are passes through. 
1141  *
1142  * @param domain_sid The domain sid acquired from the remote server
1143  * @param cli A cli_state connected to the server.
1144  * @param mem_ctx Talloc context, destoyed on completion of the function.
1145  * @param argc  Standard main() style argc
1146  * @param argv  Standard main() style argv.  Initial components are already
1147  *              stripped
1148  *
1149  * @return Normal NTSTATUS return.
1150  **/
1151
1152 static NTSTATUS 
1153 rpc_share_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1154                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
1155 {
1156         SRV_SHARE_INFO_CTR ctr;
1157         WERROR result;
1158         ENUM_HND hnd;
1159         uint32 preferred_len = 0xffffffff, i;
1160
1161         init_enum_hnd(&hnd, 0);
1162
1163         result = cli_srvsvc_net_share_enum(
1164                 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1165
1166         if (!W_ERROR_IS_OK(result))
1167                 goto done;
1168
1169         /* Display results */
1170
1171         if (opt_long_list_entries) {
1172                 d_printf(
1173         "\nEnumerating shared resources (exports) on remote server:\n\n"\
1174         "\nShare name   Type     Description\n"\
1175         "----------   ----     -----------\n");
1176         }
1177         for (i = 0; i < ctr.num_entries; i++)
1178                 display_share_info_1(&ctr.share.info1[i]);
1179  done:
1180         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1181 }
1182
1183 /** 
1184  * 'net rpc share' entrypoint.
1185  * @param argc  Standard main() style argc
1186  * @param argv  Standard main() style argv.  Initial components are already
1187  *              stripped
1188  **/
1189
1190 int net_rpc_share(int argc, const char **argv) 
1191 {
1192         struct functable func[] = {
1193                 {"add", rpc_share_add},
1194                 {"delete", rpc_share_delete},
1195                 {NULL, NULL}
1196         };
1197
1198         if (argc == 0)
1199                 return run_rpc_command(NULL, PIPE_SRVSVC, 0, 
1200                                        rpc_share_list_internals,
1201                                        argc, argv);
1202
1203         return net_run_function(argc, argv, func, rpc_share_usage);
1204 }
1205
1206 /****************************************************************************/
1207
1208 static int rpc_file_usage(int argc, const char **argv)
1209 {
1210         return net_help_file(argc, argv);
1211 }
1212
1213 /** 
1214  * Close a file on a remote RPC server
1215  *
1216  * All parameters are provided by the run_rpc_command function, except for
1217  * argc, argv which are passes through. 
1218  *
1219  * @param domain_sid The domain sid acquired from the remote server
1220  * @param cli A cli_state connected to the server.
1221  * @param mem_ctx Talloc context, destoyed on completion of the function.
1222  * @param argc  Standard main() style argc
1223  * @param argv  Standard main() style argv.  Initial components are already
1224  *              stripped
1225  *
1226  * @return Normal NTSTATUS return.
1227  **/
1228 static NTSTATUS 
1229 rpc_file_close_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1230                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
1231 {
1232         WERROR result;
1233         result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1234         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1235 }
1236
1237 /** 
1238  * Close a file on a remote RPC server
1239  *
1240  * @param argc  Standard main() style argc
1241  * @param argv  Standard main() style argv.  Initial components are already
1242  *              stripped
1243  *
1244  * @return A shell status integer (0 for success)
1245  **/
1246 static int rpc_file_close(int argc, const char **argv)
1247 {
1248         if (argc < 1) {
1249                 DEBUG(1, ("No fileid given on close\n"));
1250                 return(rpc_file_usage(argc, argv));
1251         }
1252
1253         return run_rpc_command(NULL, PIPE_SRVSVC, 0, 
1254                                rpc_file_close_internals,
1255                                argc, argv);
1256 }
1257
1258 /** 
1259  * Formatted print of open file info 
1260  *
1261  * @param info3  FILE_INFO_3 contents
1262  * @param str3   strings for FILE_INFO_3
1263  **/
1264
1265 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1266 {
1267         fstring user = "", path = "";
1268
1269         rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1270         rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1271
1272         d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1273                  info3->id, user, info3->perms, info3->num_locks, path);
1274 }
1275
1276 /** 
1277  * List open files on a remote RPC server
1278  *
1279  * All parameters are provided by the run_rpc_command function, except for
1280  * argc, argv which are passes through. 
1281  *
1282  * @param domain_sid The domain sid acquired from the remote server
1283  * @param cli A cli_state connected to the server.
1284  * @param mem_ctx Talloc context, destoyed on completion of the function.
1285  * @param argc  Standard main() style argc
1286  * @param argv  Standard main() style argv.  Initial components are already
1287  *              stripped
1288  *
1289  * @return Normal NTSTATUS return.
1290  **/
1291
1292 static NTSTATUS 
1293 rpc_file_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1294                         TALLOC_CTX *mem_ctx, int argc, const char **argv)
1295 {
1296         SRV_FILE_INFO_CTR ctr;
1297         WERROR result;
1298         ENUM_HND hnd;
1299         uint32 preferred_len = 0xffffffff, i;
1300         const char *username=NULL;
1301
1302         init_enum_hnd(&hnd, 0);
1303
1304         /* if argc > 0, must be user command */
1305         if (argc > 0)
1306                 username = smb_xstrdup(argv[0]);
1307                 
1308         result = cli_srvsvc_net_file_enum(
1309                 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1310
1311         if (!W_ERROR_IS_OK(result))
1312                 goto done;
1313
1314         /* Display results */
1315
1316         d_printf(
1317                  "\nEnumerating open files on remote server:\n\n"\
1318                  "\nFileId  Opened by            Perms  Locks  Path"\
1319                  "\n------  ---------            -----  -----  ---- \n");
1320         for (i = 0; i < ctr.num_entries; i++)
1321                 display_file_info_3(&ctr.file.info3[i].info_3, 
1322                                     &ctr.file.info3[i].info_3_str);
1323  done:
1324         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1325 }
1326
1327
1328 /** 
1329  * List files for a user on a remote RPC server
1330  *
1331  * @param argc  Standard main() style argc
1332  * @param argv  Standard main() style argv.  Initial components are already
1333  *              stripped
1334  *
1335  * @return A shell status integer (0 for success)
1336  **/
1337 static int rpc_file_user(int argc, const char **argv)
1338 {
1339         if (argc < 1) {
1340                 DEBUG(1, ("No username given\n"));
1341                 return(rpc_file_usage(argc, argv));
1342         }
1343
1344         return run_rpc_command(NULL, PIPE_SRVSVC, 0, 
1345                                rpc_file_list_internals,
1346                                argc, argv);
1347 }
1348
1349
1350 /** 
1351  * 'net rpc file' entrypoint.
1352  * @param argc  Standard main() style argc
1353  * @param argv  Standard main() style argv.  Initial components are already
1354  *              stripped
1355  **/
1356
1357 int net_rpc_file(int argc, const char **argv) 
1358 {
1359         struct functable func[] = {
1360                 {"close", rpc_file_close},
1361                 {"user", rpc_file_user},
1362 #if 0
1363                 {"info", rpc_file_info},
1364 #endif
1365                 {NULL, NULL}
1366         };
1367
1368         if (argc == 0)
1369                 return run_rpc_command(NULL, PIPE_SRVSVC, 0, 
1370                                        rpc_file_list_internals,
1371                                        argc, argv);
1372
1373         return net_run_function(argc, argv, func, rpc_file_usage);
1374 }
1375
1376 /****************************************************************************/
1377
1378
1379
1380 /** 
1381  * ABORT the shutdown of a remote RPC Server
1382  *
1383  * All parameters are provided by the run_rpc_command function, except for
1384  * argc, argv which are passed through. 
1385  *
1386  * @param domain_sid The domain sid aquired from the remote server
1387  * @param cli A cli_state connected to the server.
1388  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1389  * @param argc  Standard main() style argc
1390  * @param argv  Standard main() style argv.  Initial components are already
1391  *              stripped
1392  *
1393  * @return Normal NTSTATUS return.
1394  **/
1395
1396 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1397                                              int argc, const char **argv) 
1398 {
1399         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1400         
1401         result = cli_reg_abort_shutdown(cli, mem_ctx);
1402         
1403         if (NT_STATUS_IS_OK(result))
1404                 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1405         else
1406                 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1407         
1408         return result;
1409 }
1410
1411
1412 /** 
1413  * ABORT the Shut down of a remote RPC server
1414  *
1415  * @param argc  Standard main() style argc
1416  * @param argv  Standard main() style argv.  Initial components are already
1417  *              stripped
1418  *
1419  * @return A shell status integer (0 for success)
1420  **/
1421
1422 static int rpc_shutdown_abort(int argc, const char **argv) 
1423 {
1424         return run_rpc_command(NULL, PIPE_WINREG, 0, rpc_shutdown_abort_internals,
1425                                argc, argv);
1426 }
1427
1428 /** 
1429  * Shut down a remote RPC Server
1430  *
1431  * All parameters are provided by the run_rpc_command function, except for
1432  * argc, argv which are passes through. 
1433  *
1434  * @param domain_sid The domain sid aquired from the remote server
1435  * @param cli A cli_state connected to the server.
1436  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1437  * @param argc  Standard main() style argc
1438  * @param argc  Standard main() style argv.  Initial components are already
1439  *              stripped
1440  *
1441  * @return Normal NTSTATUS return.
1442  **/
1443
1444 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1445                                        int argc, const char **argv) 
1446 {
1447         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1448         char *msg = "This machine will be shutdown shortly";
1449         uint32 timeout = 20;
1450         uint16 flgs = 0;
1451         BOOL reboot = opt_reboot;
1452         BOOL force = opt_force;
1453 #if 0
1454         poptContext pc;
1455         int rc;
1456
1457         struct poptOption long_options[] = {
1458                 {"message",    'm', POPT_ARG_STRING, &msg},
1459                 {"timeout",    't', POPT_ARG_INT,    &timeout},
1460                 {"reboot",     'r', POPT_ARG_NONE,   &reboot},
1461                 {"force",      'f', POPT_ARG_NONE,   &force},
1462                 { 0, 0, 0, 0}
1463         };
1464
1465         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
1466                             POPT_CONTEXT_KEEP_FIRST);
1467
1468         rc = poptGetNextOpt(pc);
1469         
1470         if (rc < -1) {
1471                 /* an error occurred during option processing */
1472                 DEBUG(0, ("%s: %s\n",
1473                           poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1474                           poptStrerror(rc)));
1475                 return NT_STATUS_INVALID_PARAMETER;
1476         }
1477 #endif
1478         if (reboot) {
1479                 flgs |= REG_REBOOT_ON_SHUTDOWN;
1480         }
1481         if (force) {
1482                 flgs |= REG_FORCE_SHUTDOWN;
1483         }
1484         if (opt_comment) {
1485                 msg = opt_comment;
1486         }
1487         if (opt_timeout) {
1488                 timeout = opt_timeout;
1489         }
1490
1491         /* create an entry */
1492         result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, flgs);
1493
1494         if (NT_STATUS_IS_OK(result))
1495                 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1496         else
1497                 DEBUG(0,("Shutdown of remote machine failed!\n"));
1498
1499         return result;
1500 }
1501
1502 /** 
1503  * Shut down a remote RPC server
1504  *
1505  * @param argc  Standard main() style argc
1506  * @param argc  Standard main() style argv.  Initial components are already
1507  *              stripped
1508  *
1509  * @return A shell status integer (0 for success)
1510  **/
1511
1512 static int rpc_shutdown(int argc, const char **argv) 
1513 {
1514         return run_rpc_command(NULL, PIPE_WINREG, 0, rpc_shutdown_internals,
1515                                        argc, argv);
1516 }
1517
1518 /***************************************************************************
1519   NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1520   
1521  ***************************************************************************/
1522
1523 /**
1524  * Add interdomain trust account to the RPC server.
1525  * All parameters (except for argc and argv) are passed by run_rpc_command
1526  * function.
1527  *
1528  * @param domain_sid The domain sid acquired from the server
1529  * @param cli A cli_state connected to the server.
1530  * @param mem_ctx Talloc context, destoyed on completion of the function.
1531  * @param argc  Standard main() style argc
1532  * @param argc  Standard main() style argv.  Initial components are already
1533  *              stripped
1534  *
1535  * @return normal NTSTATUS return code
1536  */
1537
1538 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1539                                            int argc, const char **argv) {
1540
1541         POLICY_HND connect_pol, domain_pol, user_pol;
1542         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1543         char *acct_name;
1544         uint16 acb_info;
1545         uint32 unknown, user_rid;
1546
1547         if (argc != 1) {
1548                 d_printf("Usage: net rpc trustdom add <domain_name>\n");
1549                 return NT_STATUS_INVALID_PARAMETER;
1550         }
1551
1552         /* 
1553          * Make valid trusting domain account (ie. uppercased and with '$' appended)
1554          */
1555          
1556         if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1557                 return NT_STATUS_NO_MEMORY;
1558         }
1559
1560         strupper(acct_name);
1561
1562         /* Get samr policy handle */
1563         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1564                                   &connect_pol);
1565         if (!NT_STATUS_IS_OK(result)) {
1566                 goto done;
1567         }
1568         
1569         /* Get domain policy handle */
1570         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1571                                       MAXIMUM_ALLOWED_ACCESS,
1572                                       domain_sid, &domain_pol);
1573         if (!NT_STATUS_IS_OK(result)) {
1574                 goto done;
1575         }
1576
1577         /* Create trusting domain's account */
1578         acb_info = ACB_DOMTRUST;
1579         unknown = 0xe005000b; /* No idea what this is - a permission mask?
1580                                  mimir: yes, most probably it is */
1581
1582         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1583                                           acct_name, acb_info, unknown,
1584                                           &user_pol, &user_rid);
1585         if (!NT_STATUS_IS_OK(result)) {
1586                 goto done;
1587         }
1588
1589  done:
1590         SAFE_FREE(acct_name);
1591         return result;
1592 }
1593
1594 /**
1595  * Create interdomain trust account for a remote domain.
1596  *
1597  * @param argc standard argc
1598  * @param argv standard argv without initial components
1599  *
1600  * @return Integer status (0 means success)
1601  **/
1602
1603 static int rpc_trustdom_add(int argc, const char **argv)
1604 {
1605         return run_rpc_command(NULL, PIPE_SAMR, 0, rpc_trustdom_add_internals,
1606                                argc, argv);
1607 }
1608
1609
1610 /**
1611  * Delete interdomain trust account for a remote domain.
1612  *
1613  * @param argc standard argc
1614  * @param argv standard argv without initial components
1615  *
1616  * @return Integer status (0 means success)
1617  **/
1618  
1619 static int rpc_trustdom_del(int argc, const char **argv)
1620 {
1621         d_printf("Sorry, not yet implemented.\n");
1622         return -1;
1623 }
1624
1625  
1626 /**
1627  * Establish trust relationship to a trusting domain.
1628  * Interdomain account must already be created on remote PDC.
1629  *
1630  * @param argc standard argc
1631  * @param argv standard argv without initial components
1632  *
1633  * @return Integer status (0 means success)
1634  **/
1635
1636 extern char *opt_user_name;
1637 extern char *opt_password;
1638 extern char *opt_workgroup;
1639
1640 static int rpc_trustdom_establish(int argc, const char **argv)
1641 {
1642         struct cli_state *cli;
1643         struct in_addr server_ip;
1644         POLICY_HND connect_hnd;
1645         TALLOC_CTX *mem_ctx;
1646         NTSTATUS nt_status;
1647         DOM_SID domain_sid;
1648         WKS_INFO_100 wks_info;
1649         
1650         char* domain_name;
1651         char* acct_name;
1652         fstring pdc_name;
1653
1654         /*
1655          * Connect to \\server\ipc$ as 'our domain' account with password
1656          */
1657
1658         if (argc != 1) {
1659                 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1660                 return -1;
1661         }
1662
1663         domain_name = smb_xstrdup(argv[0]);
1664         strupper(domain_name);
1665         
1666         /*
1667          * opt_workgroup will be used by connection functions further,
1668          * hence it should be set to remote domain name instead of ours
1669          */
1670         if (opt_workgroup) {
1671                 SAFE_FREE(opt_workgroup);
1672                 opt_workgroup = smb_xstrdup(domain_name);
1673         };
1674         
1675         asprintf(&acct_name, "%s$", lp_workgroup());
1676         strupper(acct_name);
1677         
1678         opt_user_name = acct_name;
1679
1680         /* find the domain controller */
1681         if (!net_find_dc(&server_ip, pdc_name, domain_name)) {
1682                 DEBUG(0, ("Coulnd find domain controller for domain %s\n", domain_name));
1683                 return -1;
1684         }
1685
1686         /* connect to ipc$ as username/password */
1687         nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1688         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1689
1690                 /* Is it trusting domain account for sure ? */
1691                 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1692                         nt_errstr(nt_status)));
1693                 return -1;
1694         }
1695         
1696         /*
1697          * Connect to \\server\ipc$ again (this time anonymously)
1698          */
1699         
1700         nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1701         
1702         if (NT_STATUS_IS_ERR(nt_status)) {
1703                 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1704                         domain_name, nt_errstr(nt_status)));
1705         }
1706
1707         /*
1708          * Use NetServerEnum2 to make sure we're talking to a proper server
1709          */
1710          
1711         if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
1712                 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
1713                          for domain %s\n", domain_name));
1714         }
1715          
1716         /*
1717          * Call WksQueryInfo to check remote server's capabilities
1718          * note: It is now used only to get unicode domain name
1719          */
1720         
1721         if (!cli_nt_session_open(cli, PIPE_WKSSVC)) {
1722                 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
1723                 return -1;
1724         }
1725
1726         if (!(mem_ctx = talloc_init_named("establishing trust relationship to domain %s",
1727                         domain_name))) {
1728                 DEBUG(0, ("talloc_init() failed\n"));
1729                 cli_shutdown(cli);
1730                 return -1;
1731         }
1732         
1733         nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
1734         
1735         if (NT_STATUS_IS_ERR(nt_status)) {
1736                 DEBUG(0, ("WksQueryInfo call failed.\n"));
1737                 return -1;
1738         }
1739
1740         if (cli->nt_pipe_fnum)
1741                 cli_nt_session_close(cli);
1742
1743
1744         /*
1745          * Call LsaOpenPolicy and LsaQueryInfo
1746          */
1747          
1748         if (!(mem_ctx = talloc_init())) {
1749                 DEBUG(0, ("talloc_init() failed\n"));
1750                 cli_shutdown(cli);
1751                 return -1;
1752         }
1753
1754         if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
1755                 DEBUG(0, ("Could not initialise lsa pipe\n"));
1756                 cli_shutdown(cli);
1757                 return -1;
1758         }
1759
1760         nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1761                                          &connect_hnd);
1762         if (NT_STATUS_IS_ERR(nt_status)) {
1763                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1764                         nt_errstr(nt_status)));
1765                 return -1;
1766         }
1767
1768         /* Querying info level 5 */
1769         
1770         nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1771                                               5 /* info level */, domain_name,
1772                                               &domain_sid);
1773         if (NT_STATUS_IS_ERR(nt_status)) {
1774                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1775                         nt_errstr(nt_status)));
1776                 return -1;
1777         }
1778
1779
1780
1781
1782         /* There should be actually query info level 3 (following nt serv behaviour),
1783            but I still don't know if it's _really_ necessary */
1784                         
1785         /*
1786          * Store the password in secrets db
1787          */
1788
1789         if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
1790                                                    wks_info.uni_lan_grp.uni_str_len, opt_password,
1791                                                    domain_sid)) {
1792                 DEBUG(0, ("Storing password for trusted domain failed.\n"));
1793                 return -1;
1794         }
1795         
1796         /*
1797          * Close the pipes and clean up
1798          */
1799          
1800         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1801         if (NT_STATUS_IS_ERR(nt_status)) {
1802                 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
1803                         nt_errstr(nt_status)));
1804                 return -1;
1805         }
1806
1807         if (cli->nt_pipe_fnum)
1808                 cli_nt_session_close(cli);
1809          
1810         talloc_destroy(mem_ctx);
1811          
1812         DEBUG(0, ("Success!\n"));
1813         return 0;
1814 }
1815
1816 /**
1817  * Revoke trust relationship to the remote domain
1818  *
1819  * @param argc standard argc
1820  * @param argv standard argv without initial components
1821  *
1822  * @return Integer status (0 means success)
1823  **/
1824
1825 static int rpc_trustdom_revoke(int argc, const char **argv)
1826 {
1827         char* domain_name;
1828
1829         if (argc < 1) return -1;
1830         
1831         /* generate upper cased domain name */
1832         domain_name = smb_xstrdup(argv[0]);
1833         strupper(domain_name);
1834
1835         /* delete password of the trust */
1836         if (!trusted_domain_password_delete(domain_name)) {
1837                 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
1838                           domain_name));
1839                 return -1;
1840         };
1841         
1842         return 0;
1843 }
1844
1845 /**
1846  * Usage for 'net rpc trustdom' command
1847  *
1848  * @param argc standard argc
1849  * @param argv standard argv without inital components
1850  *
1851  * @return Integer status returned to shell
1852  **/
1853  
1854 static int rpc_trustdom_usage(int argc, const char **argv)
1855 {
1856         d_printf("  net rpc trustdom add \t\t add trusting domain's account\n");
1857         d_printf("  net rpc trustdom del \t\t delete trusting domain's account\n");
1858         d_printf("  net rpc trustdom establish \t establish relationship to trusted domain\n");
1859         d_printf("  net rpc trustdom revoke \t abandon relationship to trusted domain\n");
1860         d_printf("  net rpc trustdom list \t show current interdomain trust relationships\n");
1861         return -1;
1862 }
1863
1864
1865 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1866                               int argc, const char **argv)
1867 {
1868         fstring str_sid;
1869         sid_to_string(str_sid, domain_sid);
1870         d_printf("%s\n", str_sid);
1871         return NT_STATUS_OK;
1872 };
1873
1874
1875 extern char* opt_workgroup;
1876 extern char* opt_target_worgroup;
1877 extern char* opt_host;
1878 extern char* opt_password;
1879
1880 static int rpc_trustdom_list(int argc, const char **argv)
1881 {
1882         /* common variables */
1883         TALLOC_CTX* mem_ctx;
1884         struct cli_state *cli, *remote_cli;
1885         NTSTATUS nt_status;
1886         char *domain_name = NULL;
1887         DOM_SID queried_dom_sid;
1888         fstring ascii_sid, padding;
1889         int ascii_dom_name_len;
1890         POLICY_HND connect_hnd;
1891         
1892         /* trusted domains listing variables */
1893         int enum_ctx = 0;
1894         int num_domains, i, pad_len, col_len = 20;
1895         DOM_SID *domain_sids;
1896         char **trusted_dom_names;
1897         fstring pdc_name;
1898         
1899         /* trusting domains listing variables */
1900         POLICY_HND domain_hnd;
1901         char **trusting_dom_names;
1902         uint32 *trusting_dom_rids;
1903         
1904         /*
1905          * Listing trusted domains (stored in secrets.tdb, if local)
1906          */
1907
1908         mem_ctx = talloc_init_named("trust relationships listing");
1909
1910         /*
1911          * set domain and pdc name to local samba server (default)
1912          * or to remote one given in command line
1913          */
1914         strupper(opt_workgroup);
1915         if (strcmp(opt_workgroup, lp_workgroup())) {
1916                 domain_name = opt_workgroup;
1917                 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
1918                 opt_target_workgroup = opt_workgroup;
1919         } else {
1920                 safe_strcpy(pdc_name, global_myname, FSTRING_LEN);
1921                 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
1922                 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
1923                 opt_target_workgroup = domain_name;
1924         };
1925
1926         /* open \PIPE\lsarpc and open policy handle */
1927         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
1928                 DEBUG(0, ("Couldn't connect to domain controller\n"));
1929                 return -1;
1930         };
1931
1932         if (!cli_nt_session_open(cli, PIPE_LSARPC)) {
1933                 DEBUG(0, ("Could not initialise lsa pipe\n"));
1934                 return -1;
1935         };
1936
1937         nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1938                                         &connect_hnd);
1939         if (NT_STATUS_IS_ERR(nt_status)) {
1940                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1941                         nt_errstr(nt_status)));
1942                 return -1;
1943         };
1944         
1945         /* query info level 5 to obtain sid of a domain being queried */
1946         nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1947                                         5 /* info level */, domain_name, &queried_dom_sid);
1948         if (NT_STATUS_IS_ERR(nt_status)) {
1949                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1950                         nt_errstr(nt_status)));
1951                 return -1;
1952         }
1953                 
1954         /*
1955          * Keep calling LsaEnumTrustdom over opened pipe until
1956          * the end of enumeration is reached
1957          */
1958          
1959         d_printf("Trusted domains list:\n\n");
1960
1961         do {
1962                 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
1963                                                    &num_domains,
1964                                                    &trusted_dom_names, &domain_sids);
1965                 
1966                 if (NT_STATUS_IS_ERR(nt_status)) {
1967                         DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
1968                                 nt_errstr(nt_status)));
1969                         return -1;
1970                 };
1971                 
1972                 for (i = 0; i < num_domains; i++) {
1973                         /* convert sid into ascii string */
1974                         sid_to_string(ascii_sid, &(domain_sids[i]));
1975                 
1976                         /* calculate padding space for d_printf to look nicer */
1977                         pad_len = col_len - strlen(trusted_dom_names[i]);
1978                         padding[pad_len] = 0;
1979                         do padding[--pad_len] = ' '; while (pad_len);
1980                         
1981                         d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
1982                 };
1983                 
1984                 /*
1985                  * in case of no trusted domains say something rather
1986                  * than just display blank line
1987                  */
1988                 if (!num_domains) d_printf("none\n");
1989
1990         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
1991
1992         /* close this connection before doing next one */
1993         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1994         if (NT_STATUS_IS_ERR(nt_status)) {
1995                 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
1996                         nt_errstr(nt_status)));
1997                 return -1;
1998         };
1999         
2000         cli_nt_session_close(cli);
2001
2002         /*
2003          * Listing trusting domains (stored in passdb backend, if local)
2004          */
2005         
2006         d_printf("\nTrusting domains list:\n\n");
2007
2008         /*
2009          * Open \PIPE\samr and get needed policy handles
2010          */
2011         if (!cli_nt_session_open(cli, PIPE_SAMR)) {
2012                 DEBUG(0, ("Could not initialise samr pipe\n"));
2013                 return -1;
2014         };
2015         
2016         /* SamrConnect */
2017         nt_status = cli_samr_connect(cli, mem_ctx, SAMR_ACCESS_OPEN_DOMAIN,
2018                                                                  &connect_hnd);
2019         if (!NT_STATUS_IS_OK(nt_status)) {
2020                 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2021                         nt_errstr(nt_status)));
2022                 return -1;
2023         };
2024         
2025         /* SamrOpenDomain - we have to open domain policy handle in order to be
2026            able to enumerate accounts*/
2027         nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2028                                                                          DOMAIN_ACCESS_ENUM_ACCOUNTS,
2029                                                                          &queried_dom_sid, &domain_hnd);                                                                         
2030         if (!NT_STATUS_IS_OK(nt_status)) {
2031                 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2032                         nt_errstr(nt_status)));
2033                 return -1;
2034         };
2035         
2036         /*
2037          * perform actual enumeration
2038          */
2039          
2040         enum_ctx = 0;   /* reset enumeration context from last enumeration */
2041         do {
2042                         
2043                 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2044                                                     &enum_ctx, ACB_DOMTRUST, 0xffff,
2045                                                     &trusting_dom_names, &trusting_dom_rids,
2046                                                     &num_domains);
2047                 if (NT_STATUS_IS_ERR(nt_status)) {
2048                         DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2049                                 nt_errstr(nt_status)));
2050                         return -1;
2051                 };
2052                 
2053                 for (i = 0; i < num_domains; i++) {
2054
2055                         /*
2056                          * get each single domain's sid (do we _really_ need this ?):
2057                          *  1) connect to domain's pdc
2058                          *  2) query the pdc for domain's sid
2059                          */
2060
2061                         /* get rid of '$' tail */
2062                         ascii_dom_name_len = strlen(trusting_dom_names[i]);
2063                         if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2064                                 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2065                         
2066                         /* calculate padding space for d_printf to look nicer */
2067                         pad_len = col_len - strlen(trusting_dom_names[i]);
2068                         padding[pad_len] = 0;
2069                         do padding[--pad_len] = ' '; while (pad_len);
2070
2071                         /* set opt_* variables to remote domain */
2072                         strupper(trusting_dom_names[i]);
2073                         opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2074                         if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
2075                         opt_target_workgroup = opt_workgroup;
2076                         
2077                         d_printf("%s%s", trusting_dom_names[i], padding);
2078                         
2079                         /* connect to remote domain controller */
2080                         remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2081                         if (remote_cli) {                       
2082                                 /* query for domain's sid */
2083                                 if (run_rpc_command(remote_cli, PIPE_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2084                                         d_printf("couldn't get domain's sid\n");
2085
2086                                 cli_shutdown(remote_cli);
2087                         
2088                         } else {
2089                                 d_printf("domain controller is not responding\n");
2090                         };
2091                 };
2092                 
2093                 if (!num_domains) d_printf("none\n");
2094                 
2095         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2096
2097         /* close opened samr and domain policy handles */
2098         nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2099         if (!NT_STATUS_IS_OK(nt_status)) {
2100                 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2101         };
2102         
2103         nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2104         if (!NT_STATUS_IS_OK(nt_status)) {
2105                 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2106         };
2107         
2108         /* close samr pipe and connection to IPC$ */
2109         cli_nt_session_close(cli);
2110         cli_shutdown(cli);
2111
2112         talloc_destroy(mem_ctx);         
2113         return 0;
2114 }
2115
2116 /**
2117  * Entrypoint for 'net rpc trustdom' code
2118  *
2119  * @param argc standard argc
2120  * @param argv standard argv without initial components
2121  *
2122  * @return Integer status (0 means success)
2123  */
2124
2125 static int rpc_trustdom(int argc, const char **argv)
2126 {
2127         struct functable func[] = {
2128                 {"add", rpc_trustdom_add},
2129                 {"del", rpc_trustdom_del},
2130                 {"establish", rpc_trustdom_establish},
2131                 {"revoke", rpc_trustdom_revoke},
2132                 {"help", rpc_trustdom_usage},
2133                 {"list", rpc_trustdom_list},
2134                 {NULL, NULL}
2135         };
2136
2137         if (argc == 0) {
2138                 rpc_trustdom_usage(argc, argv);
2139                 return -1;
2140         }
2141
2142         return (net_run_function(argc, argv, func, rpc_user_usage));
2143 }
2144
2145 /**
2146  * Check if a server will take rpc commands
2147  * @param flags Type of server to connect to (PDC, DMB, localhost)
2148  *              if the host is not explicitly specified
2149  * @return  BOOL (true means rpc supported)
2150  */
2151 BOOL net_rpc_check(unsigned flags)
2152 {
2153         struct cli_state cli;
2154         BOOL ret = False;
2155         struct in_addr server_ip;
2156         char *server_name = NULL;
2157
2158         /* flags (i.e. server type) may depend on command */
2159         if (!net_find_server(flags, &server_ip, &server_name))
2160                 return False;
2161
2162         ZERO_STRUCT(cli);
2163         if (cli_initialise(&cli) == False)
2164                 return False;
2165
2166         if (!cli_connect(&cli, server_name, &server_ip))
2167                 goto done;
2168         if (!attempt_netbios_session_request(&cli, global_myname, 
2169                                              server_name, &server_ip))
2170                 goto done;
2171         if (!cli_negprot(&cli))
2172                 goto done;
2173         if (cli.protocol < PROTOCOL_NT1)
2174                 goto done;
2175
2176         ret = True;
2177  done:
2178         cli_shutdown(&cli);
2179         return ret;
2180 }
2181
2182
2183 /****************************************************************************/
2184
2185
2186 /** 
2187  * Basic usage function for 'net rpc'
2188  * @param argc  Standard main() style argc
2189  * @param argv  Standard main() style argv.  Initial components are already
2190  *              stripped
2191  **/
2192
2193 int net_rpc_usage(int argc, const char **argv) 
2194 {
2195         d_printf("  net rpc info \t\t\tshow basic info about a domain \n");
2196         d_printf("  net rpc join \t\t\tto join a domain \n");
2197         d_printf("  net rpc testjoin \t\ttests that a join is valid\n");
2198         d_printf("  net rpc user \t\t\tto add, delete and list users\n");
2199         d_printf("  net rpc group \t\tto list groups\n");
2200         d_printf("  net rpc share \t\tto add, delete, and list shares\n");
2201         d_printf("  net rpc file \t\t\tto list open files\n");
2202         d_printf("  net rpc changetrustpw \tto change the trust account password\n");
2203         d_printf("  net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2204         d_printf("  net rpc trustdom \t\tto create trusting domain's account\n"
2205                  "\t\t\t\t\tor establish trust\n");
2206         d_printf("  net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2207         d_printf("  net rpc shutdown \t\tto shutdown a remote server\n");
2208         d_printf("\n");
2209         d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2210         d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2211         d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2212         d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2213         d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2214         return -1;
2215 }
2216
2217
2218 /**
2219  * Help function for 'net rpc'.  Calls command specific help if requested
2220  * or displays usage of net rpc
2221  * @param argc  Standard main() style argc
2222  * @param argv  Standard main() style argv.  Initial components are already
2223  *              stripped
2224  **/
2225
2226 int net_rpc_help(int argc, const char **argv)
2227 {
2228         struct functable func[] = {
2229                 {"join", rpc_join_usage},
2230                 {"user", rpc_user_usage},
2231                 {"group", rpc_group_usage},
2232                 {"share", rpc_share_usage},
2233                 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2234                 {"trustdom", rpc_trustdom_usage},
2235                 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2236                 /*{"shutdown", rpc_shutdown_usage}, */
2237                 {NULL, NULL}
2238         };
2239
2240         if (argc == 0) {
2241                 net_rpc_usage(argc, argv);
2242                 return -1;
2243         }
2244
2245         return (net_run_function(argc, argv, func, rpc_user_usage));
2246 }
2247
2248
2249 /** 
2250  * 'net rpc' entrypoint.
2251  * @param argc  Standard main() style argc
2252  * @param argv  Standard main() style argv.  Initial components are already
2253  *              stripped
2254  **/
2255
2256 int net_rpc(int argc, const char **argv)
2257 {
2258         struct functable func[] = {
2259                 {"info", net_rpc_info},
2260                 {"join", net_rpc_join},
2261                 {"testjoin", net_rpc_testjoin},
2262                 {"user", net_rpc_user},
2263                 {"group", net_rpc_group},
2264                 {"share", net_rpc_share},
2265                 {"file", net_rpc_file},
2266                 {"changetrustpw", rpc_changetrustpw},
2267                 {"trustdom", rpc_trustdom},
2268                 {"abortshutdown", rpc_shutdown_abort},
2269                 {"shutdown", rpc_shutdown},
2270                 {"samdump", rpc_samdump},
2271                 {"vampire", rpc_vampire},
2272                 {"getsid", net_rpc_getsid},
2273                 {"help", net_rpc_help},
2274                 {NULL, NULL}
2275         };
2276         return net_run_function(argc, argv, func, net_rpc_usage);
2277 }