2e30a47afaab346d8fd372c98f546e4194cd3022
[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         return NT_STATUS_UNSUCCESSFUL;
1383 }
1384
1385 static int rpc_group_addmem(int argc, const char **argv)
1386 {
1387         return run_rpc_command(NULL, PI_SAMR, 0,
1388                                rpc_group_addmem_internals,
1389                                argc, argv);
1390 }
1391
1392 static NTSTATUS
1393 rpc_del_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1394                  const DOM_SID *group_sid, const char *member)
1395 {
1396         POLICY_HND connect_pol, domain_pol;
1397         NTSTATUS result;
1398         uint32 group_rid;
1399         POLICY_HND group_pol;
1400
1401         uint32 num_rids;
1402         uint32 *rids = NULL;
1403         uint32 *rid_types = NULL;
1404
1405         DOM_SID sid;
1406
1407         sid_copy(&sid, group_sid);
1408
1409         if (!sid_split_rid(&sid, &group_rid))
1410                 return NT_STATUS_UNSUCCESSFUL;
1411
1412         /* Get sam policy handle */     
1413         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1414                                   &connect_pol);
1415         if (!NT_STATUS_IS_OK(result))
1416                 return result;
1417         
1418         /* Get domain policy handle */
1419         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1420                                       MAXIMUM_ALLOWED_ACCESS,
1421                                       &sid, &domain_pol);
1422         if (!NT_STATUS_IS_OK(result))
1423                 return result;
1424
1425         result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1426                                        1, &member,
1427                                        &num_rids, &rids, &rid_types);
1428
1429         if (!NT_STATUS_IS_OK(result)) {
1430                 d_printf("Could not lookup up group member %s\n", member);
1431                 goto done;
1432         }
1433
1434         result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1435                                      MAXIMUM_ALLOWED_ACCESS,
1436                                      group_rid, &group_pol);
1437
1438         if (!NT_STATUS_IS_OK(result))
1439                 goto done;
1440
1441         result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1442
1443  done:
1444         cli_samr_close(cli, mem_ctx, &connect_pol);
1445         return result;
1446 }
1447
1448 static NTSTATUS
1449 rpc_del_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1450                  const DOM_SID *alias_sid, const char *member)
1451 {
1452         POLICY_HND connect_pol, domain_pol;
1453         NTSTATUS result;
1454         uint32 alias_rid;
1455         POLICY_HND alias_pol;
1456
1457         DOM_SID member_sid;
1458         enum SID_NAME_USE member_type;
1459
1460         DOM_SID sid;
1461
1462         sid_copy(&sid, alias_sid);
1463
1464         if (!sid_split_rid(&sid, &alias_rid))
1465                 return NT_STATUS_UNSUCCESSFUL;
1466
1467         result = get_sid_from_name(cli, mem_ctx, member,
1468                                    &member_sid, &member_type);
1469
1470         if (!NT_STATUS_IS_OK(result)) {
1471                 d_printf("Could not lookup up group member %s\n", member);
1472                 return result;
1473         }
1474
1475         /* Get sam policy handle */     
1476         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1477                                   &connect_pol);
1478         if (!NT_STATUS_IS_OK(result)) {
1479                 goto done;
1480         }
1481         
1482         /* Get domain policy handle */
1483         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1484                                       MAXIMUM_ALLOWED_ACCESS,
1485                                       &sid, &domain_pol);
1486         if (!NT_STATUS_IS_OK(result)) {
1487                 goto done;
1488         }
1489
1490         result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1491                                      MAXIMUM_ALLOWED_ACCESS,
1492                                      alias_rid, &alias_pol);
1493
1494         if (!NT_STATUS_IS_OK(result))
1495                 return result;
1496
1497         result = cli_samr_del_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1498
1499         if (!NT_STATUS_IS_OK(result))
1500                 return result;
1501
1502  done:
1503         cli_samr_close(cli, mem_ctx, &connect_pol);
1504         return result;
1505 }
1506
1507 static NTSTATUS 
1508 rpc_group_delmem_internals(const DOM_SID *domain_sid, const char *domain_name, 
1509                            struct cli_state *cli,
1510                            TALLOC_CTX *mem_ctx, int argc, const char **argv)
1511 {
1512         DOM_SID group_sid;
1513         enum SID_NAME_USE group_type;
1514
1515         if (argc != 2) {
1516                 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1517                 return NT_STATUS_UNSUCCESSFUL;
1518         }
1519
1520         if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1521                                                &group_sid, &group_type))) {
1522                 d_printf("Could not lookup group name %s\n", argv[0]);
1523                 return NT_STATUS_UNSUCCESSFUL;
1524         }
1525
1526         if (group_type == SID_NAME_DOM_GRP) {
1527                 NTSTATUS result = rpc_del_groupmem(cli, mem_ctx,
1528                                                    &group_sid, argv[1]);
1529
1530                 if (!NT_STATUS_IS_OK(result)) {
1531                         d_printf("Could not del %s to %s: %s\n",
1532                                  argv[1], argv[0], nt_errstr(result));
1533                         return result;
1534                 }
1535         }
1536
1537         if (group_type == SID_NAME_ALIAS) {
1538                 NTSTATUS result = rpc_del_aliasmem(cli, mem_ctx, 
1539                                                    &group_sid, argv[1]);
1540
1541                 if (!NT_STATUS_IS_OK(result)) {
1542                         d_printf("Could not add %s to %s: %s\n",
1543                                  argv[1], argv[0], nt_errstr(result));
1544                         return result;
1545                 }
1546         }
1547
1548         return NT_STATUS_UNSUCCESSFUL;
1549 }
1550
1551 static int rpc_group_delmem(int argc, const char **argv)
1552 {
1553         return run_rpc_command(NULL, PI_SAMR, 0,
1554                                rpc_group_delmem_internals,
1555                                argc, argv);
1556 }
1557
1558 /** 
1559  * List groups on a remote RPC server
1560  *
1561  * All parameters are provided by the run_rpc_command function, except for
1562  * argc, argv which are passes through. 
1563  *
1564  * @param domain_sid The domain sid acquired from the remote server
1565  * @param cli A cli_state connected to the server.
1566  * @param mem_ctx Talloc context, destoyed on completion of the function.
1567  * @param argc  Standard main() style argc
1568  * @param argv  Standard main() style argv.  Initial components are already
1569  *              stripped
1570  *
1571  * @return Normal NTSTATUS return.
1572  **/
1573
1574 static NTSTATUS 
1575 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
1576                          struct cli_state *cli,
1577                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
1578 {
1579         POLICY_HND connect_pol, domain_pol;
1580         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1581         uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1582         struct acct_info *groups;
1583         DOM_SID global_sid_Builtin;
1584         BOOL global = False;
1585         BOOL local = False;
1586         BOOL builtin = False;
1587
1588         if (argc == 0) {
1589                 global = True;
1590                 local = True;
1591                 builtin = True;
1592         }
1593
1594         for (i=0; i<argc; i++) {
1595                 if (strequal(argv[i], "global"))
1596                         global = True;
1597
1598                 if (strequal(argv[i], "local"))
1599                         local = True;
1600
1601                 if (strequal(argv[i], "builtin"))
1602                         builtin = True;
1603         }
1604
1605         string_to_sid(&global_sid_Builtin, "S-1-5-32");
1606
1607         /* Get sam policy handle */
1608         
1609         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1610                                   &connect_pol);
1611         if (!NT_STATUS_IS_OK(result)) {
1612                 goto done;
1613         }
1614         
1615         /* Get domain policy handle */
1616         
1617         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1618                                       MAXIMUM_ALLOWED_ACCESS,
1619                                       domain_sid, &domain_pol);
1620         if (!NT_STATUS_IS_OK(result)) {
1621                 goto done;
1622         }
1623
1624         /* Query domain groups */
1625         if (opt_long_list_entries)
1626                 d_printf("\nGroup name            Comment"\
1627                          "\n-----------------------------\n");
1628         do {
1629                 SAM_DISPINFO_CTR ctr;
1630                 SAM_DISPINFO_3 info3;
1631                 uint32 max_size;
1632
1633                 ZERO_STRUCT(ctr);
1634                 ZERO_STRUCT(info3);
1635                 ctr.sam.info3 = &info3;
1636
1637                 if (!global) break;
1638
1639                 get_query_dispinfo_params(
1640                         loop_count, &max_entries, &max_size);
1641
1642                 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1643                                                  &start_idx, 3, &num_entries,
1644                                                  max_entries, max_size, &ctr);
1645
1646                 if (!NT_STATUS_IS_OK(result) &&
1647                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1648                         break;
1649                                                  
1650                 for (i = 0; i < num_entries; i++) {
1651
1652                         fstring group, desc;
1653
1654                         unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1655                         unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1656                         
1657                         if (opt_long_list_entries)
1658                                 printf("%-21.21s %-50.50s\n",
1659                                        group, desc);
1660                         else
1661                                 printf("%s\n", group);
1662                 }
1663         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1664         /* query domain aliases */
1665         start_idx = 0;
1666         do {
1667                 if (!local) break;
1668
1669                 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1670                                                   &start_idx, max_entries,
1671                                                   &groups, &num_entries);
1672
1673                 if (!NT_STATUS_IS_OK(result) &&
1674                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1675                         break;
1676                                                  
1677                 for (i = 0; i < num_entries; i++) {
1678
1679                         char *description = NULL;
1680
1681                         if (opt_long_list_entries) {
1682
1683                                 POLICY_HND alias_pol;
1684                                 ALIAS_INFO_CTR ctr;
1685
1686                                 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1687                                                                          &domain_pol,
1688                                                                          0x8,
1689                                                                          groups[i].rid,
1690                                                                          &alias_pol))) &&
1691                                     (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1692                                                                                &alias_pol, 3,
1693                                                                                &ctr))) &&
1694                                     (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1695                                                                     &alias_pol)))) {
1696                                         description = unistr2_tdup(mem_ctx,
1697                                                                    &ctr.alias.info3.uni_acct_desc);
1698                                 }
1699                         }
1700                         
1701                         if (description != NULL) {
1702                                 printf("%-21.21s %-50.50s\n", 
1703                                        groups[i].acct_name,
1704                                        description);
1705                         } else {
1706                                 printf("%s\n", groups[i].acct_name);
1707                         }
1708                 }
1709         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1710         cli_samr_close(cli, mem_ctx, &domain_pol);
1711         /* Get builtin policy handle */
1712         
1713         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1714                                       MAXIMUM_ALLOWED_ACCESS,
1715                                       &global_sid_Builtin, &domain_pol);
1716         if (!NT_STATUS_IS_OK(result)) {
1717                 goto done;
1718         }
1719         /* query builtin aliases */
1720         start_idx = 0;
1721         do {
1722                 if (!builtin) break;
1723
1724                 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1725                                                   &start_idx, max_entries,
1726                                                   &groups, &num_entries);
1727                                                  
1728                 if (!NT_STATUS_IS_OK(result) &&
1729                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1730                         break;
1731                                                  
1732                 for (i = 0; i < num_entries; i++) {
1733
1734                         char *description = NULL;
1735
1736                         if (opt_long_list_entries) {
1737
1738                                 POLICY_HND alias_pol;
1739                                 ALIAS_INFO_CTR ctr;
1740
1741                                 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1742                                                                          &domain_pol,
1743                                                                          0x8,
1744                                                                          groups[i].rid,
1745                                                                          &alias_pol))) &&
1746                                     (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1747                                                                                &alias_pol, 3,
1748                                                                                &ctr))) &&
1749                                     (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1750                                                                     &alias_pol)))) {
1751                                         description = unistr2_tdup(mem_ctx,
1752                                                                    &ctr.alias.info3.uni_acct_desc);
1753                                 }
1754                         }
1755                         
1756                         if (description != NULL) {
1757                                 printf("%-21.21s %-50.50s\n", 
1758                                        groups[i].acct_name,
1759                                        description);
1760                         } else {
1761                                 printf("%s\n", groups[i].acct_name);
1762                         }
1763                 }
1764         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1765
1766  done:
1767         return result;
1768 }
1769
1770 static int rpc_group_list(int argc, const char **argv)
1771 {
1772         return run_rpc_command(NULL, PI_SAMR, 0,
1773                                rpc_group_list_internals,
1774                                argc, argv);
1775 }
1776
1777 static NTSTATUS
1778 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1779                        const char *domain_name, const DOM_SID *domain_sid,
1780                        POLICY_HND *domain_pol, uint32 rid)
1781 {
1782         NTSTATUS result;
1783         POLICY_HND group_pol;
1784         uint32 num_members, *group_rids, *group_attrs;
1785         uint32 num_names;
1786         char **names;
1787         uint32 *name_types;
1788         int i;
1789
1790         fstring sid_str;
1791         sid_to_string(sid_str, domain_sid);
1792
1793         result = cli_samr_open_group(cli, mem_ctx, domain_pol,
1794                                      MAXIMUM_ALLOWED_ACCESS,
1795                                      rid, &group_pol);
1796
1797         if (!NT_STATUS_IS_OK(result))
1798                 return result;
1799
1800         result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1801                                          &num_members, &group_rids,
1802                                          &group_attrs);
1803
1804         if (!NT_STATUS_IS_OK(result))
1805                 return result;
1806
1807         while (num_members > 0) {
1808                 int this_time = 512;
1809
1810                 if (num_members < this_time)
1811                         this_time = num_members;
1812
1813                 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol, 1000,
1814                                               this_time, group_rids,
1815                                               &num_names, &names, &name_types);
1816
1817                 if (!NT_STATUS_IS_OK(result))
1818                         return result;
1819
1820                 /* We only have users as members, but make the output
1821                    the same as the output of alias members */
1822
1823                 for (i = 0; i < this_time; i++) {
1824
1825                         if (opt_long_list_entries) {
1826                                 printf("%s-%d %s\\%s %d\n", sid_str,
1827                                        group_rids[i], domain_name, names[i],
1828                                        SID_NAME_USER);
1829                         } else {
1830                                 printf("%s\\%s\n", domain_name, names[i]);
1831                         }
1832                 }
1833
1834                 num_members -= this_time;
1835                 group_rids += 512;
1836         }
1837
1838         return NT_STATUS_OK;
1839 }
1840
1841 static NTSTATUS
1842 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1843                        POLICY_HND *domain_pol, uint32 rid)
1844 {
1845         NTSTATUS result;
1846         POLICY_HND alias_pol, lsa_pol;
1847         uint32 num_members;
1848         DOM_SID *alias_sids;
1849         char **domains;
1850         char **names;
1851         uint32 *types;
1852         int i;
1853
1854         result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
1855                                      MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
1856
1857         if (!NT_STATUS_IS_OK(result))
1858                 return result;
1859
1860         result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
1861                                          &num_members, &alias_sids);
1862
1863         if (!NT_STATUS_IS_OK(result)) {
1864                 d_printf("Couldn't list alias members\n");
1865                 return result;
1866         }
1867
1868         if (num_members == 0) {
1869                 return NT_STATUS_OK;
1870         }
1871
1872         cli_nt_session_close(cli);
1873
1874         if (!cli_nt_session_open(cli, PI_LSARPC)) {
1875                 d_printf("Couldn't open LSA pipe\n");
1876                 return result;
1877         }
1878
1879         result = cli_lsa_open_policy(cli, mem_ctx, True,
1880                                      SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1881
1882         if (!NT_STATUS_IS_OK(result)) {
1883                 d_printf("Couldn't open LSA policy handle\n");
1884                 return result;
1885         }
1886
1887         result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
1888                                      alias_sids, 
1889                                      &domains, &names, &types);
1890
1891         if (!NT_STATUS_IS_OK(result) &&
1892             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
1893                 d_printf("Couldn't lookup SIDs\n");
1894                 return result;
1895         }
1896
1897         for (i = 0; i < num_members; i++) {
1898                 fstring sid_str;
1899                 sid_to_string(sid_str, &alias_sids[i]);
1900
1901                 if (opt_long_list_entries) {
1902                         printf("%s %s\\%s %d\n", sid_str, 
1903                                domains[i] ? domains[i] : "*unknown*", 
1904                                names[i] ? names[i] : "*unknown*", types[i]);
1905                 } else {
1906                         if (domains[i])
1907                                 printf("%s\\%s\n", domains[i], names[i]);
1908                         else
1909                                 printf("%s\n", sid_str);
1910                 }
1911         }
1912
1913         return NT_STATUS_OK;
1914 }
1915  
1916 static NTSTATUS 
1917 rpc_group_members_internals(const DOM_SID *domain_sid,
1918                             const char *domain_name, 
1919                             struct cli_state *cli,
1920                             TALLOC_CTX *mem_ctx, int argc, const char **argv)
1921 {
1922         NTSTATUS result;
1923         POLICY_HND connect_pol, domain_pol;
1924         uint32 num_rids, *rids, *rid_types;
1925
1926         /* Get sam policy handle */
1927         
1928         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1929                                   &connect_pol);
1930
1931         if (!NT_STATUS_IS_OK(result))
1932                 return result;
1933         
1934         /* Get domain policy handle */
1935         
1936         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1937                                       MAXIMUM_ALLOWED_ACCESS,
1938                                       domain_sid, &domain_pol);
1939
1940         if (!NT_STATUS_IS_OK(result))
1941                 return result;
1942
1943         result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1944                                        1, argv, &num_rids, &rids, &rid_types);
1945
1946         if (!NT_STATUS_IS_OK(result)) {
1947
1948                 /* Ok, did not find it in the global sam, try with builtin */
1949
1950                 DOM_SID sid_Builtin;
1951
1952                 cli_samr_close(cli, mem_ctx, &domain_pol);
1953
1954                 string_to_sid(&sid_Builtin, "S-1-5-32");                
1955
1956                 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1957                                               MAXIMUM_ALLOWED_ACCESS,
1958                                               &sid_Builtin, &domain_pol);
1959
1960                 if (!NT_STATUS_IS_OK(result)) {
1961                         d_printf("Couldn't find group %s\n", argv[0]);
1962                         return result;
1963                 }
1964
1965                 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1966                                                1, argv, &num_rids,
1967                                                &rids, &rid_types);
1968
1969                 if (!NT_STATUS_IS_OK(result)) {
1970                         d_printf("Couldn't find group %s\n", argv[0]);
1971                         return result;
1972                 }
1973         }
1974
1975         if (num_rids != 1) {
1976                 d_printf("Couldn't find group %s\n", argv[0]);
1977                 return result;
1978         }
1979
1980         if (rid_types[0] == SID_NAME_DOM_GRP) {
1981                 return rpc_list_group_members(cli, mem_ctx, domain_name,
1982                                               domain_sid, &domain_pol,
1983                                               rids[0]);
1984         }
1985
1986         if (rid_types[0] == SID_NAME_ALIAS) {
1987                 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
1988                                               rids[0]);
1989         }
1990
1991         return NT_STATUS_NO_SUCH_GROUP;
1992 }
1993
1994 static int rpc_group_members(int argc, const char **argv)
1995 {
1996         if (argc != 1) {
1997                 return rpc_group_usage(argc, argv);
1998         }
1999
2000         return run_rpc_command(NULL, PI_SAMR, 0,
2001                                rpc_group_members_internals,
2002                                argc, argv);
2003 }
2004
2005 /** 
2006  * 'net rpc group' entrypoint.
2007  * @param argc  Standard main() style argc
2008  * @param argc  Standard main() style argv.  Initial components are already
2009  *              stripped
2010  **/
2011
2012 int net_rpc_group(int argc, const char **argv) 
2013 {
2014         struct functable func[] = {
2015                 {"add", rpc_group_add},
2016                 {"addmem", rpc_group_addmem},
2017                 {"delmem", rpc_group_delmem},
2018 #if 0
2019                 {"delete", rpc_group_delete},
2020 #endif
2021                 {"list", rpc_group_list},
2022                 {"members", rpc_group_members},
2023                 {NULL, NULL}
2024         };
2025         
2026         if (argc == 0) {
2027                 if (opt_long_list_entries) {
2028                 } else {
2029                 }
2030                 return run_rpc_command(NULL, PI_SAMR, 0, 
2031                                        rpc_group_list_internals,
2032                                        argc, argv);
2033         }
2034
2035         return net_run_function(argc, argv, func, rpc_group_usage);
2036 }
2037
2038 /****************************************************************************/
2039
2040 static int rpc_share_usage(int argc, const char **argv)
2041 {
2042         return net_help_share(argc, argv);
2043 }
2044
2045 /** 
2046  * Add a share on a remote RPC server
2047  *
2048  * All parameters are provided by the run_rpc_command function, except for
2049  * argc, argv which are passes through. 
2050  *
2051  * @param domain_sid The domain sid acquired from the remote server
2052  * @param cli A cli_state connected to the server.
2053  * @param mem_ctx Talloc context, destoyed on completion of the function.
2054  * @param argc  Standard main() style argc
2055  * @param argv  Standard main() style argv.  Initial components are already
2056  *              stripped
2057  *
2058  * @return Normal NTSTATUS return.
2059  **/
2060 static NTSTATUS 
2061 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name, 
2062                         struct cli_state *cli,
2063                         TALLOC_CTX *mem_ctx,int argc, const char **argv)
2064 {
2065         WERROR result;
2066         char *sharename=talloc_strdup(mem_ctx, argv[0]);
2067         char *path;
2068         uint32 type=0; /* only allow disk shares to be added */
2069         uint32 num_users=0, perms=0;
2070         char *password=NULL; /* don't allow a share password */
2071
2072         path = strchr(sharename, '=');
2073         if (!path)
2074                 return NT_STATUS_UNSUCCESSFUL;
2075         *path++ = '\0';
2076
2077         result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
2078                                           opt_comment, perms, opt_maxusers,
2079                                           num_users, path, password);
2080         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2081 }
2082
2083 static int rpc_share_add(int argc, const char **argv)
2084 {
2085         if ((argc < 1) || !strchr(argv[0], '=')) {
2086                 DEBUG(1,("Sharename or path not specified on add\n"));
2087                 return rpc_share_usage(argc, argv);
2088         }
2089         return run_rpc_command(NULL, PI_SRVSVC, 0, 
2090                                rpc_share_add_internals,
2091                                argc, argv);
2092 }
2093
2094 /** 
2095  * Delete a share on a remote RPC server
2096  *
2097  * All parameters are provided by the run_rpc_command function, except for
2098  * argc, argv which are passes through. 
2099  *
2100  * @param domain_sid The domain sid acquired from the remote server
2101  * @param cli A cli_state connected to the server.
2102  * @param mem_ctx Talloc context, destoyed on completion of the function.
2103  * @param argc  Standard main() style argc
2104  * @param argv  Standard main() style argv.  Initial components are already
2105  *              stripped
2106  *
2107  * @return Normal NTSTATUS return.
2108  **/
2109 static NTSTATUS 
2110 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name, 
2111                         struct cli_state *cli,
2112                         TALLOC_CTX *mem_ctx,int argc, const char **argv)
2113 {
2114         WERROR result;
2115
2116         result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
2117         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2118 }
2119
2120 /** 
2121  * Delete a share on a remote RPC server
2122  *
2123  * @param domain_sid The domain sid acquired from the remote server
2124  * @param argc  Standard main() style argc
2125  * @param argv  Standard main() style argv.  Initial components are already
2126  *              stripped
2127  *
2128  * @return A shell status integer (0 for success)
2129  **/
2130 static int rpc_share_delete(int argc, const char **argv)
2131 {
2132         if (argc < 1) {
2133                 DEBUG(1,("Sharename not specified on delete\n"));
2134                 return rpc_share_usage(argc, argv);
2135         }
2136         return run_rpc_command(NULL, PI_SRVSVC, 0, 
2137                                rpc_share_del_internals,
2138                                argc, argv);
2139 }
2140
2141 /**
2142  * Formatted print of share info
2143  *
2144  * @param info1  pointer to SRV_SHARE_INFO_1 to format
2145  **/
2146  
2147 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
2148 {
2149         fstring netname = "", remark = "";
2150
2151         rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
2152         rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
2153
2154         if (opt_long_list_entries) {
2155                 d_printf("%-12s %-8.8s %-50s\n",
2156                          netname, share_type[info1->info_1.type], remark);
2157         } else {
2158                 d_printf("%s\n", netname);
2159         }
2160
2161 }
2162
2163 /** 
2164  * List shares on a remote RPC server
2165  *
2166  * All parameters are provided by the run_rpc_command function, except for
2167  * argc, argv which are passes through. 
2168  *
2169  * @param domain_sid The domain sid acquired from the remote server
2170  * @param cli A cli_state connected to the server.
2171  * @param mem_ctx Talloc context, destoyed on completion of the function.
2172  * @param argc  Standard main() style argc
2173  * @param argv  Standard main() style argv.  Initial components are already
2174  *              stripped
2175  *
2176  * @return Normal NTSTATUS return.
2177  **/
2178
2179 static NTSTATUS 
2180 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
2181                          struct cli_state *cli,
2182                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
2183 {
2184         SRV_SHARE_INFO_CTR ctr;
2185         WERROR result;
2186         ENUM_HND hnd;
2187         uint32 preferred_len = 0xffffffff, i;
2188
2189         init_enum_hnd(&hnd, 0);
2190
2191         result = cli_srvsvc_net_share_enum(
2192                 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
2193
2194         if (!W_ERROR_IS_OK(result))
2195                 goto done;
2196
2197         /* Display results */
2198
2199         if (opt_long_list_entries) {
2200                 d_printf(
2201         "\nEnumerating shared resources (exports) on remote server:\n\n"\
2202         "\nShare name   Type     Description\n"\
2203         "----------   ----     -----------\n");
2204         }
2205         for (i = 0; i < ctr.num_entries; i++)
2206                 display_share_info_1(&ctr.share.info1[i]);
2207  done:
2208         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2209 }
2210
2211 /** 
2212  * 'net rpc share' entrypoint.
2213  * @param argc  Standard main() style argc
2214  * @param argv  Standard main() style argv.  Initial components are already
2215  *              stripped
2216  **/
2217
2218 int net_rpc_share(int argc, const char **argv) 
2219 {
2220         struct functable func[] = {
2221                 {"add", rpc_share_add},
2222                 {"delete", rpc_share_delete},
2223                 {NULL, NULL}
2224         };
2225
2226         if (argc == 0)
2227                 return run_rpc_command(NULL, PI_SRVSVC, 0, 
2228                                        rpc_share_list_internals,
2229                                        argc, argv);
2230
2231         return net_run_function(argc, argv, func, rpc_share_usage);
2232 }
2233
2234 /****************************************************************************/
2235
2236 static int rpc_file_usage(int argc, const char **argv)
2237 {
2238         return net_help_file(argc, argv);
2239 }
2240
2241 /** 
2242  * Close a file on a remote RPC server
2243  *
2244  * All parameters are provided by the run_rpc_command function, except for
2245  * argc, argv which are passes through. 
2246  *
2247  * @param domain_sid The domain sid acquired from the remote server
2248  * @param cli A cli_state connected to the server.
2249  * @param mem_ctx Talloc context, destoyed on completion of the function.
2250  * @param argc  Standard main() style argc
2251  * @param argv  Standard main() style argv.  Initial components are already
2252  *              stripped
2253  *
2254  * @return Normal NTSTATUS return.
2255  **/
2256 static NTSTATUS 
2257 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name, 
2258                          struct cli_state *cli,
2259                          TALLOC_CTX *mem_ctx, int argc, const char **argv)
2260 {
2261         WERROR result;
2262         result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
2263         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2264 }
2265
2266 /** 
2267  * Close a file on a remote RPC server
2268  *
2269  * @param argc  Standard main() style argc
2270  * @param argv  Standard main() style argv.  Initial components are already
2271  *              stripped
2272  *
2273  * @return A shell status integer (0 for success)
2274  **/
2275 static int rpc_file_close(int argc, const char **argv)
2276 {
2277         if (argc < 1) {
2278                 DEBUG(1, ("No fileid given on close\n"));
2279                 return(rpc_file_usage(argc, argv));
2280         }
2281
2282         return run_rpc_command(NULL, PI_SRVSVC, 0, 
2283                                rpc_file_close_internals,
2284                                argc, argv);
2285 }
2286
2287 /** 
2288  * Formatted print of open file info 
2289  *
2290  * @param info3  FILE_INFO_3 contents
2291  * @param str3   strings for FILE_INFO_3
2292  **/
2293
2294 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
2295 {
2296         fstring user = "", path = "";
2297
2298         rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
2299         rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
2300
2301         d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
2302                  info3->id, user, info3->perms, info3->num_locks, path);
2303 }
2304
2305 /** 
2306  * List open files on a remote RPC server
2307  *
2308  * All parameters are provided by the run_rpc_command function, except for
2309  * argc, argv which are passes through. 
2310  *
2311  * @param domain_sid The domain sid acquired from the remote server
2312  * @param cli A cli_state connected to the server.
2313  * @param mem_ctx Talloc context, destoyed on completion of the function.
2314  * @param argc  Standard main() style argc
2315  * @param argv  Standard main() style argv.  Initial components are already
2316  *              stripped
2317  *
2318  * @return Normal NTSTATUS return.
2319  **/
2320
2321 static NTSTATUS 
2322 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
2323                         struct cli_state *cli,
2324                         TALLOC_CTX *mem_ctx, int argc, const char **argv)
2325 {
2326         SRV_FILE_INFO_CTR ctr;
2327         WERROR result;
2328         ENUM_HND hnd;
2329         uint32 preferred_len = 0xffffffff, i;
2330         const char *username=NULL;
2331
2332         init_enum_hnd(&hnd, 0);
2333
2334         /* if argc > 0, must be user command */
2335         if (argc > 0)
2336                 username = smb_xstrdup(argv[0]);
2337                 
2338         result = cli_srvsvc_net_file_enum(
2339                 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
2340
2341         if (!W_ERROR_IS_OK(result))
2342                 goto done;
2343
2344         /* Display results */
2345
2346         d_printf(
2347                  "\nEnumerating open files on remote server:\n\n"\
2348                  "\nFileId  Opened by            Perms  Locks  Path"\
2349                  "\n------  ---------            -----  -----  ---- \n");
2350         for (i = 0; i < ctr.num_entries; i++)
2351                 display_file_info_3(&ctr.file.info3[i].info_3, 
2352                                     &ctr.file.info3[i].info_3_str);
2353  done:
2354         return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2355 }
2356
2357
2358 /** 
2359  * List files for a user on a remote RPC server
2360  *
2361  * @param argc  Standard main() style argc
2362  * @param argv  Standard main() style argv.  Initial components are already
2363  *              stripped
2364  *
2365  * @return A shell status integer (0 for success)
2366  **/
2367 static int rpc_file_user(int argc, const char **argv)
2368 {
2369         if (argc < 1) {
2370                 DEBUG(1, ("No username given\n"));
2371                 return(rpc_file_usage(argc, argv));
2372         }
2373
2374         return run_rpc_command(NULL, PI_SRVSVC, 0, 
2375                                rpc_file_list_internals,
2376                                argc, argv);
2377 }
2378
2379
2380 /** 
2381  * 'net rpc file' entrypoint.
2382  * @param argc  Standard main() style argc
2383  * @param argv  Standard main() style argv.  Initial components are already
2384  *              stripped
2385  **/
2386
2387 int net_rpc_file(int argc, const char **argv) 
2388 {
2389         struct functable func[] = {
2390                 {"close", rpc_file_close},
2391                 {"user", rpc_file_user},
2392 #if 0
2393                 {"info", rpc_file_info},
2394 #endif
2395                 {NULL, NULL}
2396         };
2397
2398         if (argc == 0)
2399                 return run_rpc_command(NULL, PI_SRVSVC, 0, 
2400                                        rpc_file_list_internals,
2401                                        argc, argv);
2402
2403         return net_run_function(argc, argv, func, rpc_file_usage);
2404 }
2405
2406 /****************************************************************************/
2407
2408
2409
2410 /** 
2411  * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
2412  *
2413  * All parameters are provided by the run_rpc_command function, except for
2414  * argc, argv which are passed through. 
2415  *
2416  * @param domain_sid The domain sid aquired from the remote server
2417  * @param cli A cli_state connected to the server.
2418  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2419  * @param argc  Standard main() style argc
2420  * @param argv  Standard main() style argv.  Initial components are already
2421  *              stripped
2422  *
2423  * @return Normal NTSTATUS return.
2424  **/
2425
2426 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, 
2427                                              const char *domain_name, 
2428                                              struct cli_state *cli, 
2429                                              TALLOC_CTX *mem_ctx, 
2430                                              int argc, const char **argv) 
2431 {
2432         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2433         
2434         result = cli_shutdown_abort(cli, mem_ctx);
2435         
2436         if (NT_STATUS_IS_OK(result))
2437                 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
2438         else
2439                 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
2440         
2441         return result;
2442 }
2443
2444
2445 /** 
2446  * ABORT the shutdown of a remote RPC Server,  over winreg pipe
2447  *
2448  * All parameters are provided by the run_rpc_command function, except for
2449  * argc, argv which are passed through. 
2450  *
2451  * @param domain_sid The domain sid aquired from the remote server
2452  * @param cli A cli_state connected to the server.
2453  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2454  * @param argc  Standard main() style argc
2455  * @param argv  Standard main() style argv.  Initial components are already
2456  *              stripped
2457  *
2458  * @return Normal NTSTATUS return.
2459  **/
2460
2461 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid, 
2462                                                  const char *domain_name, 
2463                                                  struct cli_state *cli, 
2464                                                  TALLOC_CTX *mem_ctx, 
2465                                                  int argc, const char **argv) 
2466 {
2467         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2468         
2469         result = cli_reg_abort_shutdown(cli, mem_ctx);
2470         
2471         if (NT_STATUS_IS_OK(result))
2472                 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
2473         else
2474                 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
2475         
2476         return result;
2477 }
2478
2479 /** 
2480  * ABORT the Shut down of a remote RPC server
2481  *
2482  * @param argc  Standard main() style argc
2483  * @param argv  Standard main() style argv.  Initial components are already
2484  *              stripped
2485  *
2486  * @return A shell status integer (0 for success)
2487  **/
2488
2489 static int rpc_shutdown_abort(int argc, const char **argv) 
2490 {
2491         int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0, 
2492                                  rpc_shutdown_abort_internals,
2493                                  argc, argv);
2494
2495         if (rc == 0)
2496                 return rc;
2497
2498         DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
2499
2500         return run_rpc_command(NULL, PI_WINREG, 0, 
2501                                rpc_reg_shutdown_abort_internals,
2502                                argc, argv);
2503 }
2504
2505 /** 
2506  * Shut down a remote RPC Server
2507  *
2508  * All parameters are provided by the run_rpc_command function, except for
2509  * argc, argv which are passes through. 
2510  *
2511  * @param domain_sid The domain sid aquired from the remote server
2512  * @param cli A cli_state connected to the server.
2513  * @param mem_ctx Talloc context, destoyed on compleation of the function.
2514  * @param argc  Standard main() style argc
2515  * @param argc  Standard main() style argv.  Initial components are already
2516  *              stripped
2517  *
2518  * @return Normal NTSTATUS return.
2519  **/
2520
2521 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, 
2522                                        const char *domain_name, 
2523                                        struct cli_state *cli, TALLOC_CTX *mem_ctx, 
2524                                        int argc, const char **argv) 
2525 {
2526         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2527         const char *msg = "This machine will be shutdown shortly";
2528         uint32 timeout = 20;
2529 #if 0
2530         poptContext pc;
2531         int rc;
2532
2533         struct poptOption long_options[] = {
2534                 {"message",    'm', POPT_ARG_STRING, &msg},
2535                 {"timeout",    't', POPT_ARG_INT,    &timeout},
2536                 {"reboot",     'r', POPT_ARG_NONE,   &reboot},
2537                 {"force",      'f', POPT_ARG_NONE,   &force},
2538                 { 0, 0, 0, 0}
2539         };
2540
2541         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
2542                             POPT_CONTEXT_KEEP_FIRST);
2543
2544         rc = poptGetNextOpt(pc);
2545         
2546         if (rc < -1) {
2547                 /* an error occurred during option processing */
2548                 DEBUG(0, ("%s: %s\n",
2549                           poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2550                           poptStrerror(rc)));
2551                 return NT_STATUS_INVALID_PARAMETER;
2552         }
2553 #endif
2554         if (opt_comment) {
2555                 msg = opt_comment;
2556         }
2557         if (opt_timeout) {
2558                 timeout = opt_timeout;
2559         }
2560
2561         /* create an entry */
2562         result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
2563
2564         if (NT_STATUS_IS_OK(result))
2565                 DEBUG(5,("Shutdown of remote machine succeeded\n"));
2566         else
2567                 DEBUG(0,("Shutdown of remote machine failed!\n"));
2568
2569         return result;
2570 }
2571
2572 /** 
2573  * Shut down a remote RPC server
2574  *
2575  * @param argc  Standard main() style argc
2576  * @param argc  Standard main() style argv.  Initial components are already
2577  *              stripped
2578  *
2579  * @return A shell status integer (0 for success)
2580  **/
2581
2582 static int rpc_shutdown(int argc, const char **argv) 
2583 {
2584         return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
2585                                        argc, argv);
2586 }
2587
2588 /***************************************************************************
2589   NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
2590   
2591  ***************************************************************************/
2592
2593 /**
2594  * Add interdomain trust account to the RPC server.
2595  * All parameters (except for argc and argv) are passed by run_rpc_command
2596  * function.
2597  *
2598  * @param domain_sid The domain sid acquired from the server
2599  * @param cli A cli_state connected to the server.
2600  * @param mem_ctx Talloc context, destoyed on completion of the function.
2601  * @param argc  Standard main() style argc
2602  * @param argc  Standard main() style argv.  Initial components are already
2603  *              stripped
2604  *
2605  * @return normal NTSTATUS return code
2606  */
2607
2608 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, 
2609                                            const char *domain_name, 
2610                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
2611                                            int argc, const char **argv) {
2612
2613         POLICY_HND connect_pol, domain_pol, user_pol;
2614         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2615         char *acct_name;
2616         uint16 acb_info;
2617         uint32 unknown, user_rid;
2618
2619         if (argc != 2) {
2620                 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
2621                 return NT_STATUS_INVALID_PARAMETER;
2622         }
2623
2624         /* 
2625          * Make valid trusting domain account (ie. uppercased and with '$' appended)
2626          */
2627          
2628         if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
2629                 return NT_STATUS_NO_MEMORY;
2630         }
2631
2632         strupper_m(acct_name);
2633
2634         /* Get samr policy handle */
2635         result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2636                                   &connect_pol);
2637         if (!NT_STATUS_IS_OK(result)) {
2638                 goto done;
2639         }
2640         
2641         /* Get domain policy handle */
2642         result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2643                                       MAXIMUM_ALLOWED_ACCESS,
2644                                       domain_sid, &domain_pol);
2645         if (!NT_STATUS_IS_OK(result)) {
2646                 goto done;
2647         }
2648
2649         /* Create trusting domain's account */
2650         acb_info = ACB_DOMTRUST;
2651         unknown = 0xe00500b0; /* No idea what this is - a permission mask?
2652                                  mimir: yes, most probably it is */
2653
2654         result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
2655                                           acct_name, acb_info, unknown,
2656                                           &user_pol, &user_rid);
2657         if (!NT_STATUS_IS_OK(result)) {
2658                 goto done;
2659         }
2660
2661         {
2662                 SAM_USERINFO_CTR ctr;
2663                 SAM_USER_INFO_24 p24;
2664                 uchar pwbuf[516];
2665
2666                 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
2667
2668                 ZERO_STRUCT(ctr);
2669                 ZERO_STRUCT(p24);
2670
2671                 init_sam_user_info24(&p24, (char *)pwbuf, 24);
2672
2673                 ctr.switch_value = 24;
2674                 ctr.info.id24 = &p24;
2675
2676                 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
2677                                                &cli->user_session_key, &ctr);
2678
2679                 if (!NT_STATUS_IS_OK(result)) {
2680                         DEBUG(0,("Could not set trust account password: %s\n",
2681                                  nt_errstr(result)));
2682                         goto done;
2683                 }
2684         }
2685
2686  done:
2687         SAFE_FREE(acct_name);
2688         return result;
2689 }
2690
2691 /**
2692  * Create interdomain trust account for a remote domain.
2693  *
2694  * @param argc standard argc
2695  * @param argv standard argv without initial components
2696  *
2697  * @return Integer status (0 means success)
2698  **/
2699
2700 static int rpc_trustdom_add(int argc, const char **argv)
2701 {
2702         if (argc > 0) {
2703                 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
2704                                        argc, argv);
2705         } else {
2706                 d_printf("Usage: net rpc trustdom add <domain>\n");
2707                 return -1;
2708         }
2709 }
2710
2711
2712 /**
2713  * Delete interdomain trust account for a remote domain.
2714  *
2715  * @param argc standard argc
2716  * @param argv standard argv without initial components
2717  *
2718  * @return Integer status (0 means success)
2719  **/
2720  
2721 static int rpc_trustdom_del(int argc, const char **argv)
2722 {
2723         d_printf("Sorry, not yet implemented.\n");
2724         d_printf("Use 'smbpasswd -x -i' instead.\n");
2725         return -1;
2726 }
2727
2728  
2729 /**
2730  * Establish trust relationship to a trusting domain.
2731  * Interdomain account must already be created on remote PDC.
2732  *
2733  * @param argc standard argc
2734  * @param argv standard argv without initial components
2735  *
2736  * @return Integer status (0 means success)
2737  **/
2738
2739 static int rpc_trustdom_establish(int argc, const char **argv)
2740 {
2741         struct cli_state *cli;
2742         struct in_addr server_ip;
2743         POLICY_HND connect_hnd;
2744         TALLOC_CTX *mem_ctx;
2745         NTSTATUS nt_status;
2746         DOM_SID *domain_sid;
2747         WKS_INFO_100 wks_info;
2748         
2749         char* domain_name;
2750         char* domain_name_pol;
2751         char* acct_name;
2752         fstring pdc_name;
2753
2754         /*
2755          * Connect to \\server\ipc$ as 'our domain' account with password
2756          */
2757
2758         if (argc != 1) {
2759                 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2760                 return -1;
2761         }
2762
2763         domain_name = smb_xstrdup(argv[0]);
2764         strupper_m(domain_name);
2765
2766         /* account name used at first is our domain's name with '$' */
2767         asprintf(&acct_name, "%s$", lp_workgroup());
2768         strupper_m(acct_name);
2769         
2770         /*
2771          * opt_workgroup will be used by connection functions further,
2772          * hence it should be set to remote domain name instead of ours
2773          */
2774         if (opt_workgroup) {
2775                 opt_workgroup = smb_xstrdup(domain_name);
2776         };
2777         
2778         opt_user_name = acct_name;
2779
2780         /* find the domain controller */
2781         if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
2782                 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
2783                 return -1;
2784         }
2785
2786         /* connect to ipc$ as username/password */
2787         nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
2788         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
2789
2790                 /* Is it trusting domain account for sure ? */
2791                 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2792                         nt_errstr(nt_status)));
2793                 return -1;
2794         }
2795         
2796         /*
2797          * Connect to \\server\ipc$ again (this time anonymously)
2798          */
2799         
2800         nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
2801         
2802         if (NT_STATUS_IS_ERR(nt_status)) {
2803                 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2804                         domain_name, nt_errstr(nt_status)));
2805         }
2806
2807         /*
2808          * Use NetServerEnum2 to make sure we're talking to a proper server
2809          */
2810          
2811         if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2812                 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2813                          for domain %s\n", domain_name));
2814         }
2815          
2816         /*
2817          * Call WksQueryInfo to check remote server's capabilities
2818          * note: It is now used only to get unicode domain name
2819          */
2820         
2821         if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2822                 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2823                 return -1;
2824         }
2825
2826         if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2827                         domain_name))) {
2828                 DEBUG(0, ("talloc_init() failed\n"));
2829                 cli_shutdown(cli);
2830                 return -1;
2831         }
2832         
2833         nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2834         
2835         if (NT_STATUS_IS_ERR(nt_status)) {
2836                 DEBUG(0, ("WksQueryInfo call failed.\n"));
2837                 return -1;
2838         }
2839
2840         if (cli->nt_pipe_fnum)
2841                 cli_nt_session_close(cli);
2842
2843
2844         /*
2845          * Call LsaOpenPolicy and LsaQueryInfo
2846          */
2847          
2848         if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2849                 DEBUG(0, ("talloc_init() failed\n"));
2850                 cli_shutdown(cli);
2851                 return -1;
2852         }
2853
2854         if (!cli_nt_session_open(cli, PI_LSARPC)) {
2855                 DEBUG(0, ("Could not initialise lsa pipe\n"));
2856                 cli_shutdown(cli);
2857                 return -1;
2858         }
2859
2860         nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2861                                          &connect_hnd);
2862         if (NT_STATUS_IS_ERR(nt_status)) {
2863                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2864                         nt_errstr(nt_status)));
2865                 return -1;
2866         }
2867
2868         /* Querying info level 5 */
2869         
2870         nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2871                                               5 /* info level */, &domain_name_pol,
2872                                               &domain_sid);
2873         if (NT_STATUS_IS_ERR(nt_status)) {
2874                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2875                         nt_errstr(nt_status)));
2876                 return -1;
2877         }
2878
2879
2880
2881
2882         /* There should be actually query info level 3 (following nt serv behaviour),
2883            but I still don't know if it's _really_ necessary */
2884                         
2885         /*
2886          * Store the password in secrets db
2887          */
2888
2889         if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2890                                                    wks_info.uni_lan_grp.uni_str_len, opt_password,
2891                                                    *domain_sid)) {
2892                 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2893                 return -1;
2894         }
2895         
2896         /*
2897          * Close the pipes and clean up
2898          */
2899          
2900         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2901         if (NT_STATUS_IS_ERR(nt_status)) {
2902                 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2903                         nt_errstr(nt_status)));
2904                 return -1;
2905         }
2906
2907         if (cli->nt_pipe_fnum)
2908                 cli_nt_session_close(cli);
2909          
2910         talloc_destroy(mem_ctx);
2911          
2912         DEBUG(0, ("Success!\n"));
2913         return 0;
2914 }
2915
2916 /**
2917  * Revoke trust relationship to the remote domain
2918  *
2919  * @param argc standard argc
2920  * @param argv standard argv without initial components
2921  *
2922  * @return Integer status (0 means success)
2923  **/
2924
2925 static int rpc_trustdom_revoke(int argc, const char **argv)
2926 {
2927         char* domain_name;
2928
2929         if (argc < 1) return -1;
2930         
2931         /* generate upper cased domain name */
2932         domain_name = smb_xstrdup(argv[0]);
2933         strupper_m(domain_name);
2934
2935         /* delete password of the trust */
2936         if (!trusted_domain_password_delete(domain_name)) {
2937                 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2938                           domain_name));
2939                 return -1;
2940         };
2941         
2942         return 0;
2943 }
2944
2945 /**
2946  * Usage for 'net rpc trustdom' command
2947  *
2948  * @param argc standard argc
2949  * @param argv standard argv without inital components
2950  *
2951  * @return Integer status returned to shell
2952  **/
2953  
2954 static int rpc_trustdom_usage(int argc, const char **argv)
2955 {
2956         d_printf("  net rpc trustdom add \t\t add trusting domain's account\n");
2957         d_printf("  net rpc trustdom del \t\t delete trusting domain's account\n");
2958         d_printf("  net rpc trustdom establish \t establish relationship to trusted domain\n");
2959         d_printf("  net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2960         d_printf("  net rpc trustdom list \t show current interdomain trust relationships\n");
2961         return -1;
2962 }
2963
2964
2965 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, 
2966                                      const char *domain_name, 
2967                                      struct cli_state *cli, TALLOC_CTX *mem_ctx,
2968                                      int argc, const char **argv)
2969 {
2970         fstring str_sid;
2971         sid_to_string(str_sid, domain_sid);
2972         d_printf("%s\n", str_sid);
2973         return NT_STATUS_OK;
2974 }
2975
2976
2977 static int rpc_trustdom_list(int argc, const char **argv)
2978 {
2979         /* common variables */
2980         TALLOC_CTX* mem_ctx;
2981         struct cli_state *cli, *remote_cli;
2982         NTSTATUS nt_status;
2983         const char *domain_name = NULL;
2984         DOM_SID *queried_dom_sid;
2985         fstring ascii_sid, padding;
2986         int ascii_dom_name_len;
2987         POLICY_HND connect_hnd;
2988         
2989         /* trusted domains listing variables */
2990         unsigned int num_domains, enum_ctx = 0;
2991         int i, pad_len, col_len = 20;
2992         DOM_SID *domain_sids;
2993         char **trusted_dom_names;
2994         fstring pdc_name;
2995         char *dummy;
2996         
2997         /* trusting domains listing variables */
2998         POLICY_HND domain_hnd;
2999         char **trusting_dom_names;
3000         uint32 *trusting_dom_rids;
3001         
3002         /*
3003          * Listing trusted domains (stored in secrets.tdb, if local)
3004          */
3005
3006         mem_ctx = talloc_init("trust relationships listing");
3007
3008         /*
3009          * set domain and pdc name to local samba server (default)
3010          * or to remote one given in command line
3011          */
3012         
3013         if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
3014                 domain_name = opt_workgroup;
3015                 opt_target_workgroup = opt_workgroup;
3016         } else {
3017                 fstrcpy(pdc_name, global_myname());
3018                 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
3019                 opt_target_workgroup = domain_name;
3020         };
3021
3022         /* open \PIPE\lsarpc and open policy handle */
3023         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
3024                 DEBUG(0, ("Couldn't connect to domain controller\n"));
3025                 return -1;
3026         };
3027
3028         if (!cli_nt_session_open(cli, PI_LSARPC)) {
3029                 DEBUG(0, ("Could not initialise lsa pipe\n"));
3030                 return -1;
3031         };
3032
3033         nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
3034                                         &connect_hnd);
3035         if (NT_STATUS_IS_ERR(nt_status)) {
3036                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
3037                         nt_errstr(nt_status)));
3038                 return -1;
3039         };
3040         
3041         /* query info level 5 to obtain sid of a domain being queried */
3042         nt_status = cli_lsa_query_info_policy(
3043                 cli, mem_ctx, &connect_hnd, 5 /* info level */, 
3044                 &dummy, &queried_dom_sid);
3045
3046         if (NT_STATUS_IS_ERR(nt_status)) {
3047                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
3048                         nt_errstr(nt_status)));
3049                 return -1;
3050         }
3051                 
3052         /*
3053          * Keep calling LsaEnumTrustdom over opened pipe until
3054          * the end of enumeration is reached
3055          */
3056          
3057         d_printf("Trusted domains list:\n\n");
3058
3059         do {
3060                 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
3061                                                    &num_domains,
3062                                                    &trusted_dom_names, &domain_sids);
3063                 
3064                 if (NT_STATUS_IS_ERR(nt_status)) {
3065                         DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
3066                                 nt_errstr(nt_status)));
3067                         return -1;
3068                 };
3069                 
3070                 for (i = 0; i < num_domains; i++) {
3071                         /* convert sid into ascii string */
3072                         sid_to_string(ascii_sid, &(domain_sids[i]));
3073                 
3074                         /* calculate padding space for d_printf to look nicer */
3075                         pad_len = col_len - strlen(trusted_dom_names[i]);
3076                         padding[pad_len] = 0;
3077                         do padding[--pad_len] = ' '; while (pad_len);
3078                         
3079                         d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
3080                 };
3081                 
3082                 /*
3083                  * in case of no trusted domains say something rather
3084                  * than just display blank line
3085                  */
3086                 if (!num_domains) d_printf("none\n");
3087
3088         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
3089
3090         /* close this connection before doing next one */
3091         nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
3092         if (NT_STATUS_IS_ERR(nt_status)) {
3093                 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
3094                         nt_errstr(nt_status)));
3095                 return -1;
3096         };
3097         
3098         cli_nt_session_close(cli);
3099
3100         /*
3101          * Listing trusting domains (stored in passdb backend, if local)
3102          */
3103         
3104         d_printf("\nTrusting domains list:\n\n");
3105
3106         /*
3107          * Open \PIPE\samr and get needed policy handles
3108          */
3109         if (!cli_nt_session_open(cli, PI_SAMR)) {
3110                 DEBUG(0, ("Could not initialise samr pipe\n"));
3111                 return -1;
3112         };
3113         
3114         /* SamrConnect */
3115         nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
3116                                                                  &connect_hnd);
3117         if (!NT_STATUS_IS_OK(nt_status)) {
3118                 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
3119                         nt_errstr(nt_status)));
3120                 return -1;
3121         };
3122         
3123         /* SamrOpenDomain - we have to open domain policy handle in order to be
3124            able to enumerate accounts*/
3125         nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
3126                                          SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
3127                                          queried_dom_sid, &domain_hnd);                                                                  
3128         if (!NT_STATUS_IS_OK(nt_status)) {
3129                 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
3130                         nt_errstr(nt_status)));
3131                 return -1;
3132         };
3133         
3134         /*
3135          * perform actual enumeration
3136          */
3137          
3138         enum_ctx = 0;   /* reset enumeration context from last enumeration */
3139         do {
3140                         
3141                 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
3142                                                     &enum_ctx, ACB_DOMTRUST, 0xffff,
3143                                                     &trusting_dom_names, &trusting_dom_rids,
3144                                                     &num_domains);
3145                 if (NT_STATUS_IS_ERR(nt_status)) {
3146                         DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
3147                                 nt_errstr(nt_status)));
3148                         return -1;
3149                 };
3150                 
3151                 for (i = 0; i < num_domains; i++) {
3152
3153                         /*
3154                          * get each single domain's sid (do we _really_ need this ?):
3155                          *  1) connect to domain's pdc
3156                          *  2) query the pdc for domain's sid
3157                          */
3158
3159                         /* get rid of '$' tail */
3160                         ascii_dom_name_len = strlen(trusting_dom_names[i]);
3161                         if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
3162                                 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
3163                         
3164                         /* calculate padding space for d_printf to look nicer */
3165                         pad_len = col_len - strlen(trusting_dom_names[i]);
3166                         padding[pad_len] = 0;
3167                         do padding[--pad_len] = ' '; while (pad_len);
3168
3169                         /* set opt_* variables to remote domain */
3170                         strupper_m(trusting_dom_names[i]);
3171                         opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
3172                         opt_target_workgroup = opt_workgroup;
3173                         
3174                         d_printf("%s%s", trusting_dom_names[i], padding);
3175                         
3176                         /* connect to remote domain controller */
3177                         remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
3178                         if (remote_cli) {                       
3179                                 /* query for domain's sid */
3180                                 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
3181                                         d_printf("couldn't get domain's sid\n");
3182
3183                                 cli_shutdown(remote_cli);
3184                         
3185                         } else {
3186                                 d_printf("domain controller is not responding\n");
3187                         };
3188                 };
3189                 
3190                 if (!num_domains) d_printf("none\n");
3191                 
3192         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
3193
3194         /* close opened samr and domain policy handles */
3195         nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
3196         if (!NT_STATUS_IS_OK(nt_status)) {
3197                 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
3198         };
3199         
3200         nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
3201         if (!NT_STATUS_IS_OK(nt_status)) {
3202                 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
3203         };
3204         
3205         /* close samr pipe and connection to IPC$ */
3206         cli_nt_session_close(cli);
3207         cli_shutdown(cli);
3208
3209         talloc_destroy(mem_ctx);         
3210         return 0;
3211 }
3212
3213 /**
3214  * Entrypoint for 'net rpc trustdom' code
3215  *
3216  * @param argc standard argc
3217  * @param argv standard argv without initial components
3218  *
3219  * @return Integer status (0 means success)
3220  */
3221
3222 static int rpc_trustdom(int argc, const char **argv)
3223 {
3224         struct functable func[] = {
3225                 {"add", rpc_trustdom_add},
3226                 {"del", rpc_trustdom_del},
3227                 {"establish", rpc_trustdom_establish},
3228                 {"revoke", rpc_trustdom_revoke},
3229                 {"help", rpc_trustdom_usage},
3230                 {"list", rpc_trustdom_list},
3231                 {NULL, NULL}
3232         };
3233
3234         if (argc == 0) {
3235                 rpc_trustdom_usage(argc, argv);
3236                 return -1;
3237         }
3238
3239         return (net_run_function(argc, argv, func, rpc_user_usage));
3240 }
3241
3242 /**
3243  * Check if a server will take rpc commands
3244  * @param flags Type of server to connect to (PDC, DMB, localhost)
3245  *              if the host is not explicitly specified
3246  * @return  BOOL (true means rpc supported)
3247  */
3248 BOOL net_rpc_check(unsigned flags)
3249 {
3250         struct cli_state cli;
3251         BOOL ret = False;
3252         struct in_addr server_ip;
3253         char *server_name = NULL;
3254
3255         /* flags (i.e. server type) may depend on command */
3256         if (!net_find_server(flags, &server_ip, &server_name))
3257                 return False;
3258
3259         ZERO_STRUCT(cli);
3260         if (cli_initialise(&cli) == False)
3261                 return False;
3262
3263         if (!cli_connect(&cli, server_name, &server_ip))
3264                 goto done;
3265         if (!attempt_netbios_session_request(&cli, global_myname(), 
3266                                              server_name, &server_ip))
3267                 goto done;
3268         if (!cli_negprot(&cli))
3269                 goto done;
3270         if (cli.protocol < PROTOCOL_NT1)
3271                 goto done;
3272
3273         ret = True;
3274  done:
3275         cli_shutdown(&cli);
3276         return ret;
3277 }
3278
3279 /* dump sam database via samsync rpc calls */
3280 static int rpc_samdump(int argc, const char **argv) {
3281         return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
3282                                argc, argv);
3283 }
3284
3285 /* syncronise sam database via samsync rpc calls */
3286 static int rpc_vampire(int argc, const char **argv) {
3287         return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
3288                                argc, argv);
3289 }
3290 /****************************************************************************/
3291
3292
3293 /** 
3294  * Basic usage function for 'net rpc'
3295  * @param argc  Standard main() style argc
3296  * @param argv  Standard main() style argv.  Initial components are already
3297  *              stripped
3298  **/
3299
3300 int net_rpc_usage(int argc, const char **argv) 
3301 {
3302         d_printf("  net rpc info \t\t\tshow basic info about a domain \n");
3303         d_printf("  net rpc join \t\t\tto join a domain \n");
3304         d_printf("  net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
3305         d_printf("  net rpc testjoin \t\ttests that a join is valid\n");
3306         d_printf("  net rpc user \t\t\tto add, delete and list users\n");
3307         d_printf("  net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
3308         d_printf("  net rpc group \t\tto list groups\n");
3309         d_printf("  net rpc share \t\tto add, delete, and list shares\n");
3310         d_printf("  net rpc file \t\t\tto list open files\n");
3311         d_printf("  net rpc changetrustpw \tto change the trust account password\n");
3312         d_printf("  net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
3313         d_printf("  net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
3314         d_printf("  net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
3315         d_printf("  net rpc trustdom \t\tto create trusting domain's account\n"
3316                  "\t\t\t\t\tor establish trust\n");
3317         d_printf("  net rpc abortshutdown \tto abort the shutdown of a remote server\n");
3318         d_printf("  net rpc shutdown \t\tto shutdown a remote server\n");
3319         d_printf("\n");
3320         d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
3321         d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
3322         d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
3323         d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
3324         d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
3325         return -1;
3326 }
3327
3328
3329 /**
3330  * Help function for 'net rpc'.  Calls command specific help if requested
3331  * or displays usage of net rpc
3332  * @param argc  Standard main() style argc
3333  * @param argv  Standard main() style argv.  Initial components are already
3334  *              stripped
3335  **/
3336
3337 int net_rpc_help(int argc, const char **argv)
3338 {
3339         struct functable func[] = {
3340                 {"join", rpc_join_usage},
3341                 {"user", rpc_user_usage},
3342                 {"group", rpc_group_usage},
3343                 {"share", rpc_share_usage},
3344                 /*{"changetrustpw", rpc_changetrustpw_usage}, */
3345                 {"trustdom", rpc_trustdom_usage},
3346                 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
3347                 /*{"shutdown", rpc_shutdown_usage}, */
3348                 {NULL, NULL}
3349         };
3350
3351         if (argc == 0) {
3352                 net_rpc_usage(argc, argv);
3353                 return -1;
3354         }
3355
3356         return (net_run_function(argc, argv, func, rpc_user_usage));
3357 }
3358
3359
3360 /** 
3361  * 'net rpc' entrypoint.
3362  * @param argc  Standard main() style argc
3363  * @param argv  Standard main() style argv.  Initial components are already
3364  *              stripped
3365  **/
3366
3367 int net_rpc(int argc, const char **argv)
3368 {
3369         struct functable func[] = {
3370                 {"info", net_rpc_info},
3371                 {"join", net_rpc_join},
3372                 {"oldjoin", net_rpc_oldjoin},
3373                 {"testjoin", net_rpc_testjoin},
3374                 {"user", net_rpc_user},
3375                 {"password", rpc_user_password},
3376                 {"group", net_rpc_group},
3377                 {"share", net_rpc_share},
3378                 {"file", net_rpc_file},
3379                 {"changetrustpw", net_rpc_changetrustpw},
3380                 {"trustdom", rpc_trustdom},
3381                 {"abortshutdown", rpc_shutdown_abort},
3382                 {"shutdown", rpc_shutdown},
3383                 {"samdump", rpc_samdump},
3384                 {"vampire", rpc_vampire},
3385                 {"getsid", net_rpc_getsid},
3386                 {"help", net_rpc_help},
3387                 {NULL, NULL}
3388         };
3389         return net_run_function(argc, argv, func, net_rpc_usage);
3390 }