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