298e8ff6690772bf9431bfce80da1c93e791d624
[samba.git] / source / 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 over, initshutdown pipe
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, 
1480                                              struct cli_state *cli, 
1481                                              TALLOC_CTX *mem_ctx, 
1482                                              int argc, const char **argv) 
1483 {
1484         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1485         
1486         result = cli_shutdown_abort(cli, mem_ctx);
1487         
1488         if (NT_STATUS_IS_OK(result))
1489                 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1490         else
1491                 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1492         
1493         return result;
1494 }
1495
1496
1497 /** 
1498  * ABORT the shutdown of a remote RPC Server,  over winreg pipe
1499  *
1500  * All parameters are provided by the run_rpc_command function, except for
1501  * argc, argv which are passed through. 
1502  *
1503  * @param domain_sid The domain sid aquired from the remote server
1504  * @param cli A cli_state connected to the server.
1505  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1506  * @param argc  Standard main() style argc
1507  * @param argv  Standard main() style argv.  Initial components are already
1508  *              stripped
1509  *
1510  * @return Normal NTSTATUS return.
1511  **/
1512
1513 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid, 
1514                                                  struct cli_state *cli, 
1515                                                  TALLOC_CTX *mem_ctx, 
1516                                                  int argc, const char **argv) 
1517 {
1518         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1519         
1520         result = cli_reg_abort_shutdown(cli, mem_ctx);
1521         
1522         if (NT_STATUS_IS_OK(result))
1523                 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1524         else
1525                 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1526         
1527         return result;
1528 }
1529
1530 /** 
1531  * ABORT the Shut down of a remote RPC server
1532  *
1533  * @param argc  Standard main() style argc
1534  * @param argv  Standard main() style argv.  Initial components are already
1535  *              stripped
1536  *
1537  * @return A shell status integer (0 for success)
1538  **/
1539
1540 static int rpc_shutdown_abort(int argc, const char **argv) 
1541 {
1542         int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0, 
1543                                  rpc_shutdown_abort_internals,
1544                                  argc, argv);
1545
1546         if (rc == 0)
1547                 return rc;
1548
1549         DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
1550
1551         return run_rpc_command(NULL, PI_WINREG, 0, 
1552                                rpc_reg_shutdown_abort_internals,
1553                                argc, argv);
1554 }
1555
1556 /** 
1557  * Shut down a remote RPC Server
1558  *
1559  * All parameters are provided by the run_rpc_command function, except for
1560  * argc, argv which are passes through. 
1561  *
1562  * @param domain_sid The domain sid aquired from the remote server
1563  * @param cli A cli_state connected to the server.
1564  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1565  * @param argc  Standard main() style argc
1566  * @param argc  Standard main() style argv.  Initial components are already
1567  *              stripped
1568  *
1569  * @return Normal NTSTATUS return.
1570  **/
1571
1572 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1573                                        int argc, const char **argv) 
1574 {
1575         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1576         const char *msg = "This machine will be shutdown shortly";
1577         uint32 timeout = 20;
1578 #if 0
1579         poptContext pc;
1580         int rc;
1581
1582         struct poptOption long_options[] = {
1583                 {"message",    'm', POPT_ARG_STRING, &msg},
1584                 {"timeout",    't', POPT_ARG_INT,    &timeout},
1585                 {"reboot",     'r', POPT_ARG_NONE,   &reboot},
1586                 {"force",      'f', POPT_ARG_NONE,   &force},
1587                 { 0, 0, 0, 0}
1588         };
1589
1590         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
1591                             POPT_CONTEXT_KEEP_FIRST);
1592
1593         rc = poptGetNextOpt(pc);
1594         
1595         if (rc < -1) {
1596                 /* an error occurred during option processing */
1597                 DEBUG(0, ("%s: %s\n",
1598                           poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1599                           poptStrerror(rc)));
1600                 return NT_STATUS_INVALID_PARAMETER;
1601         }
1602 #endif
1603         if (opt_comment) {
1604                 msg = opt_comment;
1605         }
1606         if (opt_timeout) {
1607                 timeout = opt_timeout;
1608         }
1609
1610         /* create an entry */
1611         result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
1612
1613         if (NT_STATUS_IS_OK(result))
1614                 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1615         else
1616                 DEBUG(0,("Shutdown of remote machine failed!\n"));
1617
1618         return result;
1619 }
1620
1621 /** 
1622  * Shut down a remote RPC server
1623  *
1624  * @param argc  Standard main() style argc
1625  * @param argc  Standard main() style argv.  Initial components are already
1626  *              stripped
1627  *
1628  * @return A shell status integer (0 for success)
1629  **/
1630
1631 static int rpc_shutdown(int argc, const char **argv) 
1632 {
1633         return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
1634                                        argc, argv);
1635 }
1636
1637 /***************************************************************************
1638   NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1639   
1640  ***************************************************************************/
1641
1642 /**
1643  * Add interdomain trust account to the RPC server.
1644  * All parameters (except for argc and argv) are passed by run_rpc_command
1645  * function.
1646  *
1647  * @param domain_sid The domain sid acquired from the server
1648  * @param cli A cli_state connected to the server.
1649  * @param mem_ctx Talloc context, destoyed on completion of the function.
1650  * @param argc  Standard main() style argc
1651  * @param argc  Standard main() style argv.  Initial components are already
1652  *              stripped
1653  *
1654  * @return normal NTSTATUS return code
1655  */
1656
1657 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1658                                            int argc, const char **argv) {
1659
1660         POLICY_HND connect_pol, domain_pol, user_pol;
1661         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1662         char *acct_name;
1663         uint16 acb_info;
1664         uint32 unknown, user_rid;
1665
1666         if (argc != 2) {
1667                 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
1668                 return NT_STATUS_INVALID_PARAMETER;
1669         }
1670
1671         /* 
1672          * Make valid trusting domain account (ie. uppercased and with '$' appended)
1673          */
1674          
1675         if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1676                 return NT_STATUS_NO_MEMORY;
1677         }
1678
1679         strupper_m(acct_name);
1680
1681         /* Get samr policy handle */
1682         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1683                                   &connect_pol);
1684         if (!NT_STATUS_IS_OK(result)) {
1685                 goto done;
1686         }
1687         
1688         /* Get domain policy handle */
1689         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1690                                       MAXIMUM_ALLOWED_ACCESS,
1691                                       domain_sid, &domain_pol);
1692         if (!NT_STATUS_IS_OK(result)) {
1693                 goto done;
1694         }
1695
1696         /* Create trusting domain's account */
1697         acb_info = ACB_DOMTRUST;
1698         unknown = 0xe00500b0; /* No idea what this is - a permission mask?
1699                                  mimir: yes, most probably it is */
1700
1701         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1702                                           acct_name, acb_info, unknown,
1703                                           &user_pol, &user_rid);
1704         if (!NT_STATUS_IS_OK(result)) {
1705                 goto done;
1706         }
1707
1708         {
1709                 SAM_USERINFO_CTR ctr;
1710                 SAM_USER_INFO_24 p24;
1711                 fstring ucs2_trust_password;
1712                 int ucs2_pw_len;
1713                 uchar pwbuf[516];
1714
1715                 ucs2_pw_len = push_ucs2(NULL, ucs2_trust_password, argv[1],
1716                                         sizeof(ucs2_trust_password), 0);
1717
1718                 encode_pw_buffer((char *)pwbuf, ucs2_trust_password,
1719                                  ucs2_pw_len);
1720
1721                 ZERO_STRUCT(ctr);
1722                 ZERO_STRUCT(p24);
1723
1724                 init_sam_user_info24(&p24, (char *)pwbuf, 24);
1725
1726                 ctr.switch_value = 24;
1727                 ctr.info.id24 = &p24;
1728
1729                 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
1730                                                cli->user_session_key, &ctr);
1731
1732                 if (!NT_STATUS_IS_OK(result)) {
1733                         DEBUG(0,("Could not set trust account password: %s\n",
1734                                  nt_errstr(result)));
1735                         goto done;
1736                 }
1737         }
1738
1739  done:
1740         SAFE_FREE(acct_name);
1741         return result;
1742 }
1743
1744 /**
1745  * Create interdomain trust account for a remote domain.
1746  *
1747  * @param argc standard argc
1748  * @param argv standard argv without initial components
1749  *
1750  * @return Integer status (0 means success)
1751  **/
1752
1753 static int rpc_trustdom_add(int argc, const char **argv)
1754 {
1755         if (argc > 0) {
1756                 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
1757                                        argc, argv);
1758         } else {
1759                 d_printf("Usage: net rpc trustdom add <domain>\n");
1760                 return -1;
1761         }
1762 }
1763
1764
1765 /**
1766  * Delete interdomain trust account for a remote domain.
1767  *
1768  * @param argc standard argc
1769  * @param argv standard argv without initial components
1770  *
1771  * @return Integer status (0 means success)
1772  **/
1773  
1774 static int rpc_trustdom_del(int argc, const char **argv)
1775 {
1776         d_printf("Sorry, not yet implemented.\n");
1777         d_printf("Use 'smbpasswd -x -i' instead.\n");
1778         return -1;
1779 }
1780
1781  
1782 /**
1783  * Establish trust relationship to a trusting domain.
1784  * Interdomain account must already be created on remote PDC.
1785  *
1786  * @param argc standard argc
1787  * @param argv standard argv without initial components
1788  *
1789  * @return Integer status (0 means success)
1790  **/
1791
1792 static int rpc_trustdom_establish(int argc, const char **argv)
1793 {
1794         struct cli_state *cli;
1795         struct in_addr server_ip;
1796         POLICY_HND connect_hnd;
1797         TALLOC_CTX *mem_ctx;
1798         NTSTATUS nt_status;
1799         DOM_SID domain_sid;
1800         WKS_INFO_100 wks_info;
1801         
1802         char* domain_name;
1803         char* acct_name;
1804         fstring pdc_name;
1805
1806         /*
1807          * Connect to \\server\ipc$ as 'our domain' account with password
1808          */
1809
1810         if (argc != 1) {
1811                 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1812                 return -1;
1813         }
1814
1815         domain_name = smb_xstrdup(argv[0]);
1816         strupper_m(domain_name);
1817
1818         /* account name used at first is our domain's name with '$' */
1819         asprintf(&acct_name, "%s$", lp_workgroup());
1820         strupper_m(acct_name);
1821         
1822         /*
1823          * opt_workgroup will be used by connection functions further,
1824          * hence it should be set to remote domain name instead of ours
1825          */
1826         if (opt_workgroup) {
1827                 opt_workgroup = smb_xstrdup(domain_name);
1828         };
1829         
1830         opt_user_name = acct_name;
1831
1832         /* find the domain controller */
1833         if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
1834                 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
1835                 return -1;
1836         }
1837
1838         /* connect to ipc$ as username/password */
1839         nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1840         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1841
1842                 /* Is it trusting domain account for sure ? */
1843                 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1844                         nt_errstr(nt_status)));
1845                 return -1;
1846         }
1847         
1848         /*
1849          * Connect to \\server\ipc$ again (this time anonymously)
1850          */
1851         
1852         nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1853         
1854         if (NT_STATUS_IS_ERR(nt_status)) {
1855                 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1856                         domain_name, nt_errstr(nt_status)));
1857         }
1858
1859         /*
1860          * Use NetServerEnum2 to make sure we're talking to a proper server
1861          */
1862          
1863         if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
1864                 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
1865                          for domain %s\n", domain_name));
1866         }
1867          
1868         /*
1869          * Call WksQueryInfo to check remote server's capabilities
1870          * note: It is now used only to get unicode domain name
1871          */
1872         
1873         if (!cli_nt_session_open(cli, PI_WKSSVC)) {
1874                 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
1875                 return -1;
1876         }
1877
1878         if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
1879                         domain_name))) {
1880                 DEBUG(0, ("talloc_init() failed\n"));
1881                 cli_shutdown(cli);
1882                 return -1;
1883         }
1884         
1885         nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
1886         
1887         if (NT_STATUS_IS_ERR(nt_status)) {
1888                 DEBUG(0, ("WksQueryInfo call failed.\n"));
1889                 return -1;
1890         }
1891
1892         if (cli->nt_pipe_fnum)
1893                 cli_nt_session_close(cli);
1894
1895
1896         /*
1897          * Call LsaOpenPolicy and LsaQueryInfo
1898          */
1899          
1900         if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
1901                 DEBUG(0, ("talloc_init() failed\n"));
1902                 cli_shutdown(cli);
1903                 return -1;
1904         }
1905
1906         if (!cli_nt_session_open(cli, PI_LSARPC)) {
1907                 DEBUG(0, ("Could not initialise lsa pipe\n"));
1908                 cli_shutdown(cli);
1909                 return -1;
1910         }
1911
1912         nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1913                                          &connect_hnd);
1914         if (NT_STATUS_IS_ERR(nt_status)) {
1915                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1916                         nt_errstr(nt_status)));
1917                 return -1;
1918         }
1919
1920         /* Querying info level 5 */
1921         
1922         nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1923                                               5 /* info level */, domain_name,
1924                                               &domain_sid);
1925         if (NT_STATUS_IS_ERR(nt_status)) {
1926                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1927                         nt_errstr(nt_status)));
1928                 return -1;
1929         }
1930
1931
1932
1933
1934         /* There should be actually query info level 3 (following nt serv behaviour),
1935            but I still don't know if it's _really_ necessary */
1936                         
1937         /*
1938          * Store the password in secrets db
1939          */
1940
1941         if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
1942                                                    wks_info.uni_lan_grp.uni_str_len, opt_password,
1943                                                    domain_sid)) {
1944                 DEBUG(0, ("Storing password for trusted domain failed.\n"));
1945                 return -1;
1946         }
1947         
1948         /*
1949          * Close the pipes and clean up
1950          */
1951          
1952         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1953         if (NT_STATUS_IS_ERR(nt_status)) {
1954                 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
1955                         nt_errstr(nt_status)));
1956                 return -1;
1957         }
1958
1959         if (cli->nt_pipe_fnum)
1960                 cli_nt_session_close(cli);
1961          
1962         talloc_destroy(mem_ctx);
1963          
1964         DEBUG(0, ("Success!\n"));
1965         return 0;
1966 }
1967
1968 /**
1969  * Revoke trust relationship to the remote domain
1970  *
1971  * @param argc standard argc
1972  * @param argv standard argv without initial components
1973  *
1974  * @return Integer status (0 means success)
1975  **/
1976
1977 static int rpc_trustdom_revoke(int argc, const char **argv)
1978 {
1979         char* domain_name;
1980
1981         if (argc < 1) return -1;
1982         
1983         /* generate upper cased domain name */
1984         domain_name = smb_xstrdup(argv[0]);
1985         strupper_m(domain_name);
1986
1987         /* delete password of the trust */
1988         if (!trusted_domain_password_delete(domain_name)) {
1989                 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
1990                           domain_name));
1991                 return -1;
1992         };
1993         
1994         return 0;
1995 }
1996
1997 /**
1998  * Usage for 'net rpc trustdom' command
1999  *
2000  * @param argc standard argc
2001  * @param argv standard argv without inital components
2002  *
2003  * @return Integer status returned to shell
2004  **/
2005  
2006 static int rpc_trustdom_usage(int argc, const char **argv)
2007 {
2008         d_printf("  net rpc trustdom add \t\t add trusting domain's account\n");
2009         d_printf("  net rpc trustdom del \t\t delete trusting domain's account\n");
2010         d_printf("  net rpc trustdom establish \t establish relationship to trusted domain\n");
2011         d_printf("  net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2012         d_printf("  net rpc trustdom list \t show current interdomain trust relationships\n");
2013         return -1;
2014 }
2015
2016
2017 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
2018                               int argc, const char **argv)
2019 {
2020         fstring str_sid;
2021         sid_to_string(str_sid, domain_sid);
2022         d_printf("%s\n", str_sid);
2023         return NT_STATUS_OK;
2024 }
2025
2026
2027 static int rpc_trustdom_list(int argc, const char **argv)
2028 {
2029         /* common variables */
2030         TALLOC_CTX* mem_ctx;
2031         struct cli_state *cli, *remote_cli;
2032         NTSTATUS nt_status;
2033         const char *domain_name = NULL;
2034         DOM_SID queried_dom_sid;
2035         fstring ascii_sid, padding;
2036         int ascii_dom_name_len;
2037         POLICY_HND connect_hnd;
2038         
2039         /* trusted domains listing variables */
2040         unsigned int num_domains, enum_ctx = 0;
2041         int i, pad_len, col_len = 20;
2042         DOM_SID *domain_sids;
2043         char **trusted_dom_names;
2044         fstring pdc_name, dummy;
2045         
2046         /* trusting domains listing variables */
2047         POLICY_HND domain_hnd;
2048         char **trusting_dom_names;
2049         uint32 *trusting_dom_rids;
2050         
2051         /*
2052          * Listing trusted domains (stored in secrets.tdb, if local)
2053          */
2054
2055         mem_ctx = talloc_init("trust relationships listing");
2056
2057         /*
2058          * set domain and pdc name to local samba server (default)
2059          * or to remote one given in command line
2060          */
2061         
2062         if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2063                 domain_name = opt_workgroup;
2064                 opt_target_workgroup = opt_workgroup;
2065         } else {
2066                 fstrcpy(pdc_name, global_myname());
2067                 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2068                 opt_target_workgroup = domain_name;
2069         };
2070
2071         /* open \PIPE\lsarpc and open policy handle */
2072         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2073                 DEBUG(0, ("Couldn't connect to domain controller\n"));
2074                 return -1;
2075         };
2076
2077         if (!cli_nt_session_open(cli, PI_LSARPC)) {
2078                 DEBUG(0, ("Could not initialise lsa pipe\n"));
2079                 return -1;
2080         };
2081
2082         nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2083                                         &connect_hnd);
2084         if (NT_STATUS_IS_ERR(nt_status)) {
2085                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2086                         nt_errstr(nt_status)));
2087                 return -1;
2088         };
2089         
2090         /* query info level 5 to obtain sid of a domain being queried */
2091         nt_status = cli_lsa_query_info_policy(
2092                 cli, mem_ctx, &connect_hnd, 5 /* info level */, 
2093                 dummy, &queried_dom_sid);
2094
2095         if (NT_STATUS_IS_ERR(nt_status)) {
2096                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2097                         nt_errstr(nt_status)));
2098                 return -1;
2099         }
2100                 
2101         /*
2102          * Keep calling LsaEnumTrustdom over opened pipe until
2103          * the end of enumeration is reached
2104          */
2105          
2106         d_printf("Trusted domains list:\n\n");
2107
2108         do {
2109                 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2110                                                    &num_domains,
2111                                                    &trusted_dom_names, &domain_sids);
2112                 
2113                 if (NT_STATUS_IS_ERR(nt_status)) {
2114                         DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2115                                 nt_errstr(nt_status)));
2116                         return -1;
2117                 };
2118                 
2119                 for (i = 0; i < num_domains; i++) {
2120                         /* convert sid into ascii string */
2121                         sid_to_string(ascii_sid, &(domain_sids[i]));
2122                 
2123                         /* calculate padding space for d_printf to look nicer */
2124                         pad_len = col_len - strlen(trusted_dom_names[i]);
2125                         padding[pad_len] = 0;
2126                         do padding[--pad_len] = ' '; while (pad_len);
2127                         
2128                         d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2129                 };
2130                 
2131                 /*
2132                  * in case of no trusted domains say something rather
2133                  * than just display blank line
2134                  */
2135                 if (!num_domains) d_printf("none\n");
2136
2137         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2138
2139         /* close this connection before doing next one */
2140         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2141         if (NT_STATUS_IS_ERR(nt_status)) {
2142                 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2143                         nt_errstr(nt_status)));
2144                 return -1;
2145         };
2146         
2147         cli_nt_session_close(cli);
2148
2149         /*
2150          * Listing trusting domains (stored in passdb backend, if local)
2151          */
2152         
2153         d_printf("\nTrusting domains list:\n\n");
2154
2155         /*
2156          * Open \PIPE\samr and get needed policy handles
2157          */
2158         if (!cli_nt_session_open(cli, PI_SAMR)) {
2159                 DEBUG(0, ("Could not initialise samr pipe\n"));
2160                 return -1;
2161         };
2162         
2163         /* SamrConnect */
2164         nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2165                                                                  &connect_hnd);
2166         if (!NT_STATUS_IS_OK(nt_status)) {
2167                 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2168                         nt_errstr(nt_status)));
2169                 return -1;
2170         };
2171         
2172         /* SamrOpenDomain - we have to open domain policy handle in order to be
2173            able to enumerate accounts*/
2174         nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2175                                                                          SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2176                                                                          &queried_dom_sid, &domain_hnd);                                                                         
2177         if (!NT_STATUS_IS_OK(nt_status)) {
2178                 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2179                         nt_errstr(nt_status)));
2180                 return -1;
2181         };
2182         
2183         /*
2184          * perform actual enumeration
2185          */
2186          
2187         enum_ctx = 0;   /* reset enumeration context from last enumeration */
2188         do {
2189                         
2190                 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2191                                                     &enum_ctx, ACB_DOMTRUST, 0xffff,
2192                                                     &trusting_dom_names, &trusting_dom_rids,
2193                                                     &num_domains);
2194                 if (NT_STATUS_IS_ERR(nt_status)) {
2195                         DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2196                                 nt_errstr(nt_status)));
2197                         return -1;
2198                 };
2199                 
2200                 for (i = 0; i < num_domains; i++) {
2201
2202                         /*
2203                          * get each single domain's sid (do we _really_ need this ?):
2204                          *  1) connect to domain's pdc
2205                          *  2) query the pdc for domain's sid
2206                          */
2207
2208                         /* get rid of '$' tail */
2209                         ascii_dom_name_len = strlen(trusting_dom_names[i]);
2210                         if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2211                                 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2212                         
2213                         /* calculate padding space for d_printf to look nicer */
2214                         pad_len = col_len - strlen(trusting_dom_names[i]);
2215                         padding[pad_len] = 0;
2216                         do padding[--pad_len] = ' '; while (pad_len);
2217
2218                         /* set opt_* variables to remote domain */
2219                         strupper_m(trusting_dom_names[i]);
2220                         opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2221                         opt_target_workgroup = opt_workgroup;
2222                         
2223                         d_printf("%s%s", trusting_dom_names[i], padding);
2224                         
2225                         /* connect to remote domain controller */
2226                         remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2227                         if (remote_cli) {                       
2228                                 /* query for domain's sid */
2229                                 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2230                                         d_printf("couldn't get domain's sid\n");
2231
2232                                 cli_shutdown(remote_cli);
2233                         
2234                         } else {
2235                                 d_printf("domain controller is not responding\n");
2236                         };
2237                 };
2238                 
2239                 if (!num_domains) d_printf("none\n");
2240                 
2241         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2242
2243         /* close opened samr and domain policy handles */
2244         nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2245         if (!NT_STATUS_IS_OK(nt_status)) {
2246                 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2247         };
2248         
2249         nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2250         if (!NT_STATUS_IS_OK(nt_status)) {
2251                 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2252         };
2253         
2254         /* close samr pipe and connection to IPC$ */
2255         cli_nt_session_close(cli);
2256         cli_shutdown(cli);
2257
2258         talloc_destroy(mem_ctx);         
2259         return 0;
2260 }
2261
2262 /**
2263  * Entrypoint for 'net rpc trustdom' code
2264  *
2265  * @param argc standard argc
2266  * @param argv standard argv without initial components
2267  *
2268  * @return Integer status (0 means success)
2269  */
2270
2271 static int rpc_trustdom(int argc, const char **argv)
2272 {
2273         struct functable func[] = {
2274                 {"add", rpc_trustdom_add},
2275                 {"del", rpc_trustdom_del},
2276                 {"establish", rpc_trustdom_establish},
2277                 {"revoke", rpc_trustdom_revoke},
2278                 {"help", rpc_trustdom_usage},
2279                 {"list", rpc_trustdom_list},
2280                 {NULL, NULL}
2281         };
2282
2283         if (argc == 0) {
2284                 rpc_trustdom_usage(argc, argv);
2285                 return -1;
2286         }
2287
2288         return (net_run_function(argc, argv, func, rpc_user_usage));
2289 }
2290
2291 /**
2292  * Check if a server will take rpc commands
2293  * @param flags Type of server to connect to (PDC, DMB, localhost)
2294  *              if the host is not explicitly specified
2295  * @return  BOOL (true means rpc supported)
2296  */
2297 BOOL net_rpc_check(unsigned flags)
2298 {
2299         struct cli_state cli;
2300         BOOL ret = False;
2301         struct in_addr server_ip;
2302         char *server_name = NULL;
2303
2304         /* flags (i.e. server type) may depend on command */
2305         if (!net_find_server(flags, &server_ip, &server_name))
2306                 return False;
2307
2308         ZERO_STRUCT(cli);
2309         if (cli_initialise(&cli) == False)
2310                 return False;
2311
2312         if (!cli_connect(&cli, server_name, &server_ip))
2313                 goto done;
2314         if (!attempt_netbios_session_request(&cli, global_myname(), 
2315                                              server_name, &server_ip))
2316                 goto done;
2317         if (!cli_negprot(&cli))
2318                 goto done;
2319         if (cli.protocol < PROTOCOL_NT1)
2320                 goto done;
2321
2322         ret = True;
2323  done:
2324         cli_shutdown(&cli);
2325         return ret;
2326 }
2327
2328
2329 /****************************************************************************/
2330
2331
2332 /** 
2333  * Basic usage function for 'net rpc'
2334  * @param argc  Standard main() style argc
2335  * @param argv  Standard main() style argv.  Initial components are already
2336  *              stripped
2337  **/
2338
2339 int net_rpc_usage(int argc, const char **argv) 
2340 {
2341         d_printf("  net rpc info \t\t\tshow basic info about a domain \n");
2342         d_printf("  net rpc join \t\t\tto join a domain \n");
2343         d_printf("  net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2344         d_printf("  net rpc testjoin \t\ttests that a join is valid\n");
2345         d_printf("  net rpc user \t\t\tto add, delete and list users\n");
2346         d_printf("  net rpc group \t\tto list groups\n");
2347         d_printf("  net rpc share \t\tto add, delete, and list shares\n");
2348         d_printf("  net rpc file \t\t\tto list open files\n");
2349         d_printf("  net rpc changetrustpw \tto change the trust account password\n");
2350         d_printf("  net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2351         d_printf("  net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2352         d_printf("  net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2353         d_printf("  net rpc trustdom \t\tto create trusting domain's account\n"
2354                  "\t\t\t\t\tor establish trust\n");
2355         d_printf("  net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2356         d_printf("  net rpc shutdown \t\tto shutdown a remote server\n");
2357         d_printf("\n");
2358         d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2359         d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2360         d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2361         d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2362         d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2363         return -1;
2364 }
2365
2366
2367 /**
2368  * Help function for 'net rpc'.  Calls command specific help if requested
2369  * or displays usage of net rpc
2370  * @param argc  Standard main() style argc
2371  * @param argv  Standard main() style argv.  Initial components are already
2372  *              stripped
2373  **/
2374
2375 int net_rpc_help(int argc, const char **argv)
2376 {
2377         struct functable func[] = {
2378                 {"join", rpc_join_usage},
2379                 {"user", rpc_user_usage},
2380                 {"group", rpc_group_usage},
2381                 {"share", rpc_share_usage},
2382                 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2383                 {"trustdom", rpc_trustdom_usage},
2384                 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2385                 /*{"shutdown", rpc_shutdown_usage}, */
2386                 {NULL, NULL}
2387         };
2388
2389         if (argc == 0) {
2390                 net_rpc_usage(argc, argv);
2391                 return -1;
2392         }
2393
2394         return (net_run_function(argc, argv, func, rpc_user_usage));
2395 }
2396
2397
2398 /** 
2399  * 'net rpc' entrypoint.
2400  * @param argc  Standard main() style argc
2401  * @param argv  Standard main() style argv.  Initial components are already
2402  *              stripped
2403  **/
2404
2405 int net_rpc(int argc, const char **argv)
2406 {
2407         struct functable func[] = {
2408                 {"info", net_rpc_info},
2409                 {"join", net_rpc_join},
2410                 {"oldjoin", net_rpc_oldjoin},
2411                 {"testjoin", net_rpc_testjoin},
2412                 {"user", net_rpc_user},
2413                 {"group", net_rpc_group},
2414                 {"share", net_rpc_share},
2415                 {"file", net_rpc_file},
2416                 {"changetrustpw", net_rpc_changetrustpw},
2417                 {"trustdom", rpc_trustdom},
2418                 {"abortshutdown", rpc_shutdown_abort},
2419                 {"shutdown", rpc_shutdown},
2420                 {"samdump", rpc_samdump},
2421                 {"vampire", rpc_vampire},
2422                 {"getsid", net_rpc_getsid},
2423                 {"help", net_rpc_help},
2424                 {NULL, NULL}
2425         };
2426         return net_run_function(argc, argv, func, net_rpc_usage);
2427 }