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