BUG 1055; patch from SATOH Fumiyasu <fumiya@miraclelinux.com>; formatting fixes...
[kai/samba-autobuild/.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 /** 
1048  * List groups on a remote RPC server
1049  *
1050  * All parameters are provided by the run_rpc_command function, except for
1051  * argc, argv which are passes through. 
1052  *
1053  * @param domain_sid The domain sid acquired from the remote server
1054  * @param cli A cli_state connected to the server.
1055  * @param mem_ctx Talloc context, destoyed on completion of the function.
1056  * @param argc  Standard main() style argc
1057  * @param argv  Standard main() style argv.  Initial components are already
1058  *              stripped
1059  *
1060  * @return Normal NTSTATUS return.
1061  **/
1062
1063 static NTSTATUS 
1064 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
1065                          struct cli_state *cli,
1066                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
1067 {
1068         POLICY_HND connect_pol, domain_pol;
1069         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1070         uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1071         struct acct_info *groups;
1072         DOM_SID global_sid_Builtin;
1073         BOOL global = False;
1074         BOOL local = False;
1075         BOOL builtin = False;
1076
1077         if (argc == 0) {
1078                 global = True;
1079                 local = True;
1080                 builtin = True;
1081         }
1082
1083         for (i=0; i<argc; i++) {
1084                 if (strequal(argv[i], "global"))
1085                         global = True;
1086
1087                 if (strequal(argv[i], "local"))
1088                         local = True;
1089
1090                 if (strequal(argv[i], "builtin"))
1091                         builtin = True;
1092         }
1093
1094         string_to_sid(&global_sid_Builtin, "S-1-5-32");
1095
1096         /* Get sam policy handle */
1097         
1098         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1099                                   &connect_pol);
1100         if (!NT_STATUS_IS_OK(result)) {
1101                 goto done;
1102         }
1103         
1104         /* Get domain policy handle */
1105         
1106         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1107                                       MAXIMUM_ALLOWED_ACCESS,
1108                                       domain_sid, &domain_pol);
1109         if (!NT_STATUS_IS_OK(result)) {
1110                 goto done;
1111         }
1112
1113         /* Query domain groups */
1114         if (opt_long_list_entries)
1115                 d_printf("\nGroup name            Comment"\
1116                          "\n-----------------------------\n");
1117         do {
1118                 SAM_DISPINFO_CTR ctr;
1119                 SAM_DISPINFO_3 info3;
1120                 uint32 max_size;
1121
1122                 ZERO_STRUCT(ctr);
1123                 ZERO_STRUCT(info3);
1124                 ctr.sam.info3 = &info3;
1125
1126                 if (!global) break;
1127
1128                 get_query_dispinfo_params(
1129                         loop_count, &max_entries, &max_size);
1130
1131                 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1132                                                  &start_idx, 3, &num_entries,
1133                                                  max_entries, max_size, &ctr);
1134                                                  
1135                 for (i = 0; i < num_entries; i++) {
1136
1137                         fstring group, desc;
1138
1139                         unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1140                         unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1141                         
1142                         if (opt_long_list_entries)
1143                                 printf("%-21.21s %-50.50s\n",
1144                                        group, desc);
1145                         else
1146                                 printf("%s\n", group);
1147                 }
1148         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1149         /* query domain aliases */
1150         start_idx = 0;
1151         do {
1152                 if (!local) break;
1153
1154                 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1155                                                   &start_idx, max_entries,
1156                                                   &groups, &num_entries);
1157
1158                 for (i = 0; i < num_entries; i++) {
1159
1160                         char *description = NULL;
1161
1162                         if (opt_long_list_entries) {
1163
1164                                 POLICY_HND alias_pol;
1165                                 ALIAS_INFO_CTR ctr;
1166
1167                                 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1168                                                                          &domain_pol,
1169                                                                          0x8,
1170                                                                          groups[i].rid,
1171                                                                          &alias_pol))) &&
1172                                     (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1173                                                                                &alias_pol, 3,
1174                                                                                &ctr))) &&
1175                                     (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1176                                                                     &alias_pol)))) {
1177                                         description = unistr2_tdup(mem_ctx,
1178                                                                    &ctr.alias.info3.uni_acct_desc);
1179                                 }
1180                         }
1181                         
1182                         if (description != NULL) {
1183                                 printf("%-21.21s %-50.50s\n", 
1184                                        groups[i].acct_name,
1185                                        description);
1186                         } else {
1187                                 printf("%s\n", groups[i].acct_name);
1188                         }
1189                 }
1190         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1191         cli_samr_close(cli, mem_ctx, &domain_pol);
1192         /* Get builtin policy handle */
1193         
1194         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1195                                       MAXIMUM_ALLOWED_ACCESS,
1196                                       &global_sid_Builtin, &domain_pol);
1197         if (!NT_STATUS_IS_OK(result)) {
1198                 goto done;
1199         }
1200         /* query builtin aliases */
1201         start_idx = 0;
1202         do {
1203                 if (!builtin) break;
1204
1205                 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1206                                                   &start_idx, max_entries,
1207                                                   &groups, &num_entries);
1208                                                  
1209                 for (i = 0; i < num_entries; i++) {
1210
1211                         char *description = NULL;
1212
1213                         if (opt_long_list_entries) {
1214
1215                                 POLICY_HND alias_pol;
1216                                 ALIAS_INFO_CTR ctr;
1217
1218                                 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1219                                                                          &domain_pol,
1220                                                                          0x8,
1221                                                                          groups[i].rid,
1222                                                                          &alias_pol))) &&
1223                                     (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1224                                                                                &alias_pol, 3,
1225                                                                                &ctr))) &&
1226                                     (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1227                                                                     &alias_pol)))) {
1228                                         description = unistr2_tdup(mem_ctx,
1229                                                                    &ctr.alias.info3.uni_acct_desc);
1230                                 }
1231                         }
1232                         
1233                         if (description != NULL) {
1234                                 printf("%-21.21s %-50.50s\n", 
1235                                        groups[i].acct_name,
1236                                        description);
1237                         } else {
1238                                 printf("%s\n", groups[i].acct_name);
1239                         }
1240                 }
1241         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1242
1243  done:
1244         return result;
1245 }
1246
1247 static int rpc_group_list(int argc, const char **argv)
1248 {
1249         return run_rpc_command(NULL, PI_SAMR, 0,
1250                                rpc_group_list_internals,
1251                                argc, argv);
1252 }
1253
1254 static NTSTATUS
1255 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1256                        const char *domain_name, const DOM_SID *domain_sid,
1257                        POLICY_HND *domain_pol, uint32 rid)
1258 {
1259         NTSTATUS result;
1260         POLICY_HND group_pol;
1261         uint32 num_members, *group_rids, *group_attrs;
1262         uint32 num_names;
1263         char **names;
1264         uint32 *name_types;
1265         int i;
1266
1267         fstring sid_str;
1268         sid_to_string(sid_str, domain_sid);
1269
1270         result = cli_samr_open_group(cli, mem_ctx, domain_pol,
1271                                      MAXIMUM_ALLOWED_ACCESS,
1272                                      rid, &group_pol);
1273
1274         if (!NT_STATUS_IS_OK(result))
1275                 return result;
1276
1277         result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1278                                          &num_members, &group_rids,
1279                                          &group_attrs);
1280
1281         if (!NT_STATUS_IS_OK(result))
1282                 return result;
1283
1284         while (num_members > 0) {
1285                 int this_time = 512;
1286
1287                 if (num_members < this_time)
1288                         this_time = num_members;
1289
1290                 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol, 1000,
1291                                               this_time, group_rids,
1292                                               &num_names, &names, &name_types);
1293
1294                 if (!NT_STATUS_IS_OK(result))
1295                         return result;
1296
1297                 /* We only have users as members, but make the output
1298                    the same as the output of alias members */
1299
1300                 for (i = 0; i < this_time; i++) {
1301
1302                         if (opt_long_list_entries) {
1303                                 printf("%s-%d %s\\%s %d\n", sid_str,
1304                                        group_rids[i], domain_name, names[i],
1305                                        SID_NAME_USER);
1306                         } else {
1307                                 printf("%s\\%s\n", domain_name, names[i]);
1308                         }
1309                 }
1310
1311                 num_members -= this_time;
1312                 group_rids += 512;
1313         }
1314
1315         return NT_STATUS_OK;
1316 }
1317
1318 static NTSTATUS
1319 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1320                        POLICY_HND *domain_pol, uint32 rid)
1321 {
1322         NTSTATUS result;
1323         POLICY_HND alias_pol, lsa_pol;
1324         uint32 num_members;
1325         DOM_SID *alias_sids;
1326         char **domains;
1327         char **names;
1328         uint32 *types;
1329         int i;
1330
1331         result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
1332                                      MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
1333
1334         if (!NT_STATUS_IS_OK(result))
1335                 return result;
1336
1337         result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
1338                                          &num_members, &alias_sids);
1339
1340         if (!NT_STATUS_IS_OK(result)) {
1341                 d_printf("Couldn't list alias members\n");
1342                 return result;
1343         }
1344
1345         cli_nt_session_close(cli);
1346
1347         if (!cli_nt_session_open(cli, PI_LSARPC)) {
1348                 d_printf("Couldn't open LSA pipe\n");
1349                 return result;
1350         }
1351
1352         result = cli_lsa_open_policy(cli, mem_ctx, True,
1353                                      SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1354
1355         if (!NT_STATUS_IS_OK(result)) {
1356                 d_printf("Couldn't open LSA policy handle\n");
1357                 return result;
1358         }
1359
1360         result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
1361                                      alias_sids, 
1362                                      &domains, &names, &types);
1363
1364         if (!NT_STATUS_IS_OK(result) &&
1365             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
1366                 d_printf("Couldn't lookup SIDs\n");
1367                 return result;
1368         }
1369
1370         for (i = 0; i < num_members; i++) {
1371                 fstring sid_str;
1372                 sid_to_string(sid_str, &alias_sids[i]);
1373
1374                 if (opt_long_list_entries) {
1375                         printf("%s %s\\%s %d\n", sid_str, 
1376                                domains[i] ? domains[i] : "*unknown*", 
1377                                names[i] ? names[i] : "*unknown*", types[i]);
1378                 } else {
1379                         if (domains[i])
1380                                 printf("%s\\%s\n", domains[i], names[i]);
1381                         else
1382                                 printf("%s\n", sid_str);
1383                 }
1384         }
1385
1386         return NT_STATUS_OK;
1387 }
1388  
1389 static NTSTATUS 
1390 rpc_group_members_internals(const DOM_SID *domain_sid,
1391                             const char *domain_name, 
1392                             struct cli_state *cli,
1393                             TALLOC_CTX *mem_ctx, int argc, const char **argv)
1394 {
1395         NTSTATUS result;
1396         POLICY_HND connect_pol, domain_pol;
1397         uint32 num_rids, *rids, *rid_types;
1398
1399         /* Get sam policy handle */
1400         
1401         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1402                                   &connect_pol);
1403
1404         if (!NT_STATUS_IS_OK(result))
1405                 return result;
1406         
1407         /* Get domain policy handle */
1408         
1409         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1410                                       MAXIMUM_ALLOWED_ACCESS,
1411                                       domain_sid, &domain_pol);
1412
1413         if (!NT_STATUS_IS_OK(result))
1414                 return result;
1415
1416         result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1417                                        1, argv, &num_rids, &rids, &rid_types);
1418
1419         if (!NT_STATUS_IS_OK(result)) {
1420
1421                 /* Ok, did not find it in the global sam, try with builtin */
1422
1423                 DOM_SID sid_Builtin;
1424
1425                 cli_samr_close(cli, mem_ctx, &domain_pol);
1426
1427                 string_to_sid(&sid_Builtin, "S-1-5-32");                
1428
1429                 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1430                                               MAXIMUM_ALLOWED_ACCESS,
1431                                               &sid_Builtin, &domain_pol);
1432
1433                 if (!NT_STATUS_IS_OK(result)) {
1434                         d_printf("Couldn't find group %s\n", argv[0]);
1435                         return result;
1436                 }
1437
1438                 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1439                                                1, argv, &num_rids,
1440                                                &rids, &rid_types);
1441
1442                 if (!NT_STATUS_IS_OK(result)) {
1443                         d_printf("Couldn't find group %s\n", argv[0]);
1444                         return result;
1445                 }
1446         }
1447
1448         if (num_rids != 1) {
1449                 d_printf("Couldn't find group %s\n", argv[0]);
1450                 return result;
1451         }
1452
1453         if (rid_types[0] == SID_NAME_DOM_GRP) {
1454                 return rpc_list_group_members(cli, mem_ctx, domain_name,
1455                                               domain_sid, &domain_pol,
1456                                               rids[0]);
1457         }
1458
1459         if (rid_types[0] == SID_NAME_ALIAS) {
1460                 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
1461                                               rids[0]);
1462         }
1463
1464         return NT_STATUS_NO_SUCH_GROUP;
1465 }
1466
1467 static int rpc_group_members(int argc, const char **argv)
1468 {
1469         if (argc != 1) {
1470                 return rpc_group_usage(argc, argv);
1471         }
1472
1473         return run_rpc_command(NULL, PI_SAMR, 0,
1474                                rpc_group_members_internals,
1475                                argc, argv);
1476 }
1477
1478 /** 
1479  * 'net rpc group' entrypoint.
1480  * @param argc  Standard main() style argc
1481  * @param argc  Standard main() style argv.  Initial components are already
1482  *              stripped
1483  **/
1484
1485 int net_rpc_group(int argc, const char **argv) 
1486 {
1487         struct functable func[] = {
1488 #if 0
1489                 {"add", rpc_group_add},
1490                 {"delete", rpc_group_delete},
1491 #endif
1492                 {"list", rpc_group_list},
1493                 {"members", rpc_group_members},
1494                 {NULL, NULL}
1495         };
1496         
1497         if (argc == 0) {
1498                 if (opt_long_list_entries) {
1499                 } else {
1500                 }
1501                 return run_rpc_command(NULL, PI_SAMR, 0, 
1502                                        rpc_group_list_internals,
1503                                        argc, argv);
1504         }
1505
1506         return net_run_function(argc, argv, func, rpc_group_usage);
1507 }
1508
1509 /****************************************************************************/
1510
1511 static int rpc_share_usage(int argc, const char **argv)
1512 {
1513         return net_help_share(argc, argv);
1514 }
1515
1516 /** 
1517  * Add a share on a remote RPC server
1518  *
1519  * All parameters are provided by the run_rpc_command function, except for
1520  * argc, argv which are passes through. 
1521  *
1522  * @param domain_sid The domain sid acquired from the remote server
1523  * @param cli A cli_state connected to the server.
1524  * @param mem_ctx Talloc context, destoyed on completion of the function.
1525  * @param argc  Standard main() style argc
1526  * @param argv  Standard main() style argv.  Initial components are already
1527  *              stripped
1528  *
1529  * @return Normal NTSTATUS return.
1530  **/
1531 static NTSTATUS 
1532 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name, 
1533                         struct cli_state *cli,
1534                         TALLOC_CTX *mem_ctx,int argc, const char **argv)
1535 {
1536         WERROR result;
1537         char *sharename=talloc_strdup(mem_ctx, argv[0]);
1538         char *path;
1539         uint32 type=0; /* only allow disk shares to be added */
1540         uint32 num_users=0, perms=0;
1541         char *password=NULL; /* don't allow a share password */
1542
1543         path = strchr(sharename, '=');
1544         if (!path)
1545                 return NT_STATUS_UNSUCCESSFUL;
1546         *path++ = '\0';
1547
1548         result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1549                                           opt_comment, perms, opt_maxusers,
1550                                           num_users, path, password);
1551         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1552 }
1553
1554 static int rpc_share_add(int argc, const char **argv)
1555 {
1556         if ((argc < 1) || !strchr(argv[0], '=')) {
1557                 DEBUG(1,("Sharename or path not specified on add\n"));
1558                 return rpc_share_usage(argc, argv);
1559         }
1560         return run_rpc_command(NULL, PI_SRVSVC, 0, 
1561                                rpc_share_add_internals,
1562                                argc, argv);
1563 }
1564
1565 /** 
1566  * Delete a share on a remote RPC server
1567  *
1568  * All parameters are provided by the run_rpc_command function, except for
1569  * argc, argv which are passes through. 
1570  *
1571  * @param domain_sid The domain sid acquired from the remote server
1572  * @param cli A cli_state connected to the server.
1573  * @param mem_ctx Talloc context, destoyed on completion of the function.
1574  * @param argc  Standard main() style argc
1575  * @param argv  Standard main() style argv.  Initial components are already
1576  *              stripped
1577  *
1578  * @return Normal NTSTATUS return.
1579  **/
1580 static NTSTATUS 
1581 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name, 
1582                         struct cli_state *cli,
1583                         TALLOC_CTX *mem_ctx,int argc, const char **argv)
1584 {
1585         WERROR result;
1586
1587         result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1588         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1589 }
1590
1591 /** 
1592  * Delete a share on a remote RPC server
1593  *
1594  * @param domain_sid The domain sid acquired from the remote server
1595  * @param argc  Standard main() style argc
1596  * @param argv  Standard main() style argv.  Initial components are already
1597  *              stripped
1598  *
1599  * @return A shell status integer (0 for success)
1600  **/
1601 static int rpc_share_delete(int argc, const char **argv)
1602 {
1603         if (argc < 1) {
1604                 DEBUG(1,("Sharename not specified on delete\n"));
1605                 return rpc_share_usage(argc, argv);
1606         }
1607         return run_rpc_command(NULL, PI_SRVSVC, 0, 
1608                                rpc_share_del_internals,
1609                                argc, argv);
1610 }
1611
1612 /**
1613  * Formatted print of share info
1614  *
1615  * @param info1  pointer to SRV_SHARE_INFO_1 to format
1616  **/
1617  
1618 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1619 {
1620         fstring netname = "", remark = "";
1621
1622         rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1623         rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1624
1625         if (opt_long_list_entries) {
1626                 d_printf("%-12s %-8.8s %-50s\n",
1627                          netname, share_type[info1->info_1.type], remark);
1628         } else {
1629                 d_printf("%s\n", netname);
1630         }
1631
1632 }
1633
1634 /** 
1635  * List shares on a remote RPC server
1636  *
1637  * All parameters are provided by the run_rpc_command function, except for
1638  * argc, argv which are passes through. 
1639  *
1640  * @param domain_sid The domain sid acquired from the remote server
1641  * @param cli A cli_state connected to the server.
1642  * @param mem_ctx Talloc context, destoyed on completion of the function.
1643  * @param argc  Standard main() style argc
1644  * @param argv  Standard main() style argv.  Initial components are already
1645  *              stripped
1646  *
1647  * @return Normal NTSTATUS return.
1648  **/
1649
1650 static NTSTATUS 
1651 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
1652                          struct cli_state *cli,
1653                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
1654 {
1655         SRV_SHARE_INFO_CTR ctr;
1656         WERROR result;
1657         ENUM_HND hnd;
1658         uint32 preferred_len = 0xffffffff, i;
1659
1660         init_enum_hnd(&hnd, 0);
1661
1662         result = cli_srvsvc_net_share_enum(
1663                 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1664
1665         if (!W_ERROR_IS_OK(result))
1666                 goto done;
1667
1668         /* Display results */
1669
1670         if (opt_long_list_entries) {
1671                 d_printf(
1672         "\nEnumerating shared resources (exports) on remote server:\n\n"\
1673         "\nShare name   Type     Description\n"\
1674         "----------   ----     -----------\n");
1675         }
1676         for (i = 0; i < ctr.num_entries; i++)
1677                 display_share_info_1(&ctr.share.info1[i]);
1678  done:
1679         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1680 }
1681
1682 /** 
1683  * 'net rpc share' entrypoint.
1684  * @param argc  Standard main() style argc
1685  * @param argv  Standard main() style argv.  Initial components are already
1686  *              stripped
1687  **/
1688
1689 int net_rpc_share(int argc, const char **argv) 
1690 {
1691         struct functable func[] = {
1692                 {"add", rpc_share_add},
1693                 {"delete", rpc_share_delete},
1694                 {NULL, NULL}
1695         };
1696
1697         if (argc == 0)
1698                 return run_rpc_command(NULL, PI_SRVSVC, 0, 
1699                                        rpc_share_list_internals,
1700                                        argc, argv);
1701
1702         return net_run_function(argc, argv, func, rpc_share_usage);
1703 }
1704
1705 /****************************************************************************/
1706
1707 static int rpc_file_usage(int argc, const char **argv)
1708 {
1709         return net_help_file(argc, argv);
1710 }
1711
1712 /** 
1713  * Close a file on a remote RPC server
1714  *
1715  * All parameters are provided by the run_rpc_command function, except for
1716  * argc, argv which are passes through. 
1717  *
1718  * @param domain_sid The domain sid acquired from the remote server
1719  * @param cli A cli_state connected to the server.
1720  * @param mem_ctx Talloc context, destoyed on completion of the function.
1721  * @param argc  Standard main() style argc
1722  * @param argv  Standard main() style argv.  Initial components are already
1723  *              stripped
1724  *
1725  * @return Normal NTSTATUS return.
1726  **/
1727 static NTSTATUS 
1728 rpc_file_close_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         WERROR result;
1733         result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1734         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1735 }
1736
1737 /** 
1738  * Close a file on a remote RPC server
1739  *
1740  * @param argc  Standard main() style argc
1741  * @param argv  Standard main() style argv.  Initial components are already
1742  *              stripped
1743  *
1744  * @return A shell status integer (0 for success)
1745  **/
1746 static int rpc_file_close(int argc, const char **argv)
1747 {
1748         if (argc < 1) {
1749                 DEBUG(1, ("No fileid given on close\n"));
1750                 return(rpc_file_usage(argc, argv));
1751         }
1752
1753         return run_rpc_command(NULL, PI_SRVSVC, 0, 
1754                                rpc_file_close_internals,
1755                                argc, argv);
1756 }
1757
1758 /** 
1759  * Formatted print of open file info 
1760  *
1761  * @param info3  FILE_INFO_3 contents
1762  * @param str3   strings for FILE_INFO_3
1763  **/
1764
1765 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1766 {
1767         fstring user = "", path = "";
1768
1769         rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1770         rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1771
1772         d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1773                  info3->id, user, info3->perms, info3->num_locks, path);
1774 }
1775
1776 /** 
1777  * List open files on a remote RPC server
1778  *
1779  * All parameters are provided by the run_rpc_command function, except for
1780  * argc, argv which are passes through. 
1781  *
1782  * @param domain_sid The domain sid acquired from the remote server
1783  * @param cli A cli_state connected to the server.
1784  * @param mem_ctx Talloc context, destoyed on completion of the function.
1785  * @param argc  Standard main() style argc
1786  * @param argv  Standard main() style argv.  Initial components are already
1787  *              stripped
1788  *
1789  * @return Normal NTSTATUS return.
1790  **/
1791
1792 static NTSTATUS 
1793 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
1794                         struct cli_state *cli,
1795                         TALLOC_CTX *mem_ctx, int argc, const char **argv)
1796 {
1797         SRV_FILE_INFO_CTR ctr;
1798         WERROR result;
1799         ENUM_HND hnd;
1800         uint32 preferred_len = 0xffffffff, i;
1801         const char *username=NULL;
1802
1803         init_enum_hnd(&hnd, 0);
1804
1805         /* if argc > 0, must be user command */
1806         if (argc > 0)
1807                 username = smb_xstrdup(argv[0]);
1808                 
1809         result = cli_srvsvc_net_file_enum(
1810                 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1811
1812         if (!W_ERROR_IS_OK(result))
1813                 goto done;
1814
1815         /* Display results */
1816
1817         d_printf(
1818                  "\nEnumerating open files on remote server:\n\n"\
1819                  "\nFileId  Opened by            Perms  Locks  Path"\
1820                  "\n------  ---------            -----  -----  ---- \n");
1821         for (i = 0; i < ctr.num_entries; i++)
1822                 display_file_info_3(&ctr.file.info3[i].info_3, 
1823                                     &ctr.file.info3[i].info_3_str);
1824  done:
1825         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1826 }
1827
1828
1829 /** 
1830  * List files for a user on a remote RPC server
1831  *
1832  * @param argc  Standard main() style argc
1833  * @param argv  Standard main() style argv.  Initial components are already
1834  *              stripped
1835  *
1836  * @return A shell status integer (0 for success)
1837  **/
1838 static int rpc_file_user(int argc, const char **argv)
1839 {
1840         if (argc < 1) {
1841                 DEBUG(1, ("No username given\n"));
1842                 return(rpc_file_usage(argc, argv));
1843         }
1844
1845         return run_rpc_command(NULL, PI_SRVSVC, 0, 
1846                                rpc_file_list_internals,
1847                                argc, argv);
1848 }
1849
1850
1851 /** 
1852  * 'net rpc file' entrypoint.
1853  * @param argc  Standard main() style argc
1854  * @param argv  Standard main() style argv.  Initial components are already
1855  *              stripped
1856  **/
1857
1858 int net_rpc_file(int argc, const char **argv) 
1859 {
1860         struct functable func[] = {
1861                 {"close", rpc_file_close},
1862                 {"user", rpc_file_user},
1863 #if 0
1864                 {"info", rpc_file_info},
1865 #endif
1866                 {NULL, NULL}
1867         };
1868
1869         if (argc == 0)
1870                 return run_rpc_command(NULL, PI_SRVSVC, 0, 
1871                                        rpc_file_list_internals,
1872                                        argc, argv);
1873
1874         return net_run_function(argc, argv, func, rpc_file_usage);
1875 }
1876
1877 /****************************************************************************/
1878
1879
1880
1881 /** 
1882  * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1883  *
1884  * All parameters are provided by the run_rpc_command function, except for
1885  * argc, argv which are passed through. 
1886  *
1887  * @param domain_sid The domain sid aquired from the remote server
1888  * @param cli A cli_state connected to the server.
1889  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1890  * @param argc  Standard main() style argc
1891  * @param argv  Standard main() style argv.  Initial components are already
1892  *              stripped
1893  *
1894  * @return Normal NTSTATUS return.
1895  **/
1896
1897 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, 
1898                                              const char *domain_name, 
1899                                              struct cli_state *cli, 
1900                                              TALLOC_CTX *mem_ctx, 
1901                                              int argc, const char **argv) 
1902 {
1903         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1904         
1905         result = cli_shutdown_abort(cli, mem_ctx);
1906         
1907         if (NT_STATUS_IS_OK(result))
1908                 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1909         else
1910                 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1911         
1912         return result;
1913 }
1914
1915
1916 /** 
1917  * ABORT the shutdown of a remote RPC Server,  over winreg pipe
1918  *
1919  * All parameters are provided by the run_rpc_command function, except for
1920  * argc, argv which are passed through. 
1921  *
1922  * @param domain_sid The domain sid aquired from the remote server
1923  * @param cli A cli_state connected to the server.
1924  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1925  * @param argc  Standard main() style argc
1926  * @param argv  Standard main() style argv.  Initial components are already
1927  *              stripped
1928  *
1929  * @return Normal NTSTATUS return.
1930  **/
1931
1932 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid, 
1933                                                  const char *domain_name, 
1934                                                  struct cli_state *cli, 
1935                                                  TALLOC_CTX *mem_ctx, 
1936                                                  int argc, const char **argv) 
1937 {
1938         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1939         
1940         result = cli_reg_abort_shutdown(cli, mem_ctx);
1941         
1942         if (NT_STATUS_IS_OK(result))
1943                 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1944         else
1945                 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1946         
1947         return result;
1948 }
1949
1950 /** 
1951  * ABORT the Shut down of a remote RPC server
1952  *
1953  * @param argc  Standard main() style argc
1954  * @param argv  Standard main() style argv.  Initial components are already
1955  *              stripped
1956  *
1957  * @return A shell status integer (0 for success)
1958  **/
1959
1960 static int rpc_shutdown_abort(int argc, const char **argv) 
1961 {
1962         int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0, 
1963                                  rpc_shutdown_abort_internals,
1964                                  argc, argv);
1965
1966         if (rc == 0)
1967                 return rc;
1968
1969         DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
1970
1971         return run_rpc_command(NULL, PI_WINREG, 0, 
1972                                rpc_reg_shutdown_abort_internals,
1973                                argc, argv);
1974 }
1975
1976 /** 
1977  * Shut down a remote RPC Server
1978  *
1979  * All parameters are provided by the run_rpc_command function, except for
1980  * argc, argv which are passes through. 
1981  *
1982  * @param domain_sid The domain sid aquired from the remote server
1983  * @param cli A cli_state connected to the server.
1984  * @param mem_ctx Talloc context, destoyed on compleation of the function.
1985  * @param argc  Standard main() style argc
1986  * @param argc  Standard main() style argv.  Initial components are already
1987  *              stripped
1988  *
1989  * @return Normal NTSTATUS return.
1990  **/
1991
1992 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, 
1993                                        const char *domain_name, 
1994                                        struct cli_state *cli, TALLOC_CTX *mem_ctx, 
1995                                        int argc, const char **argv) 
1996 {
1997         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1998         const char *msg = "This machine will be shutdown shortly";
1999         uint32 timeout = 20;
2000 #if 0
2001         poptContext pc;
2002         int rc;
2003
2004         struct poptOption long_options[] = {
2005                 {"message",    'm', POPT_ARG_STRING, &msg},
2006                 {"timeout",    't', POPT_ARG_INT,    &timeout},
2007                 {"reboot",     'r', POPT_ARG_NONE,   &reboot},
2008                 {"force",      'f', POPT_ARG_NONE,   &force},
2009                 { 0, 0, 0, 0}
2010         };
2011
2012         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
2013                             POPT_CONTEXT_KEEP_FIRST);
2014
2015         rc = poptGetNextOpt(pc);
2016         
2017         if (rc < -1) {
2018                 /* an error occurred during option processing */
2019                 DEBUG(0, ("%s: %s\n",
2020                           poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2021                           poptStrerror(rc)));
2022                 return NT_STATUS_INVALID_PARAMETER;
2023         }
2024 #endif
2025         if (opt_comment) {
2026                 msg = opt_comment;
2027         }
2028         if (opt_timeout) {
2029                 timeout = opt_timeout;
2030         }
2031
2032         /* create an entry */
2033         result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
2034
2035         if (NT_STATUS_IS_OK(result))
2036                 DEBUG(5,("Shutdown of remote machine succeeded\n"));
2037         else
2038                 DEBUG(0,("Shutdown of remote machine failed!\n"));
2039
2040         return result;
2041 }
2042
2043 /** 
2044  * Shut down a remote RPC server
2045  *
2046  * @param argc  Standard main() style argc
2047  * @param argc  Standard main() style argv.  Initial components are already
2048  *              stripped
2049  *
2050  * @return A shell status integer (0 for success)
2051  **/
2052
2053 static int rpc_shutdown(int argc, const char **argv) 
2054 {
2055         return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
2056                                        argc, argv);
2057 }
2058
2059 /***************************************************************************
2060   NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
2061   
2062  ***************************************************************************/
2063
2064 /**
2065  * Add interdomain trust account to the RPC server.
2066  * All parameters (except for argc and argv) are passed by run_rpc_command
2067  * function.
2068  *
2069  * @param domain_sid The domain sid acquired from the server
2070  * @param cli A cli_state connected to the server.
2071  * @param mem_ctx Talloc context, destoyed on completion of the function.
2072  * @param argc  Standard main() style argc
2073  * @param argc  Standard main() style argv.  Initial components are already
2074  *              stripped
2075  *
2076  * @return normal NTSTATUS return code
2077  */
2078
2079 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, 
2080                                            const char *domain_name, 
2081                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
2082                                            int argc, const char **argv) {
2083
2084         POLICY_HND connect_pol, domain_pol, user_pol;
2085         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2086         char *acct_name;
2087         uint16 acb_info;
2088         uint32 unknown, user_rid;
2089
2090         if (argc != 2) {
2091                 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
2092                 return NT_STATUS_INVALID_PARAMETER;
2093         }
2094
2095         /* 
2096          * Make valid trusting domain account (ie. uppercased and with '$' appended)
2097          */
2098          
2099         if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
2100                 return NT_STATUS_NO_MEMORY;
2101         }
2102
2103         strupper_m(acct_name);
2104
2105         /* Get samr policy handle */
2106         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2107                                   &connect_pol);
2108         if (!NT_STATUS_IS_OK(result)) {
2109                 goto done;
2110         }
2111         
2112         /* Get domain policy handle */
2113         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2114                                       MAXIMUM_ALLOWED_ACCESS,
2115                                       domain_sid, &domain_pol);
2116         if (!NT_STATUS_IS_OK(result)) {
2117                 goto done;
2118         }
2119
2120         /* Create trusting domain's account */
2121         acb_info = ACB_DOMTRUST;
2122         unknown = 0xe00500b0; /* No idea what this is - a permission mask?
2123                                  mimir: yes, most probably it is */
2124
2125         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
2126                                           acct_name, acb_info, unknown,
2127                                           &user_pol, &user_rid);
2128         if (!NT_STATUS_IS_OK(result)) {
2129                 goto done;
2130         }
2131
2132         {
2133                 SAM_USERINFO_CTR ctr;
2134                 SAM_USER_INFO_24 p24;
2135                 uchar pwbuf[516];
2136
2137                 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
2138
2139                 ZERO_STRUCT(ctr);
2140                 ZERO_STRUCT(p24);
2141
2142                 init_sam_user_info24(&p24, (char *)pwbuf, 24);
2143
2144                 ctr.switch_value = 24;
2145                 ctr.info.id24 = &p24;
2146
2147                 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
2148                                                &cli->user_session_key, &ctr);
2149
2150                 if (!NT_STATUS_IS_OK(result)) {
2151                         DEBUG(0,("Could not set trust account password: %s\n",
2152                                  nt_errstr(result)));
2153                         goto done;
2154                 }
2155         }
2156
2157  done:
2158         SAFE_FREE(acct_name);
2159         return result;
2160 }
2161
2162 /**
2163  * Create interdomain trust account for a remote domain.
2164  *
2165  * @param argc standard argc
2166  * @param argv standard argv without initial components
2167  *
2168  * @return Integer status (0 means success)
2169  **/
2170
2171 static int rpc_trustdom_add(int argc, const char **argv)
2172 {
2173         if (argc > 0) {
2174                 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
2175                                        argc, argv);
2176         } else {
2177                 d_printf("Usage: net rpc trustdom add <domain>\n");
2178                 return -1;
2179         }
2180 }
2181
2182
2183 /**
2184  * Delete interdomain trust account for a remote domain.
2185  *
2186  * @param argc standard argc
2187  * @param argv standard argv without initial components
2188  *
2189  * @return Integer status (0 means success)
2190  **/
2191  
2192 static int rpc_trustdom_del(int argc, const char **argv)
2193 {
2194         d_printf("Sorry, not yet implemented.\n");
2195         d_printf("Use 'smbpasswd -x -i' instead.\n");
2196         return -1;
2197 }
2198
2199  
2200 /**
2201  * Establish trust relationship to a trusting domain.
2202  * Interdomain account must already be created on remote PDC.
2203  *
2204  * @param argc standard argc
2205  * @param argv standard argv without initial components
2206  *
2207  * @return Integer status (0 means success)
2208  **/
2209
2210 static int rpc_trustdom_establish(int argc, const char **argv)
2211 {
2212         struct cli_state *cli;
2213         struct in_addr server_ip;
2214         POLICY_HND connect_hnd;
2215         TALLOC_CTX *mem_ctx;
2216         NTSTATUS nt_status;
2217         DOM_SID *domain_sid;
2218         WKS_INFO_100 wks_info;
2219         
2220         char* domain_name;
2221         char* domain_name_pol;
2222         char* acct_name;
2223         fstring pdc_name;
2224
2225         /*
2226          * Connect to \\server\ipc$ as 'our domain' account with password
2227          */
2228
2229         if (argc != 1) {
2230                 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2231                 return -1;
2232         }
2233
2234         domain_name = smb_xstrdup(argv[0]);
2235         strupper_m(domain_name);
2236
2237         /* account name used at first is our domain's name with '$' */
2238         asprintf(&acct_name, "%s$", lp_workgroup());
2239         strupper_m(acct_name);
2240         
2241         /*
2242          * opt_workgroup will be used by connection functions further,
2243          * hence it should be set to remote domain name instead of ours
2244          */
2245         if (opt_workgroup) {
2246                 opt_workgroup = smb_xstrdup(domain_name);
2247         };
2248         
2249         opt_user_name = acct_name;
2250
2251         /* find the domain controller */
2252         if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
2253                 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
2254                 return -1;
2255         }
2256
2257         /* connect to ipc$ as username/password */
2258         nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
2259         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
2260
2261                 /* Is it trusting domain account for sure ? */
2262                 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2263                         nt_errstr(nt_status)));
2264                 return -1;
2265         }
2266         
2267         /*
2268          * Connect to \\server\ipc$ again (this time anonymously)
2269          */
2270         
2271         nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
2272         
2273         if (NT_STATUS_IS_ERR(nt_status)) {
2274                 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2275                         domain_name, nt_errstr(nt_status)));
2276         }
2277
2278         /*
2279          * Use NetServerEnum2 to make sure we're talking to a proper server
2280          */
2281          
2282         if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2283                 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2284                          for domain %s\n", domain_name));
2285         }
2286          
2287         /*
2288          * Call WksQueryInfo to check remote server's capabilities
2289          * note: It is now used only to get unicode domain name
2290          */
2291         
2292         if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2293                 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2294                 return -1;
2295         }
2296
2297         if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2298                         domain_name))) {
2299                 DEBUG(0, ("talloc_init() failed\n"));
2300                 cli_shutdown(cli);
2301                 return -1;
2302         }
2303         
2304         nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2305         
2306         if (NT_STATUS_IS_ERR(nt_status)) {
2307                 DEBUG(0, ("WksQueryInfo call failed.\n"));
2308                 return -1;
2309         }
2310
2311         if (cli->nt_pipe_fnum)
2312                 cli_nt_session_close(cli);
2313
2314
2315         /*
2316          * Call LsaOpenPolicy and LsaQueryInfo
2317          */
2318          
2319         if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2320                 DEBUG(0, ("talloc_init() failed\n"));
2321                 cli_shutdown(cli);
2322                 return -1;
2323         }
2324
2325         if (!cli_nt_session_open(cli, PI_LSARPC)) {
2326                 DEBUG(0, ("Could not initialise lsa pipe\n"));
2327                 cli_shutdown(cli);
2328                 return -1;
2329         }
2330
2331         nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2332                                          &connect_hnd);
2333         if (NT_STATUS_IS_ERR(nt_status)) {
2334                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2335                         nt_errstr(nt_status)));
2336                 return -1;
2337         }
2338
2339         /* Querying info level 5 */
2340         
2341         nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2342                                               5 /* info level */, &domain_name_pol,
2343                                               &domain_sid);
2344         if (NT_STATUS_IS_ERR(nt_status)) {
2345                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2346                         nt_errstr(nt_status)));
2347                 return -1;
2348         }
2349
2350
2351
2352
2353         /* There should be actually query info level 3 (following nt serv behaviour),
2354            but I still don't know if it's _really_ necessary */
2355                         
2356         /*
2357          * Store the password in secrets db
2358          */
2359
2360         if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2361                                                    wks_info.uni_lan_grp.uni_str_len, opt_password,
2362                                                    *domain_sid)) {
2363                 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2364                 return -1;
2365         }
2366         
2367         /*
2368          * Close the pipes and clean up
2369          */
2370          
2371         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2372         if (NT_STATUS_IS_ERR(nt_status)) {
2373                 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2374                         nt_errstr(nt_status)));
2375                 return -1;
2376         }
2377
2378         if (cli->nt_pipe_fnum)
2379                 cli_nt_session_close(cli);
2380          
2381         talloc_destroy(mem_ctx);
2382          
2383         DEBUG(0, ("Success!\n"));
2384         return 0;
2385 }
2386
2387 /**
2388  * Revoke trust relationship to the remote domain
2389  *
2390  * @param argc standard argc
2391  * @param argv standard argv without initial components
2392  *
2393  * @return Integer status (0 means success)
2394  **/
2395
2396 static int rpc_trustdom_revoke(int argc, const char **argv)
2397 {
2398         char* domain_name;
2399
2400         if (argc < 1) return -1;
2401         
2402         /* generate upper cased domain name */
2403         domain_name = smb_xstrdup(argv[0]);
2404         strupper_m(domain_name);
2405
2406         /* delete password of the trust */
2407         if (!trusted_domain_password_delete(domain_name)) {
2408                 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2409                           domain_name));
2410                 return -1;
2411         };
2412         
2413         return 0;
2414 }
2415
2416 /**
2417  * Usage for 'net rpc trustdom' command
2418  *
2419  * @param argc standard argc
2420  * @param argv standard argv without inital components
2421  *
2422  * @return Integer status returned to shell
2423  **/
2424  
2425 static int rpc_trustdom_usage(int argc, const char **argv)
2426 {
2427         d_printf("  net rpc trustdom add \t\t add trusting domain's account\n");
2428         d_printf("  net rpc trustdom del \t\t delete trusting domain's account\n");
2429         d_printf("  net rpc trustdom establish \t establish relationship to trusted domain\n");
2430         d_printf("  net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2431         d_printf("  net rpc trustdom list \t show current interdomain trust relationships\n");
2432         return -1;
2433 }
2434
2435
2436 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, 
2437                                      const char *domain_name, 
2438                                      struct cli_state *cli, TALLOC_CTX *mem_ctx,
2439                                      int argc, const char **argv)
2440 {
2441         fstring str_sid;
2442         sid_to_string(str_sid, domain_sid);
2443         d_printf("%s\n", str_sid);
2444         return NT_STATUS_OK;
2445 }
2446
2447
2448 static int rpc_trustdom_list(int argc, const char **argv)
2449 {
2450         /* common variables */
2451         TALLOC_CTX* mem_ctx;
2452         struct cli_state *cli, *remote_cli;
2453         NTSTATUS nt_status;
2454         const char *domain_name = NULL;
2455         DOM_SID *queried_dom_sid;
2456         fstring ascii_sid, padding;
2457         int ascii_dom_name_len;
2458         POLICY_HND connect_hnd;
2459         
2460         /* trusted domains listing variables */
2461         unsigned int num_domains, enum_ctx = 0;
2462         int i, pad_len, col_len = 20;
2463         DOM_SID *domain_sids;
2464         char **trusted_dom_names;
2465         fstring pdc_name;
2466         char *dummy;
2467         
2468         /* trusting domains listing variables */
2469         POLICY_HND domain_hnd;
2470         char **trusting_dom_names;
2471         uint32 *trusting_dom_rids;
2472         
2473         /*
2474          * Listing trusted domains (stored in secrets.tdb, if local)
2475          */
2476
2477         mem_ctx = talloc_init("trust relationships listing");
2478
2479         /*
2480          * set domain and pdc name to local samba server (default)
2481          * or to remote one given in command line
2482          */
2483         
2484         if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2485                 domain_name = opt_workgroup;
2486                 opt_target_workgroup = opt_workgroup;
2487         } else {
2488                 fstrcpy(pdc_name, global_myname());
2489                 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2490                 opt_target_workgroup = domain_name;
2491         };
2492
2493         /* open \PIPE\lsarpc and open policy handle */
2494         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2495                 DEBUG(0, ("Couldn't connect to domain controller\n"));
2496                 return -1;
2497         };
2498
2499         if (!cli_nt_session_open(cli, PI_LSARPC)) {
2500                 DEBUG(0, ("Could not initialise lsa pipe\n"));
2501                 return -1;
2502         };
2503
2504         nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2505                                         &connect_hnd);
2506         if (NT_STATUS_IS_ERR(nt_status)) {
2507                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2508                         nt_errstr(nt_status)));
2509                 return -1;
2510         };
2511         
2512         /* query info level 5 to obtain sid of a domain being queried */
2513         nt_status = cli_lsa_query_info_policy(
2514                 cli, mem_ctx, &connect_hnd, 5 /* info level */, 
2515                 &dummy, &queried_dom_sid);
2516
2517         if (NT_STATUS_IS_ERR(nt_status)) {
2518                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2519                         nt_errstr(nt_status)));
2520                 return -1;
2521         }
2522                 
2523         /*
2524          * Keep calling LsaEnumTrustdom over opened pipe until
2525          * the end of enumeration is reached
2526          */
2527          
2528         d_printf("Trusted domains list:\n\n");
2529
2530         do {
2531                 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2532                                                    &num_domains,
2533                                                    &trusted_dom_names, &domain_sids);
2534                 
2535                 if (NT_STATUS_IS_ERR(nt_status)) {
2536                         DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2537                                 nt_errstr(nt_status)));
2538                         return -1;
2539                 };
2540                 
2541                 for (i = 0; i < num_domains; i++) {
2542                         /* convert sid into ascii string */
2543                         sid_to_string(ascii_sid, &(domain_sids[i]));
2544                 
2545                         /* calculate padding space for d_printf to look nicer */
2546                         pad_len = col_len - strlen(trusted_dom_names[i]);
2547                         padding[pad_len] = 0;
2548                         do padding[--pad_len] = ' '; while (pad_len);
2549                         
2550                         d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2551                 };
2552                 
2553                 /*
2554                  * in case of no trusted domains say something rather
2555                  * than just display blank line
2556                  */
2557                 if (!num_domains) d_printf("none\n");
2558
2559         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2560
2561         /* close this connection before doing next one */
2562         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2563         if (NT_STATUS_IS_ERR(nt_status)) {
2564                 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2565                         nt_errstr(nt_status)));
2566                 return -1;
2567         };
2568         
2569         cli_nt_session_close(cli);
2570
2571         /*
2572          * Listing trusting domains (stored in passdb backend, if local)
2573          */
2574         
2575         d_printf("\nTrusting domains list:\n\n");
2576
2577         /*
2578          * Open \PIPE\samr and get needed policy handles
2579          */
2580         if (!cli_nt_session_open(cli, PI_SAMR)) {
2581                 DEBUG(0, ("Could not initialise samr pipe\n"));
2582                 return -1;
2583         };
2584         
2585         /* SamrConnect */
2586         nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2587                                                                  &connect_hnd);
2588         if (!NT_STATUS_IS_OK(nt_status)) {
2589                 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2590                         nt_errstr(nt_status)));
2591                 return -1;
2592         };
2593         
2594         /* SamrOpenDomain - we have to open domain policy handle in order to be
2595            able to enumerate accounts*/
2596         nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2597                                          SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2598                                          queried_dom_sid, &domain_hnd);                                                                  
2599         if (!NT_STATUS_IS_OK(nt_status)) {
2600                 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2601                         nt_errstr(nt_status)));
2602                 return -1;
2603         };
2604         
2605         /*
2606          * perform actual enumeration
2607          */
2608          
2609         enum_ctx = 0;   /* reset enumeration context from last enumeration */
2610         do {
2611                         
2612                 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2613                                                     &enum_ctx, ACB_DOMTRUST, 0xffff,
2614                                                     &trusting_dom_names, &trusting_dom_rids,
2615                                                     &num_domains);
2616                 if (NT_STATUS_IS_ERR(nt_status)) {
2617                         DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2618                                 nt_errstr(nt_status)));
2619                         return -1;
2620                 };
2621                 
2622                 for (i = 0; i < num_domains; i++) {
2623
2624                         /*
2625                          * get each single domain's sid (do we _really_ need this ?):
2626                          *  1) connect to domain's pdc
2627                          *  2) query the pdc for domain's sid
2628                          */
2629
2630                         /* get rid of '$' tail */
2631                         ascii_dom_name_len = strlen(trusting_dom_names[i]);
2632                         if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2633                                 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2634                         
2635                         /* calculate padding space for d_printf to look nicer */
2636                         pad_len = col_len - strlen(trusting_dom_names[i]);
2637                         padding[pad_len] = 0;
2638                         do padding[--pad_len] = ' '; while (pad_len);
2639
2640                         /* set opt_* variables to remote domain */
2641                         strupper_m(trusting_dom_names[i]);
2642                         opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2643                         opt_target_workgroup = opt_workgroup;
2644                         
2645                         d_printf("%s%s", trusting_dom_names[i], padding);
2646                         
2647                         /* connect to remote domain controller */
2648                         remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2649                         if (remote_cli) {                       
2650                                 /* query for domain's sid */
2651                                 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2652                                         d_printf("couldn't get domain's sid\n");
2653
2654                                 cli_shutdown(remote_cli);
2655                         
2656                         } else {
2657                                 d_printf("domain controller is not responding\n");
2658                         };
2659                 };
2660                 
2661                 if (!num_domains) d_printf("none\n");
2662                 
2663         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2664
2665         /* close opened samr and domain policy handles */
2666         nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2667         if (!NT_STATUS_IS_OK(nt_status)) {
2668                 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2669         };
2670         
2671         nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2672         if (!NT_STATUS_IS_OK(nt_status)) {
2673                 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2674         };
2675         
2676         /* close samr pipe and connection to IPC$ */
2677         cli_nt_session_close(cli);
2678         cli_shutdown(cli);
2679
2680         talloc_destroy(mem_ctx);         
2681         return 0;
2682 }
2683
2684 /**
2685  * Entrypoint for 'net rpc trustdom' code
2686  *
2687  * @param argc standard argc
2688  * @param argv standard argv without initial components
2689  *
2690  * @return Integer status (0 means success)
2691  */
2692
2693 static int rpc_trustdom(int argc, const char **argv)
2694 {
2695         struct functable func[] = {
2696                 {"add", rpc_trustdom_add},
2697                 {"del", rpc_trustdom_del},
2698                 {"establish", rpc_trustdom_establish},
2699                 {"revoke", rpc_trustdom_revoke},
2700                 {"help", rpc_trustdom_usage},
2701                 {"list", rpc_trustdom_list},
2702                 {NULL, NULL}
2703         };
2704
2705         if (argc == 0) {
2706                 rpc_trustdom_usage(argc, argv);
2707                 return -1;
2708         }
2709
2710         return (net_run_function(argc, argv, func, rpc_user_usage));
2711 }
2712
2713 /**
2714  * Check if a server will take rpc commands
2715  * @param flags Type of server to connect to (PDC, DMB, localhost)
2716  *              if the host is not explicitly specified
2717  * @return  BOOL (true means rpc supported)
2718  */
2719 BOOL net_rpc_check(unsigned flags)
2720 {
2721         struct cli_state cli;
2722         BOOL ret = False;
2723         struct in_addr server_ip;
2724         char *server_name = NULL;
2725
2726         /* flags (i.e. server type) may depend on command */
2727         if (!net_find_server(flags, &server_ip, &server_name))
2728                 return False;
2729
2730         ZERO_STRUCT(cli);
2731         if (cli_initialise(&cli) == False)
2732                 return False;
2733
2734         if (!cli_connect(&cli, server_name, &server_ip))
2735                 goto done;
2736         if (!attempt_netbios_session_request(&cli, global_myname(), 
2737                                              server_name, &server_ip))
2738                 goto done;
2739         if (!cli_negprot(&cli))
2740                 goto done;
2741         if (cli.protocol < PROTOCOL_NT1)
2742                 goto done;
2743
2744         ret = True;
2745  done:
2746         cli_shutdown(&cli);
2747         return ret;
2748 }
2749
2750 /* dump sam database via samsync rpc calls */
2751 static int rpc_samdump(int argc, const char **argv) {
2752         return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
2753                                argc, argv);
2754 }
2755
2756 /* syncronise sam database via samsync rpc calls */
2757 static int rpc_vampire(int argc, const char **argv) {
2758         return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
2759                                argc, argv);
2760 }
2761 /****************************************************************************/
2762
2763
2764 /** 
2765  * Basic usage function for 'net rpc'
2766  * @param argc  Standard main() style argc
2767  * @param argv  Standard main() style argv.  Initial components are already
2768  *              stripped
2769  **/
2770
2771 int net_rpc_usage(int argc, const char **argv) 
2772 {
2773         d_printf("  net rpc info \t\t\tshow basic info about a domain \n");
2774         d_printf("  net rpc join \t\t\tto join a domain \n");
2775         d_printf("  net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2776         d_printf("  net rpc testjoin \t\ttests that a join is valid\n");
2777         d_printf("  net rpc user \t\t\tto add, delete and list users\n");
2778         d_printf("  net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
2779         d_printf("  net rpc group \t\tto list groups\n");
2780         d_printf("  net rpc share \t\tto add, delete, and list shares\n");
2781         d_printf("  net rpc file \t\t\tto list open files\n");
2782         d_printf("  net rpc changetrustpw \tto change the trust account password\n");
2783         d_printf("  net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2784         d_printf("  net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2785         d_printf("  net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2786         d_printf("  net rpc trustdom \t\tto create trusting domain's account\n"
2787                  "\t\t\t\t\tor establish trust\n");
2788         d_printf("  net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2789         d_printf("  net rpc shutdown \t\tto shutdown a remote server\n");
2790         d_printf("\n");
2791         d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2792         d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2793         d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2794         d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2795         d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2796         return -1;
2797 }
2798
2799
2800 /**
2801  * Help function for 'net rpc'.  Calls command specific help if requested
2802  * or displays usage of net rpc
2803  * @param argc  Standard main() style argc
2804  * @param argv  Standard main() style argv.  Initial components are already
2805  *              stripped
2806  **/
2807
2808 int net_rpc_help(int argc, const char **argv)
2809 {
2810         struct functable func[] = {
2811                 {"join", rpc_join_usage},
2812                 {"user", rpc_user_usage},
2813                 {"group", rpc_group_usage},
2814                 {"share", rpc_share_usage},
2815                 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2816                 {"trustdom", rpc_trustdom_usage},
2817                 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2818                 /*{"shutdown", rpc_shutdown_usage}, */
2819                 {NULL, NULL}
2820         };
2821
2822         if (argc == 0) {
2823                 net_rpc_usage(argc, argv);
2824                 return -1;
2825         }
2826
2827         return (net_run_function(argc, argv, func, rpc_user_usage));
2828 }
2829
2830
2831 /** 
2832  * 'net rpc' entrypoint.
2833  * @param argc  Standard main() style argc
2834  * @param argv  Standard main() style argv.  Initial components are already
2835  *              stripped
2836  **/
2837
2838 int net_rpc(int argc, const char **argv)
2839 {
2840         struct functable func[] = {
2841                 {"info", net_rpc_info},
2842                 {"join", net_rpc_join},
2843                 {"oldjoin", net_rpc_oldjoin},
2844                 {"testjoin", net_rpc_testjoin},
2845                 {"user", net_rpc_user},
2846                 {"password", rpc_user_password},
2847                 {"group", net_rpc_group},
2848                 {"share", net_rpc_share},
2849                 {"file", net_rpc_file},
2850                 {"changetrustpw", net_rpc_changetrustpw},
2851                 {"trustdom", rpc_trustdom},
2852                 {"abortshutdown", rpc_shutdown_abort},
2853                 {"shutdown", rpc_shutdown},
2854                 {"samdump", rpc_samdump},
2855                 {"vampire", rpc_vampire},
2856                 {"getsid", net_rpc_getsid},
2857                 {"help", net_rpc_help},
2858                 {NULL, NULL}
2859         };
2860         return net_run_function(argc, argv, func, net_rpc_usage);
2861 }