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