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