r19797: Convert the remaining pipes to the "new" unique out ptr handling
[samba.git] / source / utils / net_rpc.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4    Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5    Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6    Copyright (C) 2004 Guenther Deschner (gd@samba.org)
7    Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8    Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
23  
24 #include "includes.h"
25 #include "utils/net.h"
26
27 static int net_mode_share;
28
29 /**
30  * @file net_rpc.c
31  *
32  * @brief RPC based subcommands for the 'net' utility.
33  *
34  * This file should contain much of the functionality that used to
35  * be found in rpcclient, execpt that the commands should change 
36  * less often, and the fucntionality should be sane (the user is not 
37  * expected to know a rid/sid before they conduct an operation etc.)
38  *
39  * @todo Perhaps eventually these should be split out into a number
40  * of files, as this could get quite big.
41  **/
42
43
44 /**
45  * Many of the RPC functions need the domain sid.  This function gets
46  *  it at the start of every run 
47  *
48  * @param cli A cli_state already connected to the remote machine
49  *
50  * @return The Domain SID of the remote machine.
51  **/
52
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
54                                    DOM_SID **domain_sid, char **domain_name)
55 {
56         struct rpc_pipe_client *lsa_pipe;
57         POLICY_HND pol;
58         NTSTATUS result = NT_STATUS_OK;
59         uint32 info_class = 5;
60         
61         lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
62         if (!lsa_pipe) {
63                 d_fprintf(stderr, "Could not initialise lsa pipe\n");
64                 return result;
65         }
66         
67         result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False, 
68                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
69                                      &pol);
70         if (!NT_STATUS_IS_OK(result)) {
71                 d_fprintf(stderr, "open_policy failed: %s\n",
72                           nt_errstr(result));
73                 return result;
74         }
75
76         result = rpccli_lsa_query_info_policy(lsa_pipe, mem_ctx, &pol,
77                                               info_class, domain_name,
78                                               domain_sid);
79         if (!NT_STATUS_IS_OK(result)) {
80                 d_fprintf(stderr, "lsaquery failed: %s\n",
81                           nt_errstr(result));
82                 return result;
83         }
84
85         rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
86         cli_rpc_pipe_close(lsa_pipe);
87
88         return NT_STATUS_OK;
89 }
90
91 /**
92  * Run a single RPC command, from start to finish.
93  *
94  * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
95  * @param conn_flag a NET_FLAG_ combination.  Passed to 
96  *                   net_make_ipc_connection.
97  * @param argc  Standard main() style argc
98  * @param argc  Standard main() style argv.  Initial components are already
99  *              stripped
100  * @return A shell status integer (0 for success)
101  */
102
103 int run_rpc_command(struct cli_state *cli_arg,
104                         const int pipe_idx,
105                         int conn_flags,
106                         rpc_command_fn fn,
107                         int argc,
108                         const char **argv) 
109 {
110         struct cli_state *cli = NULL;
111         struct rpc_pipe_client *pipe_hnd = NULL;
112         TALLOC_CTX *mem_ctx;
113         NTSTATUS nt_status;
114         DOM_SID *domain_sid;
115         char *domain_name;
116
117         /* make use of cli_state handed over as an argument, if possible */
118         if (!cli_arg) {
119                 cli = net_make_ipc_connection(conn_flags);
120         } else {
121                 cli = cli_arg;
122         }
123
124         if (!cli) {
125                 return -1;
126         }
127
128         /* Create mem_ctx */
129         
130         if (!(mem_ctx = talloc_init("run_rpc_command"))) {
131                 DEBUG(0, ("talloc_init() failed\n"));
132                 cli_shutdown(cli);
133                 return -1;
134         }
135         
136         nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
137                                               &domain_name);
138         if (!NT_STATUS_IS_OK(nt_status)) {
139                 cli_shutdown(cli);
140                 return -1;
141         }
142
143         if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
144                 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
145                         /* Always try and create an schannel netlogon pipe. */
146                         pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
147                                                         PIPE_AUTH_LEVEL_PRIVACY,
148                                                         domain_name,
149                                                         &nt_status);
150                         if (!pipe_hnd) {
151                                 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
152                                         nt_errstr(nt_status) ));
153                                 cli_shutdown(cli);
154                                 return -1;
155                         }
156                 } else {
157                         pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
158                         if (!pipe_hnd) {
159                                 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
160                                         cli_get_pipe_name(pipe_idx),
161                                         nt_errstr(nt_status) ));
162                                 cli_shutdown(cli);
163                                 return -1;
164                         }
165                 }
166         }
167         
168         nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
169         
170         if (!NT_STATUS_IS_OK(nt_status)) {
171                 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
172         } else {
173                 DEBUG(5, ("rpc command function succedded\n"));
174         }
175                 
176         if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
177                 if (pipe_hnd) {
178                         cli_rpc_pipe_close(pipe_hnd);
179                 }
180         }
181
182         /* close the connection only if it was opened here */
183         if (!cli_arg) {
184                 cli_shutdown(cli);
185         }
186         
187         talloc_destroy(mem_ctx);
188         return (!NT_STATUS_IS_OK(nt_status));
189 }
190
191 /** 
192  * Force a change of the trust acccount password.
193  *
194  * All parameters are provided by the run_rpc_command function, except for
195  * argc, argv which are passes through. 
196  *
197  * @param domain_sid The domain sid aquired from the remote server
198  * @param cli A cli_state connected to the server.
199  * @param mem_ctx Talloc context, destoyed on compleation of the function.
200  * @param argc  Standard main() style argc
201  * @param argc  Standard main() style argv.  Initial components are already
202  *              stripped
203  *
204  * @return Normal NTSTATUS return.
205  **/
206
207 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
208                                         const char *domain_name, 
209                                         struct cli_state *cli,
210                                         struct rpc_pipe_client *pipe_hnd,
211                                         TALLOC_CTX *mem_ctx, 
212                                         int argc,
213                                         const char **argv)
214 {
215         
216         return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
217 }
218
219 /** 
220  * Force a change of the trust acccount password.
221  *
222  * @param argc  Standard main() style argc
223  * @param argc  Standard main() style argv.  Initial components are already
224  *              stripped
225  *
226  * @return A shell status integer (0 for success)
227  **/
228
229 int net_rpc_changetrustpw(int argc, const char **argv) 
230 {
231         return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, 
232                                rpc_changetrustpw_internals,
233                                argc, argv);
234 }
235
236 /** 
237  * Join a domain, the old way.
238  *
239  * This uses 'machinename' as the inital password, and changes it. 
240  *
241  * The password should be created with 'server manager' or equiv first.
242  *
243  * All parameters are provided by the run_rpc_command function, except for
244  * argc, argv which are passes through. 
245  *
246  * @param domain_sid The domain sid aquired from the remote server
247  * @param cli A cli_state connected to the server.
248  * @param mem_ctx Talloc context, destoyed on compleation of the function.
249  * @param argc  Standard main() style argc
250  * @param argc  Standard main() style argv.  Initial components are already
251  *              stripped
252  *
253  * @return Normal NTSTATUS return.
254  **/
255
256 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
257                                         const char *domain_name, 
258                                         struct cli_state *cli, 
259                                         struct rpc_pipe_client *pipe_hnd,
260                                         TALLOC_CTX *mem_ctx, 
261                                         int argc,
262                                         const char **argv)
263 {
264         
265         fstring trust_passwd;
266         unsigned char orig_trust_passwd_hash[16];
267         NTSTATUS result;
268         uint32 sec_channel_type;
269
270         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
271         if (!pipe_hnd) {
272                 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
273                         "error was %s\n",
274                         cli->desthost,
275                         nt_errstr(result) ));
276                 return result;
277         }
278
279         /* 
280            check what type of join - if the user want's to join as
281            a BDC, the server must agree that we are a BDC.
282         */
283         if (argc >= 0) {
284                 sec_channel_type = get_sec_channel_type(argv[0]);
285         } else {
286                 sec_channel_type = get_sec_channel_type(NULL);
287         }
288         
289         fstrcpy(trust_passwd, global_myname());
290         strlower_m(trust_passwd);
291
292         /*
293          * Machine names can be 15 characters, but the max length on
294          * a password is 14.  --jerry
295          */
296
297         trust_passwd[14] = '\0';
298
299         E_md4hash(trust_passwd, orig_trust_passwd_hash);
300
301         result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
302                                               orig_trust_passwd_hash,
303                                               sec_channel_type);
304
305         if (NT_STATUS_IS_OK(result))
306                 printf("Joined domain %s.\n",opt_target_workgroup);
307
308
309         if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
310                 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
311                 result = NT_STATUS_UNSUCCESSFUL;
312         }
313
314         return result;
315 }
316
317 /** 
318  * Join a domain, the old way.
319  *
320  * @param argc  Standard main() style argc
321  * @param argc  Standard main() style argv.  Initial components are already
322  *              stripped
323  *
324  * @return A shell status integer (0 for success)
325  **/
326
327 static int net_rpc_perform_oldjoin(int argc, const char **argv)
328 {
329         return run_rpc_command(NULL, PI_NETLOGON, 
330                                NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, 
331                                rpc_oldjoin_internals,
332                                argc, argv);
333 }
334
335 /** 
336  * Join a domain, the old way.  This function exists to allow
337  * the message to be displayed when oldjoin was explicitly 
338  * requested, but not when it was implied by "net rpc join"
339  *
340  * @param argc  Standard main() style argc
341  * @param argc  Standard main() style argv.  Initial components are already
342  *              stripped
343  *
344  * @return A shell status integer (0 for success)
345  **/
346
347 static int net_rpc_oldjoin(int argc, const char **argv) 
348 {
349         int rc = net_rpc_perform_oldjoin(argc, argv);
350
351         if (rc) {
352                 d_fprintf(stderr, "Failed to join domain\n");
353         }
354
355         return rc;
356 }
357
358 /** 
359  * Basic usage function for 'net rpc join'
360  * @param argc  Standard main() style argc
361  * @param argc  Standard main() style argv.  Initial components are already
362  *              stripped
363  **/
364
365 static int rpc_join_usage(int argc, const char **argv) 
366 {       
367         d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
368                  "\t to join a domain with admin username & password\n"\
369                  "\t\t password will be prompted if needed and none is specified\n"\
370                  "\t <type> can be (default MEMBER)\n"\
371                  "\t\t BDC - Join as a BDC\n"\
372                  "\t\t PDC - Join as a PDC\n"\
373                  "\t\t MEMBER - Join as a MEMBER server\n");
374
375         net_common_flags_usage(argc, argv);
376         return -1;
377 }
378
379 /** 
380  * 'net rpc join' entrypoint.
381  * @param argc  Standard main() style argc
382  * @param argc  Standard main() style argv.  Initial components are already
383  *              stripped
384  *
385  * Main 'net_rpc_join()' (where the admain username/password is used) is 
386  * in net_rpc_join.c
387  * Try to just change the password, but if that doesn't work, use/prompt
388  * for a username/password.
389  **/
390
391 int net_rpc_join(int argc, const char **argv) 
392 {
393         if (lp_server_role() == ROLE_STANDALONE) {
394                 d_printf("cannot join as standalone machine\n");
395                 return -1;
396         }
397
398         if (strlen(global_myname()) > 15) {
399                 d_printf("Our netbios name can be at most 15 chars long, "
400                          "\"%s\" is %u chars long\n",
401                          global_myname(), (unsigned int)strlen(global_myname()));
402                 return -1;
403         }
404
405         if ((net_rpc_perform_oldjoin(argc, argv) == 0))
406                 return 0;
407         
408         return net_rpc_join_newstyle(argc, argv);
409 }
410
411 /** 
412  * display info about a rpc domain
413  *
414  * All parameters are provided by the run_rpc_command function, except for
415  * argc, argv which are passed through. 
416  *
417  * @param domain_sid The domain sid acquired from the remote server
418  * @param cli A cli_state connected to the server.
419  * @param mem_ctx Talloc context, destoyed on completion of the function.
420  * @param argc  Standard main() style argc
421  * @param argv  Standard main() style argv.  Initial components are already
422  *              stripped
423  *
424  * @return Normal NTSTATUS return.
425  **/
426
427 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
428                         const char *domain_name, 
429                         struct cli_state *cli,
430                         struct rpc_pipe_client *pipe_hnd,
431                         TALLOC_CTX *mem_ctx,
432                         int argc,
433                         const char **argv)
434 {
435         POLICY_HND connect_pol, domain_pol;
436         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
437         SAM_UNK_CTR ctr;
438         fstring sid_str;
439
440         sid_to_string(sid_str, domain_sid);
441
442         /* Get sam policy handle */     
443         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
444                                   &connect_pol);
445         if (!NT_STATUS_IS_OK(result)) {
446                 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
447                 goto done;
448         }
449         
450         /* Get domain policy handle */
451         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
452                                       MAXIMUM_ALLOWED_ACCESS,
453                                       domain_sid, &domain_pol);
454         if (!NT_STATUS_IS_OK(result)) {
455                 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
456                 goto done;
457         }
458
459         ZERO_STRUCT(ctr);
460         result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
461                                          2, &ctr);
462         if (NT_STATUS_IS_OK(result)) {
463                 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
464                 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
465                 d_printf("Domain SID: %s\n", sid_str);
466                 d_printf("Sequence number: %llu\n", (unsigned long long)ctr.info.inf2.seq_num);
467                 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
468                 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
469                 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
470                 talloc_destroy(ctx);
471         }
472
473  done:
474         return result;
475 }
476
477 /** 
478  * 'net rpc info' entrypoint.
479  * @param argc  Standard main() style argc
480  * @param argc  Standard main() style argv.  Initial components are already
481  *              stripped
482  **/
483
484 int net_rpc_info(int argc, const char **argv) 
485 {
486         return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC, 
487                                rpc_info_internals,
488                                argc, argv);
489 }
490
491 /** 
492  * Fetch domain SID into the local secrets.tdb
493  *
494  * All parameters are provided by the run_rpc_command function, except for
495  * argc, argv which are passes through. 
496  *
497  * @param domain_sid The domain sid acquired from the remote server
498  * @param cli A cli_state connected to the server.
499  * @param mem_ctx Talloc context, destoyed on completion of the function.
500  * @param argc  Standard main() style argc
501  * @param argv  Standard main() style argv.  Initial components are already
502  *              stripped
503  *
504  * @return Normal NTSTATUS return.
505  **/
506
507 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
508                         const char *domain_name, 
509                         struct cli_state *cli,
510                         struct rpc_pipe_client *pipe_hnd,
511                         TALLOC_CTX *mem_ctx,
512                         int argc,
513                         const char **argv)
514 {
515         fstring sid_str;
516
517         sid_to_string(sid_str, domain_sid);
518         d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
519                  sid_str, domain_name);
520
521         if (!secrets_store_domain_sid(domain_name, domain_sid)) {
522                 DEBUG(0,("Can't store domain SID\n"));
523                 return NT_STATUS_UNSUCCESSFUL;
524         }
525
526         return NT_STATUS_OK;
527 }
528
529 /** 
530  * 'net rpc getsid' entrypoint.
531  * @param argc  Standard main() style argc
532  * @param argc  Standard main() style argv.  Initial components are already
533  *              stripped
534  **/
535
536 int net_rpc_getsid(int argc, const char **argv) 
537 {
538         return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, 
539                                rpc_getsid_internals,
540                                argc, argv);
541 }
542
543 /****************************************************************************/
544
545 /**
546  * Basic usage function for 'net rpc user'
547  * @param argc  Standard main() style argc.
548  * @param argv  Standard main() style argv.  Initial components are already
549  *              stripped.
550  **/
551
552 static int rpc_user_usage(int argc, const char **argv)
553 {
554         return net_help_user(argc, argv);
555 }
556
557 /** 
558  * Add a new user to a remote RPC server
559  *
560  * All parameters are provided by the run_rpc_command function, except for
561  * argc, argv which are passes through. 
562  *
563  * @param domain_sid The domain sid acquired from the remote server
564  * @param cli A cli_state connected to the server.
565  * @param mem_ctx Talloc context, destoyed on completion of the function.
566  * @param argc  Standard main() style argc
567  * @param argv  Standard main() style argv.  Initial components are already
568  *              stripped
569  *
570  * @return Normal NTSTATUS return.
571  **/
572
573 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
574                                 const char *domain_name, 
575                                 struct cli_state *cli,
576                                 struct rpc_pipe_client *pipe_hnd,
577                                 TALLOC_CTX *mem_ctx, 
578                                 int argc, const char **argv)
579 {
580         
581         POLICY_HND connect_pol, domain_pol, user_pol;
582         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
583         const char *acct_name;
584         uint32 acb_info;
585         uint32 unknown, user_rid;
586
587         if (argc < 1) {
588                 d_printf("User must be specified\n");
589                 rpc_user_usage(argc, argv);
590                 return NT_STATUS_OK;
591         }
592
593         acct_name = argv[0];
594
595         /* Get sam policy handle */
596         
597         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
598                                   &connect_pol);
599         if (!NT_STATUS_IS_OK(result)) {
600                 goto done;
601         }
602         
603         /* Get domain policy handle */
604         
605         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
606                                       MAXIMUM_ALLOWED_ACCESS,
607                                       domain_sid, &domain_pol);
608         if (!NT_STATUS_IS_OK(result)) {
609                 goto done;
610         }
611
612         /* Create domain user */
613
614         acb_info = ACB_NORMAL;
615         unknown = 0xe005000b; /* No idea what this is - a permission mask? */
616
617         result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
618                                           acct_name, acb_info, unknown,
619                                           &user_pol, &user_rid);
620         if (!NT_STATUS_IS_OK(result)) {
621                 goto done;
622         }
623
624         if (argc == 2) {
625
626                 uint32 *user_rids, num_rids, *name_types;
627                 uint32 flags = 0x000003e8; /* Unknown */
628                 SAM_USERINFO_CTR ctr;
629                 SAM_USER_INFO_24 p24;
630                 uchar pwbuf[516];
631
632                 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
633                                                   flags, 1, &acct_name,
634                                                   &num_rids, &user_rids,
635                                                   &name_types);
636
637                 if (!NT_STATUS_IS_OK(result)) {
638                         goto done;
639                 }
640
641                 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
642                                                MAXIMUM_ALLOWED_ACCESS,
643                                                user_rids[0], &user_pol);
644
645                 if (!NT_STATUS_IS_OK(result)) {
646                         goto done;
647                 }
648
649                 /* Set password on account */
650
651                 ZERO_STRUCT(ctr);
652                 ZERO_STRUCT(p24);
653
654                 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
655
656                 init_sam_user_info24(&p24, (char *)pwbuf,24);
657
658                 ctr.switch_value = 24;
659                 ctr.info.id24 = &p24;
660
661                 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24, 
662                                                &cli->user_session_key, &ctr);
663
664                 if (!NT_STATUS_IS_OK(result)) {
665                         d_fprintf(stderr, "Failed to set password for user %s - %s\n", 
666                                  acct_name, nt_errstr(result));
667
668                         result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
669
670                         if (!NT_STATUS_IS_OK(result)) {
671                                 d_fprintf(stderr, "Failed to delete user %s - %s\n", 
672                                          acct_name, nt_errstr(result));
673                                  return result;
674                         }
675                 }
676
677         }
678  done:
679         if (!NT_STATUS_IS_OK(result)) {
680                 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name, 
681                          nt_errstr(result));
682         } else {
683                 d_printf("Added user %s\n", acct_name);
684         }
685         return result;
686 }
687
688 /** 
689  * Add a new user to a remote RPC server
690  *
691  * @param argc  Standard main() style argc
692  * @param argv  Standard main() style argv.  Initial components are already
693  *              stripped
694  *
695  * @return A shell status integer (0 for success)
696  **/
697
698 static int rpc_user_add(int argc, const char **argv) 
699 {
700         return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
701                                argc, argv);
702 }
703
704 /** 
705  * Delete a user from a remote RPC server
706  *
707  * All parameters are provided by the run_rpc_command function, except for
708  * argc, argv which are passes through. 
709  *
710  * @param domain_sid The domain sid acquired from the remote server
711  * @param cli A cli_state connected to the server.
712  * @param mem_ctx Talloc context, destoyed on completion of the function.
713  * @param argc  Standard main() style argc
714  * @param argv  Standard main() style argv.  Initial components are already
715  *              stripped
716  *
717  * @return Normal NTSTATUS return.
718  **/
719
720 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid, 
721                                         const char *domain_name, 
722                                         struct cli_state *cli, 
723                                         struct rpc_pipe_client *pipe_hnd,
724                                         TALLOC_CTX *mem_ctx, 
725                                         int argc,
726                                         const char **argv)
727 {
728         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
729         POLICY_HND connect_pol, domain_pol, user_pol;
730
731         if (argc < 1) {
732                 d_printf("User must be specified\n");
733                 rpc_user_usage(argc, argv);
734                 return NT_STATUS_OK;
735         }
736         /* Get sam policy and domain handles */
737
738         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
739                                   &connect_pol);
740
741         if (!NT_STATUS_IS_OK(result)) {
742                 goto done;
743         }
744
745         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
746                                       MAXIMUM_ALLOWED_ACCESS,
747                                       domain_sid, &domain_pol);
748
749         if (!NT_STATUS_IS_OK(result)) {
750                 goto done;
751         }
752
753         /* Get handle on user */
754
755         {
756                 uint32 *user_rids, num_rids, *name_types;
757                 uint32 flags = 0x000003e8; /* Unknown */
758
759                 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
760                                                flags, 1, &argv[0],
761                                                &num_rids, &user_rids,
762                                                &name_types);
763
764                 if (!NT_STATUS_IS_OK(result)) {
765                         goto done;
766                 }
767
768                 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
769                                             MAXIMUM_ALLOWED_ACCESS,
770                                             user_rids[0], &user_pol);
771
772                 if (!NT_STATUS_IS_OK(result)) {
773                         goto done;
774                 }
775         }
776
777         /* Delete user */
778
779         result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
780
781         if (!NT_STATUS_IS_OK(result)) {
782                 goto done;
783         }
784
785         /* Display results */
786         if (!NT_STATUS_IS_OK(result)) {
787                 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
788         } else {
789                 d_printf("Deleted user account\n");
790         }
791
792  done:
793         return result;
794 }
795
796 /** 
797  * Rename a user on a remote RPC server
798  *
799  * All parameters are provided by the run_rpc_command function, except for
800  * argc, argv which are passes through. 
801  *
802  * @param domain_sid The domain sid acquired from the remote server
803  * @param cli A cli_state connected to the server.
804  * @param mem_ctx Talloc context, destoyed on completion of the function.
805  * @param argc  Standard main() style argc
806  * @param argv  Standard main() style argv.  Initial components are already
807  *              stripped
808  *
809  * @return Normal NTSTATUS return.
810  **/
811
812 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
813                                         const char *domain_name, 
814                                         struct cli_state *cli,
815                                         struct rpc_pipe_client *pipe_hnd,
816                                         TALLOC_CTX *mem_ctx, 
817                                         int argc,
818                                         const char **argv)
819 {
820         POLICY_HND connect_pol, domain_pol, user_pol;
821         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
822         uint32 info_level = 7;
823         const char *old_name, *new_name;
824         uint32 *user_rid;
825         uint32 flags = 0x000003e8; /* Unknown */
826         uint32 num_rids, *name_types;
827         uint32 num_names = 1;
828         const char **names;
829         SAM_USERINFO_CTR *user_ctr;
830         SAM_USERINFO_CTR ctr;
831         SAM_USER_INFO_7 info7;
832
833         if (argc != 2) {
834                 d_printf("Old and new username must be specified\n");
835                 rpc_user_usage(argc, argv);
836                 return NT_STATUS_OK;
837         }
838
839         old_name = argv[0];
840         new_name = argv[1];
841
842         ZERO_STRUCT(ctr);
843         ZERO_STRUCT(user_ctr);
844
845         /* Get sam policy handle */
846         
847         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
848                                   &connect_pol);
849         if (!NT_STATUS_IS_OK(result)) {
850                 goto done;
851         }
852         
853         /* Get domain policy handle */
854         
855         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
856                                       MAXIMUM_ALLOWED_ACCESS,
857                                       domain_sid, &domain_pol);
858         if (!NT_STATUS_IS_OK(result)) {
859                 goto done;
860         }
861
862         if ((names = TALLOC_ARRAY(mem_ctx, const char *, num_names)) == NULL) {
863                 result = NT_STATUS_NO_MEMORY;
864                 goto done;
865         }
866         names[0] = old_name;
867         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
868                                        flags, num_names, names,
869                                        &num_rids, &user_rid, &name_types);
870         if (!NT_STATUS_IS_OK(result)) {
871                 goto done;
872         }
873
874         /* Open domain user */
875         result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
876                                     MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
877
878         if (!NT_STATUS_IS_OK(result)) {
879                 goto done;
880         }
881
882         /* Query user info */
883         result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
884                                          info_level, &user_ctr);
885
886         if (!NT_STATUS_IS_OK(result)) {
887                 goto done;
888         }
889
890         ctr.switch_value = info_level;
891         ctr.info.id7 = &info7;
892
893         init_sam_user_info7(&info7, new_name);
894
895         /* Set new name */
896         result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
897                                        info_level, &cli->user_session_key, &ctr);
898
899         if (!NT_STATUS_IS_OK(result)) {
900                 goto done;
901         }
902
903  done:
904         if (!NT_STATUS_IS_OK(result)) {
905                 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name, 
906                          nt_errstr(result));
907         } else {
908                 d_printf("Renamed user from %s to %s\n", old_name, new_name);
909         }
910         return result;
911 }
912
913 /** 
914  * Rename a user on a remote RPC server
915  *
916  * @param argc  Standard main() style argc
917  * @param argv  Standard main() style argv.  Initial components are already
918  *              stripped
919  *
920  * @return A shell status integer (0 for success)
921  **/
922
923 static int rpc_user_rename(int argc, const char **argv) 
924 {
925         return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
926                                argc, argv);
927 }
928
929 /** 
930  * Delete a user from a remote RPC server
931  *
932  * @param argc  Standard main() style argc
933  * @param argv  Standard main() style argv.  Initial components are already
934  *              stripped
935  *
936  * @return A shell status integer (0 for success)
937  **/
938
939 static int rpc_user_delete(int argc, const char **argv) 
940 {
941         return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
942                                argc, argv);
943 }
944
945 /** 
946  * Set a password for a user on a remote RPC server
947  *
948  * All parameters are provided by the run_rpc_command function, except for
949  * argc, argv which are passes through. 
950  *
951  * @param domain_sid The domain sid acquired from the remote server
952  * @param cli A cli_state connected to the server.
953  * @param mem_ctx Talloc context, destoyed on completion of the function.
954  * @param argc  Standard main() style argc
955  * @param argv  Standard main() style argv.  Initial components are already
956  *              stripped
957  *
958  * @return Normal NTSTATUS return.
959  **/
960
961 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid, 
962                                         const char *domain_name, 
963                                         struct cli_state *cli, 
964                                         struct rpc_pipe_client *pipe_hnd,
965                                         TALLOC_CTX *mem_ctx, 
966                                         int argc,
967                                         const char **argv)
968 {
969         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
970         POLICY_HND connect_pol, domain_pol, user_pol;
971         SAM_USERINFO_CTR ctr;
972         SAM_USER_INFO_24 p24;
973         uchar pwbuf[516];
974         const char *user;
975         const char *new_password;
976         char *prompt = NULL;
977
978         if (argc < 1) {
979                 d_printf("User must be specified\n");
980                 rpc_user_usage(argc, argv);
981                 return NT_STATUS_OK;
982         }
983         
984         user = argv[0];
985
986         if (argv[1]) {
987                 new_password = argv[1];
988         } else {
989                 asprintf(&prompt, "Enter new password for %s:", user);
990                 new_password = getpass(prompt);
991                 SAFE_FREE(prompt);
992         }
993
994         /* Get sam policy and domain handles */
995
996         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
997                                   &connect_pol);
998
999         if (!NT_STATUS_IS_OK(result)) {
1000                 goto done;
1001         }
1002
1003         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1004                                       MAXIMUM_ALLOWED_ACCESS,
1005                                       domain_sid, &domain_pol);
1006
1007         if (!NT_STATUS_IS_OK(result)) {
1008                 goto done;
1009         }
1010
1011         /* Get handle on user */
1012
1013         {
1014                 uint32 *user_rids, num_rids, *name_types;
1015                 uint32 flags = 0x000003e8; /* Unknown */
1016
1017                 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1018                                                flags, 1, &user,
1019                                                &num_rids, &user_rids,
1020                                                &name_types);
1021
1022                 if (!NT_STATUS_IS_OK(result)) {
1023                         goto done;
1024                 }
1025
1026                 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1027                                             MAXIMUM_ALLOWED_ACCESS,
1028                                             user_rids[0], &user_pol);
1029
1030                 if (!NT_STATUS_IS_OK(result)) {
1031                         goto done;
1032                 }
1033         }
1034
1035         /* Set password on account */
1036
1037         ZERO_STRUCT(ctr);
1038         ZERO_STRUCT(p24);
1039
1040         encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1041
1042         init_sam_user_info24(&p24, (char *)pwbuf,24);
1043
1044         ctr.switch_value = 24;
1045         ctr.info.id24 = &p24;
1046
1047         result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24, 
1048                                        &cli->user_session_key, &ctr);
1049
1050         if (!NT_STATUS_IS_OK(result)) {
1051                 goto done;
1052         }
1053
1054         /* Display results */
1055
1056  done:
1057         return result;
1058
1059 }       
1060
1061 /** 
1062  * Set a user's password on a remote RPC server
1063  *
1064  * @param argc  Standard main() style argc
1065  * @param argv  Standard main() style argv.  Initial components are already
1066  *              stripped
1067  *
1068  * @return A shell status integer (0 for success)
1069  **/
1070
1071 static int rpc_user_password(int argc, const char **argv) 
1072 {
1073         return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1074                                argc, argv);
1075 }
1076
1077 /** 
1078  * List user's groups on a remote RPC server
1079  *
1080  * All parameters are provided by the run_rpc_command function, except for
1081  * argc, argv which are passes through. 
1082  *
1083  * @param domain_sid The domain sid acquired from the remote server
1084  * @param cli A cli_state connected to the server.
1085  * @param mem_ctx Talloc context, destoyed on completion of the function.
1086  * @param argc  Standard main() style argc
1087  * @param argv  Standard main() style argv.  Initial components are already
1088  *              stripped
1089  *
1090  * @return Normal NTSTATUS return.
1091  **/
1092
1093 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1094                         const char *domain_name, 
1095                         struct cli_state *cli,
1096                         struct rpc_pipe_client *pipe_hnd,
1097                         TALLOC_CTX *mem_ctx,
1098                         int argc,
1099                         const char **argv)
1100 {
1101         POLICY_HND connect_pol, domain_pol, user_pol;
1102         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1103         uint32 *rids, num_rids, *name_types, num_names;
1104         uint32 flags = 0x000003e8; /* Unknown */
1105         int i;
1106         char **names;
1107         DOM_GID *user_gids;
1108
1109         if (argc < 1) {
1110                 d_printf("User must be specified\n");
1111                 rpc_user_usage(argc, argv);
1112                 return NT_STATUS_OK;
1113         }
1114         /* Get sam policy handle */
1115         
1116         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1117                                   &connect_pol);
1118         if (!NT_STATUS_IS_OK(result)) goto done;
1119         
1120         /* Get domain policy handle */
1121         
1122         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1123                                       MAXIMUM_ALLOWED_ACCESS,
1124                                       domain_sid, &domain_pol);
1125         if (!NT_STATUS_IS_OK(result)) goto done;
1126
1127         /* Get handle on user */
1128
1129         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1130                                        flags, 1, &argv[0],
1131                                        &num_rids, &rids, &name_types);
1132
1133         if (!NT_STATUS_IS_OK(result)) goto done;
1134
1135         result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1136                                     MAXIMUM_ALLOWED_ACCESS,
1137                                     rids[0], &user_pol);
1138         if (!NT_STATUS_IS_OK(result)) goto done;
1139
1140         result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1141                                            &num_rids, &user_gids);
1142
1143         if (!NT_STATUS_IS_OK(result)) goto done;
1144
1145         /* Look up rids */
1146
1147         if (num_rids) {
1148                 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1149                         result = NT_STATUS_NO_MEMORY;
1150                         goto done;
1151                 }
1152
1153                 for (i = 0; i < num_rids; i++)
1154                         rids[i] = user_gids[i].g_rid;
1155
1156                 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1157                                               num_rids, rids,
1158                                               &num_names, &names, &name_types);
1159
1160                 if (!NT_STATUS_IS_OK(result)) {
1161                         goto done;
1162                 }
1163
1164                 /* Display results */
1165
1166                 for (i = 0; i < num_names; i++)
1167                         printf("%s\n", names[i]);
1168         }
1169  done:
1170         return result;
1171 }
1172
1173 /** 
1174  * List a user's groups from a remote RPC server
1175  *
1176  * @param argc  Standard main() style argc
1177  * @param argv  Standard main() style argv.  Initial components are already
1178  *              stripped
1179  *
1180  * @return A shell status integer (0 for success)
1181  **/
1182
1183 static int rpc_user_info(int argc, const char **argv) 
1184 {
1185         return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1186                                argc, argv);
1187 }
1188
1189 /** 
1190  * List users on a remote RPC server
1191  *
1192  * All parameters are provided by the run_rpc_command function, except for
1193  * argc, argv which are passes through. 
1194  *
1195  * @param domain_sid The domain sid acquired from the remote server
1196  * @param cli A cli_state connected to the server.
1197  * @param mem_ctx Talloc context, destoyed on completion of the function.
1198  * @param argc  Standard main() style argc
1199  * @param argv  Standard main() style argv.  Initial components are already
1200  *              stripped
1201  *
1202  * @return Normal NTSTATUS return.
1203  **/
1204
1205 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1206                                         const char *domain_name, 
1207                                         struct cli_state *cli,
1208                                         struct rpc_pipe_client *pipe_hnd,
1209                                         TALLOC_CTX *mem_ctx,
1210                                         int argc,
1211                                         const char **argv)
1212 {
1213         POLICY_HND connect_pol, domain_pol;
1214         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1215         uint32 start_idx=0, num_entries, i, loop_count = 0;
1216         SAM_DISPINFO_CTR ctr;
1217         SAM_DISPINFO_1 info1;
1218
1219         /* Get sam policy handle */
1220         
1221         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1222                                   &connect_pol);
1223         if (!NT_STATUS_IS_OK(result)) {
1224                 goto done;
1225         }
1226         
1227         /* Get domain policy handle */
1228         
1229         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1230                                       MAXIMUM_ALLOWED_ACCESS,
1231                                       domain_sid, &domain_pol);
1232         if (!NT_STATUS_IS_OK(result)) {
1233                 goto done;
1234         }
1235
1236         /* Query domain users */
1237         ZERO_STRUCT(ctr);
1238         ZERO_STRUCT(info1);
1239         ctr.sam.info1 = &info1;
1240         if (opt_long_list_entries)
1241                 d_printf("\nUser name             Comment"\
1242                          "\n-----------------------------\n");
1243         do {
1244                 fstring user, desc;
1245                 uint32 max_entries, max_size;
1246
1247                 get_query_dispinfo_params(
1248                         loop_count, &max_entries, &max_size);
1249
1250                 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1251                                                  &start_idx, 1, &num_entries,
1252                                                  max_entries, max_size, &ctr);
1253                 loop_count++;
1254
1255                 for (i = 0; i < num_entries; i++) {
1256                         unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1257                         if (opt_long_list_entries) 
1258                                 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1259                         
1260                         if (opt_long_list_entries)
1261                                 printf("%-21.21s %s\n", user, desc);
1262                         else
1263                                 printf("%s\n", user);
1264                 }
1265         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1266
1267  done:
1268         return result;
1269 }
1270
1271 /** 
1272  * 'net rpc user' entrypoint.
1273  * @param argc  Standard main() style argc
1274  * @param argc  Standard main() style argv.  Initial components are already
1275  *              stripped
1276  **/
1277
1278 int net_rpc_user(int argc, const char **argv) 
1279 {
1280         struct functable func[] = {
1281                 {"add", rpc_user_add},
1282                 {"info", rpc_user_info},
1283                 {"delete", rpc_user_delete},
1284                 {"password", rpc_user_password},
1285                 {"rename", rpc_user_rename},
1286                 {NULL, NULL}
1287         };
1288         
1289         if (argc == 0) {
1290                 return run_rpc_command(NULL,PI_SAMR, 0, 
1291                                        rpc_user_list_internals,
1292                                        argc, argv);
1293         }
1294
1295         return net_run_function(argc, argv, func, rpc_user_usage);
1296 }
1297
1298 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1299                                  struct rpc_sh_ctx *ctx,
1300                                  struct rpc_pipe_client *pipe_hnd,
1301                                  int argc, const char **argv)
1302 {
1303         return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1304                                        ctx->cli, pipe_hnd, mem_ctx,
1305                                        argc, argv);
1306 }
1307
1308 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1309                                  struct rpc_sh_ctx *ctx,
1310                                  struct rpc_pipe_client *pipe_hnd,
1311                                  int argc, const char **argv)
1312 {
1313         return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1314                                        ctx->cli, pipe_hnd, mem_ctx,
1315                                        argc, argv);
1316 }
1317
1318 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1319                                    struct rpc_sh_ctx *ctx,
1320                                    struct rpc_pipe_client *pipe_hnd,
1321                                    int argc, const char **argv,
1322                                    NTSTATUS (*fn)(
1323                                            TALLOC_CTX *mem_ctx,
1324                                            struct rpc_sh_ctx *ctx,
1325                                            struct rpc_pipe_client *pipe_hnd,
1326                                            const POLICY_HND *user_hnd,
1327                                            int argc, const char **argv))
1328                                            
1329 {
1330         POLICY_HND connect_pol, domain_pol, user_pol;
1331         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1332         DOM_SID sid;
1333         uint32 rid;
1334         enum lsa_SidType type;
1335
1336         if (argc == 0) {
1337                 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1338                 return NT_STATUS_INVALID_PARAMETER;
1339         }
1340
1341         ZERO_STRUCT(connect_pol);
1342         ZERO_STRUCT(domain_pol);
1343         ZERO_STRUCT(user_pol);
1344
1345         result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1346                                      NULL, NULL, &sid, &type);
1347         if (!NT_STATUS_IS_OK(result)) {
1348                 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1349                           nt_errstr(result));
1350                 goto done;
1351         }
1352
1353         if (type != SID_NAME_USER) {
1354                 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1355                           sid_type_lookup(type));
1356                 result = NT_STATUS_NO_SUCH_USER;
1357                 goto done;
1358         }
1359
1360         if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1361                 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1362                 result = NT_STATUS_NO_SUCH_USER;
1363                 goto done;
1364         }
1365
1366         result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1367                                      MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1368         if (!NT_STATUS_IS_OK(result)) {
1369                 goto done;
1370         }
1371
1372         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1373                                          MAXIMUM_ALLOWED_ACCESS,
1374                                          ctx->domain_sid, &domain_pol);
1375         if (!NT_STATUS_IS_OK(result)) {
1376                 goto done;
1377         }
1378
1379         result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1380                                        MAXIMUM_ALLOWED_ACCESS,
1381                                        rid, &user_pol);
1382         if (!NT_STATUS_IS_OK(result)) {
1383                 goto done;
1384         }
1385
1386         result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1387
1388  done:
1389         if (is_valid_policy_hnd(&user_pol)) {
1390                 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1391         }
1392         if (is_valid_policy_hnd(&domain_pol)) {
1393                 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1394         }
1395         if (is_valid_policy_hnd(&connect_pol)) {
1396                 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1397         }
1398         return result;
1399 }
1400
1401 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1402                                            struct rpc_sh_ctx *ctx,
1403                                            struct rpc_pipe_client *pipe_hnd,
1404                                            const POLICY_HND *user_hnd,
1405                                            int argc, const char **argv)
1406 {
1407         NTSTATUS result;
1408         SAM_USERINFO_CTR *ctr;
1409         SAM_USER_INFO_21 *info;
1410
1411         if (argc != 0) {
1412                 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1413                 return NT_STATUS_INVALID_PARAMETER;
1414         }
1415
1416         result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1417                                             21, &ctr);
1418         if (!NT_STATUS_IS_OK(result)) {
1419                 return result;
1420         }
1421
1422         info = ctr->info.id21;
1423
1424         d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1425                  info->group_rid);
1426
1427         return result;
1428 }
1429
1430 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1431                                  struct rpc_sh_ctx *ctx,
1432                                  struct rpc_pipe_client *pipe_hnd,
1433                                  int argc, const char **argv)
1434 {
1435         return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1436                                   rpc_sh_user_show_internals);
1437 }
1438
1439 #define FETCHSTR(name, rec) \
1440 do { if (strequal(ctx->thiscmd, name)) { \
1441         oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1442 } while (0);
1443
1444 #define SETSTR(name, rec, flag) \
1445 do { if (strequal(ctx->thiscmd, name)) { \
1446         init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1447         init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1448         usr->fields_present |= ACCT_##flag; } \
1449 } while (0);
1450
1451 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1452                                                struct rpc_sh_ctx *ctx,
1453                                                struct rpc_pipe_client *pipe_hnd,
1454                                                const POLICY_HND *user_hnd,
1455                                                int argc, const char **argv)
1456 {
1457         NTSTATUS result;
1458         SAM_USERINFO_CTR *ctr;
1459         SAM_USER_INFO_21 *usr;
1460         const char *username;
1461         const char *oldval = "";
1462
1463         if (argc > 1) {
1464                 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1465                           ctx->whoami);
1466                 return NT_STATUS_INVALID_PARAMETER;
1467         }
1468
1469         result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1470                                             21, &ctr);
1471         if (!NT_STATUS_IS_OK(result)) {
1472                 return result;
1473         }
1474
1475         usr = ctr->info.id21;
1476
1477         username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1478
1479         FETCHSTR("fullname", full_name);
1480         FETCHSTR("homedir", home_dir);
1481         FETCHSTR("homedrive", dir_drive);
1482         FETCHSTR("logonscript", logon_script);
1483         FETCHSTR("profilepath", profile_path);
1484         FETCHSTR("description", acct_desc);
1485
1486         if (argc == 0) {
1487                 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1488                 goto done;
1489         }
1490
1491         ZERO_STRUCTP(usr);
1492
1493         if (strcmp(argv[0], "NULL") == 0) {
1494                 argv[0] = "";
1495         }
1496
1497         SETSTR("fullname", full_name, FULL_NAME);
1498         SETSTR("homedir", home_dir, HOME_DIR);
1499         SETSTR("homedrive", dir_drive, HOME_DRIVE);
1500         SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1501         SETSTR("profilepath", profile_path, PROFILE);
1502         SETSTR("description", acct_desc, DESCRIPTION);
1503
1504         result = rpccli_samr_set_userinfo2(
1505                 pipe_hnd, mem_ctx, user_hnd, 21,
1506                 &pipe_hnd->cli->user_session_key, ctr);
1507
1508         d_printf("Set %s's %s from [%s] to [%s]\n", username,
1509                  ctx->thiscmd, oldval, argv[0]);
1510
1511  done:
1512
1513         return result;
1514 }
1515
1516 #define HANDLEFLG(name, rec) \
1517 do { if (strequal(ctx->thiscmd, name)) { \
1518         oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1519         if (newval) { \
1520                 newflags = oldflags | ACB_##rec; \
1521         } else { \
1522                 newflags = oldflags & ~ACB_##rec; \
1523         } } } while (0);
1524
1525 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1526                                      struct rpc_sh_ctx *ctx,
1527                                      struct rpc_pipe_client *pipe_hnd,
1528                                      int argc, const char **argv)
1529 {
1530         return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1531                                   rpc_sh_user_str_edit_internals);
1532 }
1533
1534 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1535                                                 struct rpc_sh_ctx *ctx,
1536                                                 struct rpc_pipe_client *pipe_hnd,
1537                                                 const POLICY_HND *user_hnd,
1538                                                 int argc, const char **argv)
1539 {
1540         NTSTATUS result;
1541         SAM_USERINFO_CTR *ctr;
1542         SAM_USER_INFO_21 *usr;
1543         const char *username;
1544         const char *oldval = "unknown";
1545         uint32 oldflags, newflags;
1546         BOOL newval;
1547
1548         if ((argc > 1) ||
1549             ((argc == 1) && !strequal(argv[0], "yes") &&
1550              !strequal(argv[0], "no"))) {
1551                 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1552                           ctx->whoami);
1553                 return NT_STATUS_INVALID_PARAMETER;
1554         }
1555
1556         newval = strequal(argv[0], "yes");
1557
1558         result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1559                                             21, &ctr);
1560         if (!NT_STATUS_IS_OK(result)) {
1561                 return result;
1562         }
1563
1564         usr = ctr->info.id21;
1565
1566         username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1567         oldflags = usr->acb_info;
1568         newflags = usr->acb_info;
1569
1570         HANDLEFLG("disabled", DISABLED);
1571         HANDLEFLG("pwnotreq", PWNOTREQ);
1572         HANDLEFLG("autolock", AUTOLOCK);
1573         HANDLEFLG("pwnoexp", PWNOEXP);
1574
1575         if (argc == 0) {
1576                 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1577                 goto done;
1578         }
1579
1580         ZERO_STRUCTP(usr);
1581
1582         usr->acb_info = newflags;
1583         usr->fields_present = ACCT_FLAGS;
1584
1585         result = rpccli_samr_set_userinfo2(
1586                 pipe_hnd, mem_ctx, user_hnd, 21,
1587                 &pipe_hnd->cli->user_session_key, ctr);
1588
1589         if (NT_STATUS_IS_OK(result)) {
1590                 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1591                          ctx->thiscmd, oldval, argv[0]);
1592         }
1593
1594  done:
1595
1596         return result;
1597 }
1598
1599 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1600                                       struct rpc_sh_ctx *ctx,
1601                                       struct rpc_pipe_client *pipe_hnd,
1602                                       int argc, const char **argv)
1603 {
1604         return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1605                                   rpc_sh_user_flag_edit_internals);
1606 }
1607
1608 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1609                                           struct rpc_sh_ctx *ctx)
1610 {
1611         static struct rpc_sh_cmd cmds[] = {
1612
1613                 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1614                   "Show/Set a user's full name" },
1615
1616                 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1617                   "Show/Set a user's home directory" },
1618
1619                 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1620                   "Show/Set a user's home drive" },
1621
1622                 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1623                   "Show/Set a user's logon script" },
1624
1625                 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1626                   "Show/Set a user's profile path" },
1627
1628                 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1629                   "Show/Set a user's description" },
1630
1631                 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1632                   "Show/Set whether a user is disabled" },
1633
1634                 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1635                   "Show/Set whether a user locked out" },
1636
1637                 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1638                   "Show/Set whether a user does not need a password" },
1639
1640                 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1641                   "Show/Set whether a user's password does not expire" },
1642
1643                 { NULL, NULL, 0, NULL, NULL }
1644         };
1645
1646         return cmds;
1647 }
1648
1649 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1650                                      struct rpc_sh_ctx *ctx)
1651 {
1652         static struct rpc_sh_cmd cmds[] = {
1653
1654                 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1655                   "List available users" },
1656
1657                 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1658                   "List the domain groups a user is member of" },
1659
1660                 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1661                   "Show info about a user" },
1662
1663                 { "edit", net_rpc_user_edit_cmds, 0, NULL, 
1664                   "Show/Modify a user's fields" },
1665
1666                 { NULL, NULL, 0, NULL, NULL }
1667         };
1668
1669         return cmds;
1670 }
1671
1672 /****************************************************************************/
1673
1674 /**
1675  * Basic usage function for 'net rpc group'
1676  * @param argc  Standard main() style argc.
1677  * @param argv  Standard main() style argv.  Initial components are already
1678  *              stripped.
1679  **/
1680
1681 static int rpc_group_usage(int argc, const char **argv)
1682 {
1683         return net_help_group(argc, argv);
1684 }
1685
1686 /**
1687  * Delete group on a remote RPC server
1688  *
1689  * All parameters are provided by the run_rpc_command function, except for
1690  * argc, argv which are passes through.
1691  *
1692  * @param domain_sid The domain sid acquired from the remote server
1693  * @param cli A cli_state connected to the server.
1694  * @param mem_ctx Talloc context, destoyed on completion of the function.
1695  * @param argc  Standard main() style argc
1696  * @param argv  Standard main() style argv.  Initial components are already
1697  *              stripped
1698  *
1699  * @return Normal NTSTATUS return.
1700  **/
1701                                                                                                              
1702 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1703                                         const char *domain_name,
1704                                         struct cli_state *cli,
1705                                         struct rpc_pipe_client *pipe_hnd,
1706                                         TALLOC_CTX *mem_ctx,
1707                                         int argc,
1708                                         const char **argv)
1709 {
1710         POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1711         BOOL group_is_primary = False;
1712         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1713
1714         uint32 *group_rids, num_rids, *name_types, num_members, 
1715                *group_attrs, group_rid;
1716         uint32 flags = 0x000003e8; /* Unknown */
1717         /* char **names; */
1718         int i;
1719         /* DOM_GID *user_gids; */
1720         SAM_USERINFO_CTR *user_ctr;
1721         fstring temp;
1722
1723         if (argc < 1) {
1724                 d_printf("specify group\n");
1725                 rpc_group_usage(argc,argv);
1726                 return NT_STATUS_OK; /* ok? */
1727         }
1728
1729         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1730                                   &connect_pol);
1731
1732         if (!NT_STATUS_IS_OK(result)) {
1733                 d_fprintf(stderr, "Request samr_connect failed\n");
1734                 goto done;
1735         }
1736         
1737         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1738                                       MAXIMUM_ALLOWED_ACCESS,
1739                                       domain_sid, &domain_pol);
1740         
1741         if (!NT_STATUS_IS_OK(result)) {
1742                 d_fprintf(stderr, "Request open_domain failed\n");
1743                 goto done;
1744         }
1745         
1746         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1747                                        flags, 1, &argv[0],
1748                                        &num_rids, &group_rids,
1749                                        &name_types);
1750
1751         if (!NT_STATUS_IS_OK(result)) {
1752                 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1753                 goto done;
1754         }
1755
1756         switch (name_types[0])
1757         {
1758         case SID_NAME_DOM_GRP:
1759                 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1760                                              MAXIMUM_ALLOWED_ACCESS,
1761                                              group_rids[0], &group_pol);
1762                 if (!NT_STATUS_IS_OK(result)) {
1763                         d_fprintf(stderr, "Request open_group failed");
1764                         goto done;
1765                 }
1766                 
1767                 group_rid = group_rids[0];
1768                 
1769                 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1770                                  &num_members, &group_rids,
1771                                  &group_attrs);
1772                 
1773                 if (!NT_STATUS_IS_OK(result)) {
1774                         d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1775                         goto done;
1776                 }
1777                 
1778                 if (opt_verbose) {
1779                         d_printf("Domain Group %s (rid: %d) has %d members\n",
1780                                 argv[0],group_rid,num_members);
1781                 }
1782
1783                 /* Check if group is anyone's primary group */
1784                 for (i = 0; i < num_members; i++)
1785                 {
1786                         result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1787                                                     MAXIMUM_ALLOWED_ACCESS,
1788                                                     group_rids[i], &user_pol);
1789         
1790                         if (!NT_STATUS_IS_OK(result)) {
1791                                 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1792                                 goto done;
1793                         }
1794         
1795                         ZERO_STRUCT(user_ctr);
1796
1797                         result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1798                                                          21, &user_ctr);
1799         
1800                         if (!NT_STATUS_IS_OK(result)) {
1801                                 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1802                                 goto done;
1803                         }
1804         
1805                         if (user_ctr->info.id21->group_rid == group_rid) {
1806                                 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name, 
1807                                                 sizeof(temp)-1);
1808                                 if (opt_verbose) 
1809                                         d_printf("Group is primary group of %s\n",temp);
1810                                 group_is_primary = True;
1811                         }
1812
1813                         rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1814                 }
1815                 
1816                 if (group_is_primary) {
1817                         d_fprintf(stderr, "Unable to delete group because some "
1818                                  "of it's members have it as primary group\n");
1819                         result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1820                         goto done;
1821                 }
1822      
1823                 /* remove all group members */
1824                 for (i = 0; i < num_members; i++)
1825                 {
1826                         if (opt_verbose) 
1827                                 d_printf("Remove group member %d...",group_rids[i]);
1828                         result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1829
1830                         if (NT_STATUS_IS_OK(result)) {
1831                                 if (opt_verbose)
1832                                         d_printf("ok\n");
1833                         } else {
1834                                 if (opt_verbose)
1835                                         d_printf("failed\n");
1836                                 goto done;
1837                         }       
1838                 }
1839
1840                 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1841
1842                 break;
1843         /* removing a local group is easier... */
1844         case SID_NAME_ALIAS:
1845                 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1846                                              MAXIMUM_ALLOWED_ACCESS,
1847                                              group_rids[0], &group_pol);
1848
1849                 if (!NT_STATUS_IS_OK(result)) {
1850                         d_fprintf(stderr, "Request open_alias failed\n");
1851                         goto done;
1852                 }
1853                 
1854                 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1855                 break;
1856         default:
1857                 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1858                         argv[0],sid_type_lookup(name_types[0]));
1859                 result = NT_STATUS_UNSUCCESSFUL;
1860                 goto done;
1861         }
1862          
1863         
1864         if (NT_STATUS_IS_OK(result)) {
1865                 if (opt_verbose)
1866                         d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1867         } else {
1868                 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1869                         get_friendly_nt_error_msg(result));
1870         }
1871         
1872  done:
1873         return result;  
1874         
1875 }
1876
1877 static int rpc_group_delete(int argc, const char **argv)
1878 {
1879         return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1880                                argc,argv);
1881 }
1882
1883 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1884                                         const char *domain_name, 
1885                                         struct cli_state *cli,
1886                                         struct rpc_pipe_client *pipe_hnd,
1887                                         TALLOC_CTX *mem_ctx,
1888                                         int argc,
1889                                         const char **argv)
1890 {
1891         POLICY_HND connect_pol, domain_pol, group_pol;
1892         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1893         GROUP_INFO_CTR group_info;
1894
1895         if (argc != 1) {
1896                 d_printf("Group name must be specified\n");
1897                 rpc_group_usage(argc, argv);
1898                 return NT_STATUS_OK;
1899         }
1900
1901         /* Get sam policy handle */
1902         
1903         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1904                                   &connect_pol);
1905         if (!NT_STATUS_IS_OK(result)) goto done;
1906         
1907         /* Get domain policy handle */
1908         
1909         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1910                                       MAXIMUM_ALLOWED_ACCESS,
1911                                       domain_sid, &domain_pol);
1912         if (!NT_STATUS_IS_OK(result)) goto done;
1913
1914         /* Create the group */
1915
1916         result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1917                                            argv[0], MAXIMUM_ALLOWED_ACCESS,
1918                                            &group_pol);
1919         if (!NT_STATUS_IS_OK(result)) goto done;
1920
1921         if (strlen(opt_comment) == 0) goto done;
1922
1923         /* We've got a comment to set */
1924
1925         group_info.switch_value1 = 4;
1926         init_samr_group_info4(&group_info.group.info4, opt_comment);
1927
1928         result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1929         if (!NT_STATUS_IS_OK(result)) goto done;
1930         
1931  done:
1932         if (NT_STATUS_IS_OK(result))
1933                 DEBUG(5, ("add group succeeded\n"));
1934         else
1935                 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1936
1937         return result;
1938 }
1939
1940 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1941                                         const char *domain_name, 
1942                                         struct cli_state *cli,
1943                                         struct rpc_pipe_client *pipe_hnd,
1944                                         TALLOC_CTX *mem_ctx,
1945                                         int argc,
1946                                         const char **argv)
1947 {
1948         POLICY_HND connect_pol, domain_pol, alias_pol;
1949         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1950         ALIAS_INFO_CTR alias_info;
1951
1952         if (argc != 1) {
1953                 d_printf("Alias name must be specified\n");
1954                 rpc_group_usage(argc, argv);
1955                 return NT_STATUS_OK;
1956         }
1957
1958         /* Get sam policy handle */
1959         
1960         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
1961                                   &connect_pol);
1962         if (!NT_STATUS_IS_OK(result)) goto done;
1963         
1964         /* Get domain policy handle */
1965         
1966         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1967                                       MAXIMUM_ALLOWED_ACCESS,
1968                                       domain_sid, &domain_pol);
1969         if (!NT_STATUS_IS_OK(result)) goto done;
1970
1971         /* Create the group */
1972
1973         result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1974                                            argv[0], &alias_pol);
1975         if (!NT_STATUS_IS_OK(result)) goto done;
1976
1977         if (strlen(opt_comment) == 0) goto done;
1978
1979         /* We've got a comment to set */
1980
1981         alias_info.level = 3;
1982         init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1983
1984         result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1985         if (!NT_STATUS_IS_OK(result)) goto done;
1986         
1987  done:
1988         if (NT_STATUS_IS_OK(result))
1989                 DEBUG(5, ("add alias succeeded\n"));
1990         else
1991                 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1992
1993         return result;
1994 }
1995
1996 static int rpc_group_add(int argc, const char **argv)
1997 {
1998         if (opt_localgroup)
1999                 return run_rpc_command(NULL, PI_SAMR, 0,
2000                                        rpc_alias_add_internals,
2001                                        argc, argv);
2002
2003         return run_rpc_command(NULL, PI_SAMR, 0,
2004                                rpc_group_add_internals,
2005                                argc, argv);
2006 }
2007
2008 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2009                                 TALLOC_CTX *mem_ctx,
2010                                 const char *name,
2011                                 DOM_SID *sid,
2012                                 enum lsa_SidType *type)
2013 {
2014         DOM_SID *sids = NULL;
2015         enum lsa_SidType *types = NULL;
2016         struct rpc_pipe_client *pipe_hnd;
2017         POLICY_HND lsa_pol;
2018         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2019
2020         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2021         if (!pipe_hnd) {
2022                 goto done;
2023         }
2024
2025         result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2026                                      SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2027
2028         if (!NT_STATUS_IS_OK(result)) {
2029                 goto done;
2030         }
2031
2032         result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2033                                       &name, NULL, &sids, &types);
2034
2035         if (NT_STATUS_IS_OK(result)) {
2036                 sid_copy(sid, &sids[0]);
2037                 *type = types[0];
2038         }
2039
2040         rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2041
2042  done:
2043         if (pipe_hnd) {
2044                 cli_rpc_pipe_close(pipe_hnd);
2045         }
2046
2047         if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2048
2049                 /* Try as S-1-5-whatever */
2050
2051                 DOM_SID tmp_sid;
2052
2053                 if (string_to_sid(&tmp_sid, name)) {
2054                         sid_copy(sid, &tmp_sid);
2055                         *type = SID_NAME_UNKNOWN;
2056                         result = NT_STATUS_OK;
2057                 }
2058         }
2059
2060         return result;
2061 }
2062
2063 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2064                                 TALLOC_CTX *mem_ctx,
2065                                 const DOM_SID *group_sid,
2066                                 const char *member)
2067 {
2068         POLICY_HND connect_pol, domain_pol;
2069         NTSTATUS result;
2070         uint32 group_rid;
2071         POLICY_HND group_pol;
2072
2073         uint32 num_rids;
2074         uint32 *rids = NULL;
2075         uint32 *rid_types = NULL;
2076
2077         DOM_SID sid;
2078
2079         sid_copy(&sid, group_sid);
2080
2081         if (!sid_split_rid(&sid, &group_rid)) {
2082                 return NT_STATUS_UNSUCCESSFUL;
2083         }
2084
2085         /* Get sam policy handle */     
2086         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
2087                                   &connect_pol);
2088         if (!NT_STATUS_IS_OK(result)) {
2089                 return result;
2090         }
2091         
2092         /* Get domain policy handle */
2093         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2094                                       MAXIMUM_ALLOWED_ACCESS,
2095                                       &sid, &domain_pol);
2096         if (!NT_STATUS_IS_OK(result)) {
2097                 return result;
2098         }
2099
2100         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2101                                        1, &member,
2102                                        &num_rids, &rids, &rid_types);
2103
2104         if (!NT_STATUS_IS_OK(result)) {
2105                 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2106                 goto done;
2107         }
2108
2109         result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2110                                      MAXIMUM_ALLOWED_ACCESS,
2111                                      group_rid, &group_pol);
2112
2113         if (!NT_STATUS_IS_OK(result)) {
2114                 goto done;
2115         }
2116
2117         result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2118
2119  done:
2120         rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2121         return result;
2122 }
2123
2124 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2125                                 TALLOC_CTX *mem_ctx,
2126                                 const DOM_SID *alias_sid,
2127                                 const char *member)
2128 {
2129         POLICY_HND connect_pol, domain_pol;
2130         NTSTATUS result;
2131         uint32 alias_rid;
2132         POLICY_HND alias_pol;
2133
2134         DOM_SID member_sid;
2135         enum lsa_SidType member_type;
2136
2137         DOM_SID sid;
2138
2139         sid_copy(&sid, alias_sid);
2140
2141         if (!sid_split_rid(&sid, &alias_rid)) {
2142                 return NT_STATUS_UNSUCCESSFUL;
2143         }
2144
2145         result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2146                                    &member_sid, &member_type);
2147
2148         if (!NT_STATUS_IS_OK(result)) {
2149                 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2150                 return result;
2151         }
2152
2153         /* Get sam policy handle */     
2154         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
2155                                   &connect_pol);
2156         if (!NT_STATUS_IS_OK(result)) {
2157                 goto done;
2158         }
2159         
2160         /* Get domain policy handle */
2161         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2162                                       MAXIMUM_ALLOWED_ACCESS,
2163                                       &sid, &domain_pol);
2164         if (!NT_STATUS_IS_OK(result)) {
2165                 goto done;
2166         }
2167
2168         result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2169                                      MAXIMUM_ALLOWED_ACCESS,
2170                                      alias_rid, &alias_pol);
2171
2172         if (!NT_STATUS_IS_OK(result)) {
2173                 return result;
2174         }
2175
2176         result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2177
2178         if (!NT_STATUS_IS_OK(result)) {
2179                 return result;
2180         }
2181
2182  done:
2183         rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2184         return result;
2185 }
2186
2187 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2188                                         const char *domain_name, 
2189                                         struct cli_state *cli,
2190                                         struct rpc_pipe_client *pipe_hnd,
2191                                         TALLOC_CTX *mem_ctx,
2192                                         int argc,
2193                                         const char **argv)
2194 {
2195         DOM_SID group_sid;
2196         enum lsa_SidType group_type;
2197
2198         if (argc != 2) {
2199                 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2200                 return NT_STATUS_UNSUCCESSFUL;
2201         }
2202
2203         if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2204                                                &group_sid, &group_type))) {
2205                 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2206                 return NT_STATUS_UNSUCCESSFUL;
2207         }
2208
2209         if (group_type == SID_NAME_DOM_GRP) {
2210                 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2211                                                    &group_sid, argv[1]);
2212
2213                 if (!NT_STATUS_IS_OK(result)) {
2214                         d_fprintf(stderr, "Could not add %s to %s: %s\n",
2215                                  argv[1], argv[0], nt_errstr(result));
2216                 }
2217                 return result;
2218         }
2219
2220         if (group_type == SID_NAME_ALIAS) {
2221                 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2222                                                    &group_sid, argv[1]);
2223
2224                 if (!NT_STATUS_IS_OK(result)) {
2225                         d_fprintf(stderr, "Could not add %s to %s: %s\n",
2226                                  argv[1], argv[0], nt_errstr(result));
2227                 }
2228                 return result;
2229         }
2230
2231         d_fprintf(stderr, "Can only add members to global or local groups "
2232                  "which %s is not\n", argv[0]);
2233
2234         return NT_STATUS_UNSUCCESSFUL;
2235 }
2236
2237 static int rpc_group_addmem(int argc, const char **argv)
2238 {
2239         return run_rpc_command(NULL, PI_SAMR, 0,
2240                                rpc_group_addmem_internals,
2241                                argc, argv);
2242 }
2243
2244 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2245                                 TALLOC_CTX *mem_ctx,
2246                                 const DOM_SID *group_sid,
2247                                 const char *member)
2248 {
2249         POLICY_HND connect_pol, domain_pol;
2250         NTSTATUS result;
2251         uint32 group_rid;
2252         POLICY_HND group_pol;
2253
2254         uint32 num_rids;
2255         uint32 *rids = NULL;
2256         uint32 *rid_types = NULL;
2257
2258         DOM_SID sid;
2259
2260         sid_copy(&sid, group_sid);
2261
2262         if (!sid_split_rid(&sid, &group_rid))
2263                 return NT_STATUS_UNSUCCESSFUL;
2264
2265         /* Get sam policy handle */     
2266         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
2267                                   &connect_pol);
2268         if (!NT_STATUS_IS_OK(result))
2269                 return result;
2270         
2271         /* Get domain policy handle */
2272         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2273                                       MAXIMUM_ALLOWED_ACCESS,
2274                                       &sid, &domain_pol);
2275         if (!NT_STATUS_IS_OK(result))
2276                 return result;
2277
2278         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2279                                        1, &member,
2280                                        &num_rids, &rids, &rid_types);
2281
2282         if (!NT_STATUS_IS_OK(result)) {
2283                 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2284                 goto done;
2285         }
2286
2287         result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2288                                      MAXIMUM_ALLOWED_ACCESS,
2289                                      group_rid, &group_pol);
2290
2291         if (!NT_STATUS_IS_OK(result))
2292                 goto done;
2293
2294         result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2295
2296  done:
2297         rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2298         return result;
2299 }
2300
2301 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2302                                 TALLOC_CTX *mem_ctx,
2303                                 const DOM_SID *alias_sid,
2304                                 const char *member)
2305 {
2306         POLICY_HND connect_pol, domain_pol;
2307         NTSTATUS result;
2308         uint32 alias_rid;
2309         POLICY_HND alias_pol;
2310
2311         DOM_SID member_sid;
2312         enum lsa_SidType member_type;
2313
2314         DOM_SID sid;
2315
2316         sid_copy(&sid, alias_sid);
2317
2318         if (!sid_split_rid(&sid, &alias_rid))
2319                 return NT_STATUS_UNSUCCESSFUL;
2320
2321         result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2322                                    &member_sid, &member_type);
2323
2324         if (!NT_STATUS_IS_OK(result)) {
2325                 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2326                 return result;
2327         }
2328
2329         /* Get sam policy handle */     
2330         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
2331                                   &connect_pol);
2332         if (!NT_STATUS_IS_OK(result)) {
2333                 goto done;
2334         }
2335         
2336         /* Get domain policy handle */
2337         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2338                                       MAXIMUM_ALLOWED_ACCESS,
2339                                       &sid, &domain_pol);
2340         if (!NT_STATUS_IS_OK(result)) {
2341                 goto done;
2342         }
2343
2344         result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2345                                      MAXIMUM_ALLOWED_ACCESS,
2346                                      alias_rid, &alias_pol);
2347
2348         if (!NT_STATUS_IS_OK(result))
2349                 return result;
2350
2351         result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2352
2353         if (!NT_STATUS_IS_OK(result))
2354                 return result;
2355
2356  done:
2357         rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2358         return result;
2359 }
2360
2361 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2362                                         const char *domain_name, 
2363                                         struct cli_state *cli,
2364                                         struct rpc_pipe_client *pipe_hnd,
2365                                         TALLOC_CTX *mem_ctx,
2366                                         int argc,
2367                                         const char **argv)
2368 {
2369         DOM_SID group_sid;
2370         enum lsa_SidType group_type;
2371
2372         if (argc != 2) {
2373                 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2374                 return NT_STATUS_UNSUCCESSFUL;
2375         }
2376
2377         if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2378                                                &group_sid, &group_type))) {
2379                 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2380                 return NT_STATUS_UNSUCCESSFUL;
2381         }
2382
2383         if (group_type == SID_NAME_DOM_GRP) {
2384                 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2385                                                    &group_sid, argv[1]);
2386
2387                 if (!NT_STATUS_IS_OK(result)) {
2388                         d_fprintf(stderr, "Could not del %s from %s: %s\n",
2389                                  argv[1], argv[0], nt_errstr(result));
2390                 }
2391                 return result;
2392         }
2393
2394         if (group_type == SID_NAME_ALIAS) {
2395                 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx, 
2396                                                    &group_sid, argv[1]);
2397
2398                 if (!NT_STATUS_IS_OK(result)) {
2399                         d_fprintf(stderr, "Could not del %s from %s: %s\n",
2400                                  argv[1], argv[0], nt_errstr(result));
2401                 }
2402                 return result;
2403         }
2404
2405         d_fprintf(stderr, "Can only delete members from global or local groups "
2406                  "which %s is not\n", argv[0]);
2407
2408         return NT_STATUS_UNSUCCESSFUL;
2409 }
2410
2411 static int rpc_group_delmem(int argc, const char **argv)
2412 {
2413         return run_rpc_command(NULL, PI_SAMR, 0,
2414                                rpc_group_delmem_internals,
2415                                argc, argv);
2416 }
2417
2418 /** 
2419  * List groups on a remote RPC server
2420  *
2421  * All parameters are provided by the run_rpc_command function, except for
2422  * argc, argv which are passes through. 
2423  *
2424  * @param domain_sid The domain sid acquired from the remote server
2425  * @param cli A cli_state connected to the server.
2426  * @param mem_ctx Talloc context, destoyed on completion of the function.
2427  * @param argc  Standard main() style argc
2428  * @param argv  Standard main() style argv.  Initial components are already
2429  *              stripped
2430  *
2431  * @return Normal NTSTATUS return.
2432  **/
2433
2434 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2435                                         const char *domain_name, 
2436                                         struct cli_state *cli,
2437                                         struct rpc_pipe_client *pipe_hnd,
2438                                         TALLOC_CTX *mem_ctx,
2439                                         int argc,
2440                                         const char **argv)
2441 {
2442         POLICY_HND connect_pol, domain_pol;
2443         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2444         uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2445         struct acct_info *groups;
2446         BOOL global = False;
2447         BOOL local = False;
2448         BOOL builtin = False;
2449
2450         if (argc == 0) {
2451                 global = True;
2452                 local = True;
2453                 builtin = True;
2454         }
2455
2456         for (i=0; i<argc; i++) {
2457                 if (strequal(argv[i], "global"))
2458                         global = True;
2459
2460                 if (strequal(argv[i], "local"))
2461                         local = True;
2462
2463                 if (strequal(argv[i], "builtin"))
2464                         builtin = True;
2465         }
2466
2467         /* Get sam policy handle */
2468         
2469         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
2470                                   &connect_pol);
2471         if (!NT_STATUS_IS_OK(result)) {
2472                 goto done;
2473         }
2474         
2475         /* Get domain policy handle */
2476         
2477         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2478                                       MAXIMUM_ALLOWED_ACCESS,
2479                                       domain_sid, &domain_pol);
2480         if (!NT_STATUS_IS_OK(result)) {
2481                 goto done;
2482         }
2483
2484         /* Query domain groups */
2485         if (opt_long_list_entries)
2486                 d_printf("\nGroup name            Comment"\
2487                          "\n-----------------------------\n");
2488         do {
2489                 SAM_DISPINFO_CTR ctr;
2490                 SAM_DISPINFO_3 info3;
2491                 uint32 max_size;
2492
2493                 ZERO_STRUCT(ctr);
2494                 ZERO_STRUCT(info3);
2495                 ctr.sam.info3 = &info3;
2496
2497                 if (!global) break;
2498
2499                 get_query_dispinfo_params(
2500                         loop_count, &max_entries, &max_size);
2501
2502                 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2503                                                  &start_idx, 3, &num_entries,
2504                                                  max_entries, max_size, &ctr);
2505
2506                 if (!NT_STATUS_IS_OK(result) &&
2507                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2508                         break;
2509                                                  
2510                 for (i = 0; i < num_entries; i++) {
2511
2512                         fstring group, desc;
2513
2514                         unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2515                         unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2516                         
2517                         if (opt_long_list_entries)
2518                                 printf("%-21.21s %-50.50s\n",
2519                                        group, desc);
2520                         else
2521                                 printf("%s\n", group);
2522                 }
2523         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2524         /* query domain aliases */
2525         start_idx = 0;
2526         do {
2527                 if (!local) break;
2528
2529                 /* The max_size field in cli_samr_enum_als_groups is more like
2530                  * an account_control field with indiviual bits what to
2531                  * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2532                  * everything. I'm too lazy (sorry) to get this through to
2533                  * rpc_parse/ etc.  Volker */
2534
2535                 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2536                                                   &start_idx, 0xffff,
2537                                                   &groups, &num_entries);
2538
2539                 if (!NT_STATUS_IS_OK(result) &&
2540                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2541                         break;
2542                                                  
2543                 for (i = 0; i < num_entries; i++) {
2544
2545                         char *description = NULL;
2546
2547                         if (opt_long_list_entries) {
2548
2549                                 POLICY_HND alias_pol;
2550                                 ALIAS_INFO_CTR ctr;
2551
2552                                 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2553                                                                          &domain_pol,
2554                                                                          0x8,
2555                                                                          groups[i].rid,
2556                                                                          &alias_pol))) &&
2557                                     (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2558                                                                                &alias_pol, 3,
2559                                                                                &ctr))) &&
2560                                     (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2561                                                                     &alias_pol)))) {
2562                                         description = unistr2_tdup(mem_ctx,
2563                                                                    ctr.alias.info3.description.string);
2564                                 }
2565                         }
2566                         
2567                         if (description != NULL) {
2568                                 printf("%-21.21s %-50.50s\n", 
2569                                        groups[i].acct_name,
2570                                        description);
2571                         } else {
2572                                 printf("%s\n", groups[i].acct_name);
2573                         }
2574                 }
2575         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2576         rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2577         /* Get builtin policy handle */
2578         
2579         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2580                                       MAXIMUM_ALLOWED_ACCESS,
2581                                       &global_sid_Builtin, &domain_pol);
2582         if (!NT_STATUS_IS_OK(result)) {
2583                 goto done;
2584         }
2585         /* query builtin aliases */
2586         start_idx = 0;
2587         do {
2588                 if (!builtin) break;
2589
2590                 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2591                                                   &start_idx, max_entries,
2592                                                   &groups, &num_entries);
2593                                                  
2594                 if (!NT_STATUS_IS_OK(result) &&
2595                     !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2596                         break;
2597                                                  
2598                 for (i = 0; i < num_entries; i++) {
2599
2600                         char *description = NULL;
2601
2602                         if (opt_long_list_entries) {
2603
2604                                 POLICY_HND alias_pol;
2605                                 ALIAS_INFO_CTR ctr;
2606
2607                                 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2608                                                                          &domain_pol,
2609                                                                          0x8,
2610                                                                          groups[i].rid,
2611                                                                          &alias_pol))) &&
2612                                     (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2613                                                                                &alias_pol, 3,
2614                                                                                &ctr))) &&
2615                                     (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2616                                                                     &alias_pol)))) {
2617                                         description = unistr2_tdup(mem_ctx,
2618                                                                    ctr.alias.info3.description.string);
2619                                 }
2620                         }
2621                         
2622                         if (description != NULL) {
2623                                 printf("%-21.21s %-50.50s\n", 
2624                                        groups[i].acct_name,
2625                                        description);
2626                         } else {
2627                                 printf("%s\n", groups[i].acct_name);
2628                         }
2629                 }
2630         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2631
2632  done:
2633         return result;
2634 }
2635
2636 static int rpc_group_list(int argc, const char **argv)
2637 {
2638         return run_rpc_command(NULL, PI_SAMR, 0,
2639                                rpc_group_list_internals,
2640                                argc, argv);
2641 }
2642
2643 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2644                                         TALLOC_CTX *mem_ctx,
2645                                         const char *domain_name,
2646                                         const DOM_SID *domain_sid,
2647                                         POLICY_HND *domain_pol,
2648                                         uint32 rid)
2649 {
2650         NTSTATUS result;
2651         POLICY_HND group_pol;
2652         uint32 num_members, *group_rids, *group_attrs;
2653         uint32 num_names;
2654         char **names;
2655         uint32 *name_types;
2656         int i;
2657
2658         fstring sid_str;
2659         sid_to_string(sid_str, domain_sid);
2660
2661         result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2662                                      MAXIMUM_ALLOWED_ACCESS,
2663                                      rid, &group_pol);
2664
2665         if (!NT_STATUS_IS_OK(result))
2666                 return result;
2667
2668         result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2669                                          &num_members, &group_rids,
2670                                          &group_attrs);
2671
2672         if (!NT_STATUS_IS_OK(result))
2673                 return result;
2674
2675         while (num_members > 0) {
2676                 int this_time = 512;
2677
2678                 if (num_members < this_time)
2679                         this_time = num_members;
2680
2681                 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2682                                               this_time, group_rids,
2683                                               &num_names, &names, &name_types);
2684
2685                 if (!NT_STATUS_IS_OK(result))
2686                         return result;
2687
2688                 /* We only have users as members, but make the output
2689                    the same as the output of alias members */
2690
2691                 for (i = 0; i < this_time; i++) {
2692
2693                         if (opt_long_list_entries) {
2694                                 printf("%s-%d %s\\%s %d\n", sid_str,
2695                                        group_rids[i], domain_name, names[i],
2696                                        SID_NAME_USER);
2697                         } else {
2698                                 printf("%s\\%s\n", domain_name, names[i]);
2699                         }
2700                 }
2701
2702                 num_members -= this_time;
2703                 group_rids += 512;
2704         }
2705
2706         return NT_STATUS_OK;
2707 }
2708
2709 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2710                                         TALLOC_CTX *mem_ctx,
2711                                         POLICY_HND *domain_pol,
2712                                         uint32 rid)
2713 {
2714         NTSTATUS result;
2715         struct rpc_pipe_client *lsa_pipe;
2716         POLICY_HND alias_pol, lsa_pol;
2717         uint32 num_members;
2718         DOM_SID *alias_sids;
2719         char **domains;
2720         char **names;
2721         enum lsa_SidType *types;
2722         int i;
2723
2724         result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2725                                      MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2726
2727         if (!NT_STATUS_IS_OK(result))
2728                 return result;
2729
2730         result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2731                                          &num_members, &alias_sids);
2732
2733         if (!NT_STATUS_IS_OK(result)) {
2734                 d_fprintf(stderr, "Couldn't list alias members\n");
2735                 return result;
2736         }
2737
2738         if (num_members == 0) {
2739                 return NT_STATUS_OK;
2740         }
2741
2742         lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2743         if (!lsa_pipe) {
2744                 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2745                         nt_errstr(result) );
2746                 return result;
2747         }
2748
2749         result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2750                                      SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2751
2752         if (!NT_STATUS_IS_OK(result)) {
2753                 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2754                 cli_rpc_pipe_close(lsa_pipe);
2755                 return result;
2756         }
2757
2758         result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2759                                      alias_sids, 
2760                                      &domains, &names, &types);
2761
2762         if (!NT_STATUS_IS_OK(result) &&
2763             !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2764                 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2765                 cli_rpc_pipe_close(lsa_pipe);
2766                 return result;
2767         }
2768
2769         for (i = 0; i < num_members; i++) {
2770                 fstring sid_str;
2771                 sid_to_string(sid_str, &alias_sids[i]);
2772
2773                 if (opt_long_list_entries) {
2774                         printf("%s %s\\%s %d\n", sid_str, 
2775                                domains[i] ? domains[i] : "*unknown*", 
2776                                names[i] ? names[i] : "*unknown*", types[i]);
2777                 } else {
2778                         if (domains[i])
2779                                 printf("%s\\%s\n", domains[i], names[i]);
2780                         else
2781                                 printf("%s\n", sid_str);
2782                 }
2783         }
2784
2785         cli_rpc_pipe_close(lsa_pipe);
2786         return NT_STATUS_OK;
2787 }
2788  
2789 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2790                                         const char *domain_name, 
2791                                         struct cli_state *cli,
2792                                         struct rpc_pipe_client *pipe_hnd,
2793                                         TALLOC_CTX *mem_ctx,
2794                                         int argc,
2795                                         const char **argv)
2796 {
2797         NTSTATUS result;
2798         POLICY_HND connect_pol, domain_pol;
2799         uint32 num_rids, *rids, *rid_types;
2800
2801         /* Get sam policy handle */
2802         
2803         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
2804                                   &connect_pol);
2805
2806         if (!NT_STATUS_IS_OK(result))
2807                 return result;
2808         
2809         /* Get domain policy handle */
2810         
2811         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2812                                       MAXIMUM_ALLOWED_ACCESS,
2813                                       domain_sid, &domain_pol);
2814
2815         if (!NT_STATUS_IS_OK(result))
2816                 return result;
2817
2818         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2819                                        1, argv, &num_rids, &rids, &rid_types);
2820
2821         if (!NT_STATUS_IS_OK(result)) {
2822
2823                 /* Ok, did not find it in the global sam, try with builtin */
2824
2825                 DOM_SID sid_Builtin;
2826
2827                 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2828
2829                 string_to_sid(&sid_Builtin, "S-1-5-32");                
2830
2831                 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2832                                               MAXIMUM_ALLOWED_ACCESS,
2833                                               &sid_Builtin, &domain_pol);
2834
2835                 if (!NT_STATUS_IS_OK(result)) {
2836                         d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2837                         return result;
2838                 }
2839
2840                 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2841                                                1, argv, &num_rids,
2842                                                &rids, &rid_types);
2843
2844                 if (!NT_STATUS_IS_OK(result)) {
2845                         d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2846                         return result;
2847                 }
2848         }
2849
2850         if (num_rids != 1) {
2851                 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2852                 return result;
2853         }
2854
2855         if (rid_types[0] == SID_NAME_DOM_GRP) {
2856                 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2857                                               domain_sid, &domain_pol,
2858                                               rids[0]);
2859         }
2860
2861         if (rid_types[0] == SID_NAME_ALIAS) {
2862                 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2863                                               rids[0]);
2864         }
2865
2866         return NT_STATUS_NO_SUCH_GROUP;
2867 }
2868
2869 static int rpc_group_members(int argc, const char **argv)
2870 {
2871         if (argc != 1) {
2872                 return rpc_group_usage(argc, argv);
2873         }
2874
2875         return run_rpc_command(NULL, PI_SAMR, 0,
2876                                rpc_group_members_internals,
2877                                argc, argv);
2878 }
2879
2880 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2881                                         const char *domain_name, 
2882                                         struct cli_state *cli,
2883                                         struct rpc_pipe_client *pipe_hnd,
2884                                         TALLOC_CTX *mem_ctx,
2885                                         int argc,
2886                                         const char **argv)
2887 {
2888         NTSTATUS result;
2889         POLICY_HND connect_pol, domain_pol, group_pol;
2890         uint32 num_rids, *rids, *rid_types;
2891         GROUP_INFO_CTR ctr;
2892
2893         if (argc != 2) {
2894                 d_printf("Usage: 'net rpc group rename group newname'\n");
2895                 return NT_STATUS_UNSUCCESSFUL;
2896         }
2897
2898         /* Get sam policy handle */
2899         
2900         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
2901                                   &connect_pol);
2902
2903         if (!NT_STATUS_IS_OK(result))
2904                 return result;
2905         
2906         /* Get domain policy handle */
2907         
2908         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2909                                       MAXIMUM_ALLOWED_ACCESS,
2910                                       domain_sid, &domain_pol);
2911
2912         if (!NT_STATUS_IS_OK(result))
2913                 return result;
2914
2915         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2916                                        1, argv, &num_rids, &rids, &rid_types);
2917
2918         if (num_rids != 1) {
2919                 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2920                 return result;
2921         }
2922
2923         if (rid_types[0] != SID_NAME_DOM_GRP) {
2924                 d_fprintf(stderr, "Can only rename domain groups\n");
2925                 return NT_STATUS_UNSUCCESSFUL;
2926         }
2927
2928         result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2929                                      MAXIMUM_ALLOWED_ACCESS,
2930                                      rids[0], &group_pol);
2931
2932         if (!NT_STATUS_IS_OK(result))
2933                 return result;
2934
2935         ZERO_STRUCT(ctr);
2936
2937         ctr.switch_value1 = 2;
2938         init_samr_group_info2(&ctr.group.info2, argv[1]);
2939
2940         result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2941
2942         if (!NT_STATUS_IS_OK(result))
2943                 return result;
2944
2945         return NT_STATUS_NO_SUCH_GROUP;
2946 }
2947
2948 static int rpc_group_rename(int argc, const char **argv)
2949 {
2950         if (argc != 2) {
2951                 return rpc_group_usage(argc, argv);
2952         }
2953
2954         return run_rpc_command(NULL, PI_SAMR, 0,
2955                                rpc_group_rename_internals,
2956                                argc, argv);
2957 }
2958
2959 /** 
2960  * 'net rpc group' entrypoint.
2961  * @param argc  Standard main() style argc
2962  * @param argc  Standard main() style argv.  Initial components are already
2963  *              stripped
2964  **/
2965
2966 int net_rpc_group(int argc, const char **argv) 
2967 {
2968         struct functable func[] = {
2969                 {"add", rpc_group_add},
2970                 {"delete", rpc_group_delete},
2971                 {"addmem", rpc_group_addmem},
2972                 {"delmem", rpc_group_delmem},
2973                 {"list", rpc_group_list},
2974                 {"members", rpc_group_members},
2975                 {"rename", rpc_group_rename},
2976                 {NULL, NULL}
2977         };
2978         
2979         if (argc == 0) {
2980                 return run_rpc_command(NULL, PI_SAMR, 0, 
2981                                        rpc_group_list_internals,
2982                                        argc, argv);
2983         }
2984
2985         return net_run_function(argc, argv, func, rpc_group_usage);
2986 }
2987
2988 /****************************************************************************/
2989
2990 static int rpc_share_usage(int argc, const char **argv)
2991 {
2992         return net_help_share(argc, argv);
2993 }
2994
2995 /** 
2996  * Add a share on a remote RPC server
2997  *
2998  * All parameters are provided by the run_rpc_command function, except for
2999  * argc, argv which are passes through. 
3000  *
3001  * @param domain_sid The domain sid acquired from the remote server
3002  * @param cli A cli_state connected to the server.
3003  * @param mem_ctx Talloc context, destoyed on completion of the function.
3004  * @param argc  Standard main() style argc
3005  * @param argv  Standard main() style argv.  Initial components are already
3006  *              stripped
3007  *
3008  * @return Normal NTSTATUS return.
3009  **/
3010 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3011                                         const char *domain_name, 
3012                                         struct cli_state *cli,
3013                                         struct rpc_pipe_client *pipe_hnd,
3014                                         TALLOC_CTX *mem_ctx,int argc,
3015                                         const char **argv)
3016 {
3017         NTSTATUS result;
3018         char *sharename;
3019         char *path;
3020         uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3021         uint32 num_users=0, perms=0;
3022         char *password=NULL; /* don't allow a share password */
3023         uint32 level = 2;
3024         uint32 *pparm_error = NULL;
3025         union srvsvc_NetShareInfo info;
3026         struct srvsvc_NetShareInfo2 info2;
3027
3028         if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3029                 return NT_STATUS_NO_MEMORY;
3030         }
3031
3032         path = strchr(sharename, '=');
3033         if (!path)
3034                 return NT_STATUS_UNSUCCESSFUL;
3035         *path++ = '\0';
3036
3037         info.info2 = &info2;
3038
3039         info2.type = type;
3040         info2.comment = opt_comment;
3041         info2.permissions = perms;
3042         info2.max_users = opt_maxusers;
3043         info2.current_users = num_users;
3044         info2.path = path;
3045         info2.password = password;
3046         info2.name = sharename;
3047
3048         result = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx, NULL, level, 
3049                                            info, &pparm_error);
3050         return result;
3051 }
3052
3053 static int rpc_share_add(int argc, const char **argv)
3054 {
3055         if ((argc < 1) || !strchr(argv[0], '=')) {
3056                 DEBUG(1,("Sharename or path not specified on add\n"));
3057                 return rpc_share_usage(argc, argv);
3058         }
3059         return run_rpc_command(NULL, PI_SRVSVC, 0, 
3060                                rpc_share_add_internals,
3061                                argc, argv);
3062 }
3063
3064 /** 
3065  * Delete a share on a remote RPC server
3066  *
3067  * All parameters are provided by the run_rpc_command function, except for
3068  * argc, argv which are passes through. 
3069  *
3070  * @param domain_sid The domain sid acquired from the remote server
3071  * @param cli A cli_state connected to the server.
3072  * @param mem_ctx Talloc context, destoyed on completion of the function.
3073  * @param argc  Standard main() style argc
3074  * @param argv  Standard main() style argv.  Initial components are already
3075  *              stripped
3076  *
3077  * @return Normal NTSTATUS return.
3078  **/
3079 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3080                                         const char *domain_name, 
3081                                         struct cli_state *cli,
3082                                         struct rpc_pipe_client *pipe_hnd,
3083                                         TALLOC_CTX *mem_ctx,
3084                                         int argc,
3085                                         const char **argv)
3086 {
3087         return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx, NULL, argv[0], 0);
3088 }
3089
3090 /** 
3091  * Delete a share on a remote RPC server
3092  *
3093  * @param domain_sid The domain sid acquired from the remote server
3094  * @param argc  Standard main() style argc
3095  * @param argv  Standard main() style argv.  Initial components are already
3096  *              stripped
3097  *
3098  * @return A shell status integer (0 for success)
3099  **/
3100 static int rpc_share_delete(int argc, const char **argv)
3101 {
3102         if (argc < 1) {
3103                 DEBUG(1,("Sharename not specified on delete\n"));
3104                 return rpc_share_usage(argc, argv);
3105         }
3106         return run_rpc_command(NULL, PI_SRVSVC, 0, 
3107                                rpc_share_del_internals,
3108                                argc, argv);
3109 }
3110
3111 /**
3112  * Formatted print of share info
3113  *
3114  * @param info1  pointer to struct srvsvc_NetShareInfo1 to format
3115  **/
3116  
3117 static void display_share_info_1(struct srvsvc_NetShareInfo1 *info1)
3118 {
3119         if (opt_long_list_entries) {
3120                 d_printf("%-12s %-8.8s %-50s\n",
3121                          info1->name, share_type[info1->type],
3122                          info1->comment ? info1->comment : "");
3123         } else {
3124                 d_printf("%s\n", info1->name);
3125         }
3126
3127 }
3128
3129 static NTSTATUS get_share_info(struct rpc_pipe_client *pipe_hnd,
3130                                 TALLOC_CTX *mem_ctx, 
3131                                 uint32 level,
3132                                 int argc,
3133                                 const char **argv, 
3134                                 union srvsvc_NetShareCtr *ctr, 
3135                                 uint32 *numentries)
3136 {
3137         union srvsvc_NetShareInfo info;
3138         NTSTATUS status;
3139
3140         switch(level) {
3141         case 1:
3142                 if (!(ctr->ctr1 = TALLOC_ZERO_P(
3143                               mem_ctx, struct srvsvc_NetShareCtr1))) {
3144                         return NT_STATUS_NO_MEMORY;
3145                 }
3146                 break;
3147         case 502:
3148                 if (!(ctr->ctr502 = TALLOC_ZERO_P(
3149                               mem_ctx, struct srvsvc_NetShareCtr502))) {
3150                         return NT_STATUS_NO_MEMORY;
3151                 }
3152                 break;
3153         default:
3154                 return NT_STATUS_INVALID_LEVEL;
3155                 break;
3156         }
3157
3158         /* no specific share requested, enumerate all */
3159         if (argc == 0) {
3160                 uint32 hnd = 0;
3161                 uint32 *phnd = &hnd;
3162
3163                 return rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
3164                                                   &level, ctr, 0xffffffff,
3165                                                   numentries, &phnd);
3166         }
3167
3168         /* request just one share */
3169         status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx, NULL,
3170                                                argv[0], level, &info);
3171         if (!NT_STATUS_IS_OK(status)) {
3172                 return status;
3173         }
3174
3175         *numentries = 1;
3176
3177         switch(level) {
3178         case 1:
3179                 ctr->ctr1->count = 1;
3180                 ctr->ctr1->array = info.info1;
3181                 break;
3182         case 502:
3183                 ctr->ctr501->count = 1;
3184                 ctr->ctr502->array = info.info502;
3185                 break;
3186         default:
3187                 return NT_STATUS_INTERNAL_ERROR;
3188                 break;
3189         }
3190
3191         return NT_STATUS_OK;
3192 }
3193
3194 /** 
3195  * List shares on a remote RPC server
3196  *
3197  * All parameters are provided by the run_rpc_command function, except for
3198  * argc, argv which are passes through. 
3199  *
3200  * @param domain_sid The domain sid acquired from the remote server
3201  * @param cli A cli_state connected to the server.
3202  * @param mem_ctx Talloc context, destoyed on completion of the function.
3203  * @param argc  Standard main() style argc
3204  * @param argv  Standard main() style argv.  Initial components are already
3205  *              stripped
3206  *
3207  * @return Normal NTSTATUS return.
3208  **/
3209
3210 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3211                                         const char *domain_name, 
3212                                         struct cli_state *cli,
3213                                         struct rpc_pipe_client *pipe_hnd,
3214                                         TALLOC_CTX *mem_ctx,
3215                                         int argc,
3216                                         const char **argv)
3217 {
3218         union srvsvc_NetShareCtr ctr;
3219         NTSTATUS result;
3220         uint32 i, level = 1;
3221         uint32 numentries;
3222
3223         result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr, 
3224                                                         &numentries);
3225         if (!NT_STATUS_IS_OK(result))
3226                 goto done;
3227
3228         /* Display results */
3229
3230         if (opt_long_list_entries) {
3231                 d_printf(
3232         "\nEnumerating shared resources (exports) on remote server:\n\n"\
3233         "\nShare name   Type     Description\n"\
3234         "----------   ----     -----------\n");
3235         }
3236         for (i = 0; i < numentries; i++)
3237                 display_share_info_1(&ctr.ctr1->array[i]);
3238  done:
3239         return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3240 }
3241
3242 /*** 
3243  * 'net rpc share list' entrypoint.
3244  * @param argc  Standard main() style argc
3245  * @param argv  Standard main() style argv.  Initial components are already
3246  *              stripped
3247  **/
3248 static int rpc_share_list(int argc, const char **argv)
3249 {
3250         return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3251 }
3252
3253 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3254 {
3255         if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3256                 d_printf("skipping   [%s]: not a file share.\n", netname);
3257                 return False;
3258         }
3259
3260         if (!cli_tdis(cli)) 
3261                 return False;
3262
3263         return True;
3264 }
3265
3266 static BOOL check_share_sanity(struct cli_state *cli, const char *netname, uint32 type)
3267 {
3268         /* only support disk shares */
3269         if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3270                 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3271                 return False;
3272         }
3273
3274         /* skip builtin shares */
3275         /* FIXME: should print$ be added too ? */
3276         if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") || 
3277             strequal(netname,"global")) 
3278                 return False;
3279
3280         if (opt_exclude && in_list(netname, opt_exclude, False)) {
3281                 printf("excluding  [%s]\n", netname);
3282                 return False;
3283         }
3284
3285         return check_share_availability(cli, netname);
3286 }
3287
3288 /** 
3289  * Migrate shares from a remote RPC server to the local RPC srever
3290  *
3291  * All parameters are provided by the run_rpc_command function, except for
3292  * argc, argv which are passes through. 
3293  *
3294  * @param domain_sid The domain sid acquired from the remote server
3295  * @param cli A cli_state connected to the server.
3296  * @param mem_ctx Talloc context, destoyed on completion of the function.
3297  * @param argc  Standard main() style argc
3298  * @param argv  Standard main() style argv.  Initial components are already
3299  *              stripped
3300  *
3301  * @return Normal NTSTATUS return.
3302  **/
3303
3304 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3305                                                 const char *domain_name, 
3306                                                 struct cli_state *cli,
3307                                                 struct rpc_pipe_client *pipe_hnd,
3308                                                 TALLOC_CTX *mem_ctx, 
3309                                                 int argc,
3310                                                 const char **argv)
3311 {
3312         NTSTATUS result;
3313         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3314         union srvsvc_NetShareCtr ctr_src;
3315         uint32 i;
3316         struct rpc_pipe_client *srvsvc_pipe = NULL;
3317         struct cli_state *cli_dst = NULL;
3318         uint32 level = 502; /* includes secdesc */
3319         uint32 numentries;
3320
3321         result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src, 
3322                                                         &numentries);
3323         if (!NT_STATUS_IS_OK(result))
3324                 goto done;
3325
3326         /* connect destination PI_SRVSVC */
3327         nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3328         if (!NT_STATUS_IS_OK(nt_status))
3329                 return nt_status;
3330
3331
3332         for (i = 0; i < numentries; i++) {
3333                 uint32 *pparm_error = NULL;
3334                 union srvsvc_NetShareInfo info;
3335
3336                 /* reset error-code */
3337                 nt_status = NT_STATUS_UNSUCCESSFUL;
3338
3339                 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name, 
3340                                                                 ctr_src.ctr502->array[i].type))
3341                                                                 
3342                         continue;
3343
3344                 /* finally add the share on the dst server */ 
3345
3346                 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n", 
3347                         ctr_src.ctr502->array[i].name, 
3348                         ctr_src.ctr502->array[i].path, 
3349                         ctr_src.ctr502->array[i].comment);
3350
3351                 info.info502 = &ctr_src.ctr502->array[i];
3352
3353                 result = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx, NULL, 
3354                                                    502, info, &pparm_error);
3355         
3356                 if (NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_COLLISION)) {
3357                         printf("           [%s] does already exist\n", ctr_src.ctr502->array[i].name);
3358                         continue;
3359                 }
3360
3361                 if (!NT_STATUS_IS_OK(result)) {
3362                         printf("cannot add share: %s\n", nt_errstr(result));
3363                         goto done;
3364                 }
3365
3366         }
3367
3368         nt_status = NT_STATUS_OK;
3369
3370 done:
3371         if (cli_dst) {
3372                 cli_shutdown(cli_dst);
3373         }
3374
3375         return nt_status;
3376
3377 }
3378
3379 /** 
3380  * Migrate shares from a rpc-server to another
3381  *
3382  * @param argc  Standard main() style argc
3383  * @param argv  Standard main() style argv.  Initial components are already
3384  *              stripped
3385  *
3386  * @return A shell status integer (0 for success)
3387  **/
3388 static int rpc_share_migrate_shares(int argc, const char **argv)
3389 {
3390
3391         if (!opt_host) {
3392                 printf("no server to migrate\n");
3393                 return -1;
3394         }
3395
3396         return run_rpc_command(NULL, PI_SRVSVC, 0, 
3397                                rpc_share_migrate_shares_internals,
3398                                argc, argv);
3399 }
3400
3401 /**
3402  * Copy a file/dir 
3403  *
3404  * @param f     file_info
3405  * @param mask  current search mask
3406  * @param state arg-pointer
3407  *
3408  **/
3409 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3410 {
3411         static NTSTATUS nt_status;
3412         static struct copy_clistate *local_state;
3413         static fstring filename, new_mask;
3414         fstring dir;
3415         char *old_dir;
3416
3417         local_state = (struct copy_clistate *)state;
3418         nt_status = NT_STATUS_UNSUCCESSFUL;
3419
3420         if (strequal(f->name, ".") || strequal(f->name, ".."))
3421                 return;
3422
3423         DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3424
3425         /* DIRECTORY */
3426         if (f->mode & aDIR) {
3427
3428                 DEBUG(3,("got dir: %s\n", f->name));
3429
3430                 fstrcpy(dir, local_state->cwd);
3431                 fstrcat(dir, "\\");
3432                 fstrcat(dir, f->name);
3433
3434                 switch (net_mode_share)
3435                 {
3436                 case NET_MODE_SHARE_MIGRATE:
3437                         /* create that directory */
3438                         nt_status = net_copy_file(local_state->mem_ctx,
3439                                                   local_state->cli_share_src,
3440                                                   local_state->cli_share_dst,
3441                                                   dir, dir,
3442                                                   opt_acls? True : False,
3443                                                   opt_attrs? True : False,
3444                                                   opt_timestamps? True : False,
3445                                                   False);
3446                         break;
3447                 default:
3448                         d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3449                         return;
3450                 }
3451
3452                 if (!NT_STATUS_IS_OK(nt_status)) 
3453                         printf("could not handle dir %s: %s\n", 
3454                                 dir, nt_errstr(nt_status));
3455
3456                 /* search below that directory */
3457                 fstrcpy(new_mask, dir);
3458                 fstrcat(new_mask, "\\*");
3459
3460                 old_dir = local_state->cwd;
3461                 local_state->cwd = dir;
3462                 if (!sync_files(local_state, new_mask))
3463                         printf("could not handle files\n");
3464                 local_state->cwd = old_dir;
3465
3466                 return;
3467         }
3468
3469
3470         /* FILE */
3471         fstrcpy(filename, local_state->cwd);
3472         fstrcat(filename, "\\");
3473         fstrcat(filename, f->name);
3474
3475         DEBUG(3,("got file: %s\n", filename));
3476
3477         switch (net_mode_share)
3478         {
3479         case NET_MODE_SHARE_MIGRATE:
3480                 nt_status = net_copy_file(local_state->mem_ctx, 
3481                                           local_state->cli_share_src, 
3482                                           local_state->cli_share_dst, 
3483                                           filename, filename, 
3484                                           opt_acls? True : False, 
3485                                           opt_attrs? True : False,
3486                                           opt_timestamps? True: False,
3487                                           True);
3488                 break;
3489         default:
3490                 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3491                 return;
3492         }
3493
3494         if (!NT_STATUS_IS_OK(nt_status)) 
3495                 printf("could not handle file %s: %s\n", 
3496                         filename, nt_errstr(nt_status));
3497
3498 }
3499
3500 /**
3501  * sync files, can be called recursivly to list files 
3502  * and then call copy_fn for each file 
3503  *
3504  * @param cp_clistate   pointer to the copy_clistate we work with
3505  * @param mask          the current search mask
3506  *
3507  * @return              Boolean result
3508  **/
3509 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3510 {
3511
3512         DEBUG(3,("calling cli_list with mask: %s\n", mask));
3513
3514         if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3515                 d_fprintf(stderr, "listing %s failed with error: %s\n", 
3516                         mask, cli_errstr(cp_clistate->cli_share_src));
3517                 return False;
3518         }
3519
3520         return True;
3521 }
3522
3523
3524 /**
3525  * Set the top level directory permissions before we do any further copies.
3526  * Should set up ACL inheritance.
3527  **/
3528
3529 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate, 
3530                                 const char *sharename)
3531 {
3532         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3533
3534         switch (net_mode_share) {
3535         case NET_MODE_SHARE_MIGRATE:
3536                 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3537                 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3538                                                 cp_clistate->cli_share_src, 
3539                                                 cp_clistate->cli_share_dst,
3540                                                 "\\", "\\",
3541                                                 opt_acls? True : False, 
3542                                                 opt_attrs? True : False,
3543                                                 opt_timestamps? True: False,
3544                                                 False);
3545                 break;
3546         default:
3547                 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3548                 break;
3549         }
3550
3551         if (!NT_STATUS_IS_OK(nt_status))  {
3552                 printf("Could handle directory attributes for top level directory of share %s. Error %s\n", 
3553                         sharename, nt_errstr(nt_status));
3554                 return False;
3555         }
3556
3557         return True;
3558 }
3559
3560 /** 
3561  * Sync all files inside a remote share to another share (over smb)
3562  *
3563  * All parameters are provided by the run_rpc_command function, except for
3564  * argc, argv which are passes through. 
3565  *
3566  * @param domain_sid The domain sid acquired from the remote server
3567  * @param cli A cli_state connected to the server.
3568  * @param mem_ctx Talloc context, destoyed on completion of the function.
3569  * @param argc  Standard main() style argc
3570  * @param argv  Standard main() style argv.  Initial components are already
3571  *              stripped
3572  *
3573  * @return Normal NTSTATUS return.
3574  **/
3575
3576 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3577                                                 const char *domain_name, 
3578                                                 struct cli_state *cli,
3579                                                 struct rpc_pipe_client *pipe_hnd,
3580                                                 TALLOC_CTX *mem_ctx,
3581                                                 int argc,
3582                                                 const char **argv)
3583 {
3584         NTSTATUS result;
3585         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3586         union srvsvc_NetShareCtr ctr_src;
3587         uint32 i;
3588         uint32 level = 502;
3589         struct copy_clistate cp_clistate;
3590         BOOL got_src_share = False;
3591         BOOL got_dst_share = False;
3592         pstring mask = "\\*";
3593         char *dst = NULL;
3594         uint32 numentries;
3595
3596         dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3597
3598         result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src, 
3599                                                         &numentries);
3600
3601         if (!NT_STATUS_IS_OK(result))
3602                 goto done;
3603
3604         for (i = 0; i < numentries; i++) {
3605                 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name,
3606                                                         ctr_src.ctr502->array[i].type))
3607                         continue;
3608
3609                 /* one might not want to mirror whole discs :) */
3610                 if (strequal(ctr_src.ctr502->array[i].name, "print$") || ctr_src.ctr502->array[i].name[1] == '$') {
3611                         d_printf("skipping   [%s]: builtin/hidden share\n", ctr_src.ctr502->array[i].name);
3612                         continue;
3613                 }
3614
3615                 switch (net_mode_share)
3616                 {
3617                 case NET_MODE_SHARE_MIGRATE:
3618                         printf("syncing");
3619                         break;
3620                 default:
3621                         d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3622                         break;
3623                 }
3624                 printf("    [%s] files and directories %s ACLs, %s DOS Attributes %s\n", 
3625                         ctr_src.ctr502->array[i].name, 
3626                         opt_acls ? "including" : "without", 
3627                         opt_attrs ? "including" : "without",
3628                         opt_timestamps ? "(preserving timestamps)" : "");
3629
3630                 cp_clistate.mem_ctx = mem_ctx;
3631                 cp_clistate.cli_share_src = NULL;
3632                 cp_clistate.cli_share_dst = NULL;
3633                 cp_clistate.cwd = NULL;
3634                 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3635
3636                 /* open share source */
3637                 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3638                                                &cli->dest_ip, cli->desthost,
3639                                                    ctr_src.ctr502->array[i].name, "A:");
3640                 if (!NT_STATUS_IS_OK(nt_status))
3641                         goto done;
3642
3643                 got_src_share = True;
3644
3645                 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3646                         /* open share destination */
3647                         nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3648                                                        NULL, dst, ctr_src.ctr502->array[i].name, "A:");
3649                         if (!NT_STATUS_IS_OK(nt_status))
3650                                 goto done;
3651
3652                         got_dst_share = True;
3653                 }
3654
3655                 if (!copy_top_level_perms(&cp_clistate, ctr_src.ctr502->array[i].name)) {
3656                         d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", ctr_src.ctr502->array[i].name);
3657                         nt_status = NT_STATUS_UNSUCCESSFUL;
3658                         goto done;
3659                 }
3660
3661                 if (!sync_files(&cp_clistate, mask)) {
3662                         d_fprintf(stderr, "could not handle files for share: %s\n", 
3663                                           ctr_src.ctr502->array[i].name);
3664                         nt_status = NT_STATUS_UNSUCCESSFUL;
3665                         goto done;
3666                 }
3667         }
3668
3669         nt_status = NT_STATUS_OK;
3670
3671 done:
3672
3673         if (got_src_share)
3674                 cli_shutdown(cp_clistate.cli_share_src);
3675
3676         if (got_dst_share)
3677                 cli_shutdown(cp_clistate.cli_share_dst);
3678
3679         return nt_status;
3680
3681 }
3682
3683 static int rpc_share_migrate_files(int argc, const char **argv)
3684 {
3685
3686         if (!opt_host) {
3687                 printf("no server to migrate\n");
3688                 return -1;
3689         }
3690
3691         return run_rpc_command(NULL, PI_SRVSVC, 0, 
3692                                rpc_share_migrate_files_internals,
3693                                argc, argv);
3694 }
3695
3696 /** 
3697  * Migrate share-ACLs from a remote RPC server to the local RPC srever
3698  *
3699  * All parameters are provided by the run_rpc_command function, except for
3700  * argc, argv which are passes through. 
3701  *
3702  * @param domain_sid The domain sid acquired from the remote server
3703  * @param cli A cli_state connected to the server.
3704  * @param mem_ctx Talloc context, destoyed on completion of the function.
3705  * @param argc  Standard main() style argc
3706  * @param argv  Standard main() style argv.  Initial components are already
3707  *              stripped
3708  *
3709  * @return Normal NTSTATUS return.
3710  **/
3711
3712 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3713                                                 const char *domain_name, 
3714                                                 struct cli_state *cli,
3715                                                 struct rpc_pipe_client *pipe_hnd,
3716                                                 TALLOC_CTX *mem_ctx, 
3717                                                 int argc,
3718                                                 const char **argv)
3719 {
3720         NTSTATUS result;
3721         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3722         union srvsvc_NetShareCtr ctr_src;
3723         union srvsvc_NetShareInfo info;
3724         uint32 i;
3725         struct rpc_pipe_client *srvsvc_pipe = NULL;
3726         struct cli_state *cli_dst = NULL;
3727         uint32 level = 502; /* includes secdesc */
3728         uint32 numentries;
3729         uint32 *parm_error = NULL;
3730
3731         result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src,
3732                                                         &numentries);
3733
3734         if (!NT_STATUS_IS_OK(result))
3735                 goto done;
3736
3737         /* connect destination PI_SRVSVC */
3738         nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3739         if (!NT_STATUS_IS_OK(nt_status))
3740                 return nt_status;
3741
3742
3743         for (i = 0; i < numentries; i++) {
3744                 /* reset error-code */
3745                 nt_status = NT_STATUS_UNSUCCESSFUL;
3746
3747                 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name, ctr_src.ctr502->array[i].type))
3748                         continue;
3749
3750                 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n", 
3751                         ctr_src.ctr502->array[i].name, 
3752                         ctr_src.ctr502->array[i].path, 
3753                         ctr_src.ctr502->array[i].comment);
3754
3755                 if (opt_verbose)
3756                         display_sec_desc(ctr_src.ctr502->array[i].sd);
3757
3758                 /* init info */
3759                 ZERO_STRUCT(info);
3760
3761                 /* finally modify the share on the dst server */
3762                 result = rpccli_srvsvc_NetShareSetInfo(
3763                         srvsvc_pipe, mem_ctx, NULL, argv[0], level, info,
3764                         &parm_error);
3765         
3766                 if (!NT_STATUS_IS_OK(result)) {
3767                         printf("cannot set share-acl: %s\n", nt_errstr(result));
3768                         goto done;
3769                 }
3770
3771         }
3772
3773         nt_status = NT_STATUS_OK;
3774
3775 done:
3776         if (cli_dst) {
3777                 cli_shutdown(cli_dst);
3778         }
3779
3780         return nt_status;
3781
3782 }
3783
3784 /** 
3785  * Migrate share-acls from a rpc-server to another
3786  *
3787  * @param argc  Standard main() style argc
3788  * @param argv  Standard main() style argv.  Initial components are already
3789  *              stripped
3790  *
3791  * @return A shell status integer (0 for success)
3792  **/
3793 static int rpc_share_migrate_security(int argc, const char **argv)
3794 {
3795
3796         if (!opt_host) {
3797                 printf("no server to migrate\n");
3798                 return -1;
3799         }
3800
3801         return run_rpc_command(NULL, PI_SRVSVC, 0, 
3802                                rpc_share_migrate_security_internals,
3803                                argc, argv);
3804 }
3805
3806 /** 
3807  * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3808  * from one server to another
3809  *
3810  * @param argc  Standard main() style argc
3811  * @param argv  Standard main() style argv.  Initial components are already
3812  *              stripped
3813  *
3814  * @return A shell status integer (0 for success)
3815  *
3816  **/
3817 static int rpc_share_migrate_all(int argc, const char **argv)
3818 {
3819         int ret;
3820
3821         if (!opt_host) {
3822                 printf("no server to migrate\n");
3823                 return -1;
3824         }
3825
3826         /* order is important. we don't want to be locked out by the share-acl
3827          * before copying files - gd */
3828         
3829         ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3830         if (ret)
3831                 return ret;
3832
3833         ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3834         if (ret)
3835                 return ret;
3836         
3837         return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3838 }
3839
3840
3841 /** 
3842  * 'net rpc share migrate' entrypoint.
3843  * @param argc  Standard main() style argc
3844  * @param argv  Standard main() style argv.  Initial components are already
3845  *              stripped
3846  **/
3847 static int rpc_share_migrate(int argc, const char **argv)
3848 {
3849
3850         struct functable func[] = {
3851                 {"all",         rpc_share_migrate_all},
3852                 {"files",       rpc_share_migrate_files},
3853                 {"help",        rpc_share_usage},
3854                 {"security",    rpc_share_migrate_security},
3855                 {"shares",      rpc_share_migrate_shares},
3856                 {NULL, NULL}
3857         };
3858
3859         net_mode_share = NET_MODE_SHARE_MIGRATE;
3860
3861         return net_run_function(argc, argv, func, rpc_share_usage);
3862 }
3863
3864 struct full_alias {
3865         DOM_SID sid;
3866         uint32 num_members;
3867         DOM_SID *members;
3868 };
3869
3870 static int num_server_aliases;
3871 static struct full_alias *server_aliases;
3872
3873 /*
3874  * Add an alias to the static list.
3875  */
3876 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3877 {
3878         if (server_aliases == NULL)
3879                 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3880
3881         server_aliases[num_server_aliases] = *alias;
3882         num_server_aliases += 1;
3883 }
3884
3885 /*
3886  * For a specific domain on the server, fetch all the aliases
3887  * and their members. Add all of them to the server_aliases.
3888  */
3889
3890 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3891                                         TALLOC_CTX *mem_ctx,
3892                                         POLICY_HND *connect_pol,
3893                                         const DOM_SID *domain_sid)
3894 {
3895         uint32 start_idx, max_entries, num_entries, i;
3896         struct acct_info *groups;
3897         NTSTATUS result;
3898         POLICY_HND domain_pol;
3899
3900         /* Get domain policy handle */
3901         
3902         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3903                                       MAXIMUM_ALLOWED_ACCESS,
3904                                       domain_sid, &domain_pol);
3905         if (!NT_STATUS_IS_OK(result))
3906                 return result;
3907
3908         start_idx = 0;
3909         max_entries = 250;
3910
3911         do {
3912                 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
3913                                                   &start_idx, max_entries,
3914                                                   &groups, &num_entries);
3915
3916                 for (i = 0; i < num_entries; i++) {
3917
3918                         POLICY_HND alias_pol;
3919                         struct full_alias alias;
3920                         DOM_SID *members;
3921                         int j;
3922
3923                         result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
3924                                                      MAXIMUM_ALLOWED_ACCESS,
3925                                                      groups[i].rid,
3926                                                      &alias_pol);
3927                         if (!NT_STATUS_IS_OK(result))
3928                                 goto done;
3929
3930                         result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
3931                                                          &alias_pol,
3932                                                          &alias.num_members,
3933                                                          &members);
3934                         if (!NT_STATUS_IS_OK(result))
3935                                 goto done;
3936
3937                         result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
3938                         if (!NT_STATUS_IS_OK(result))
3939                                 goto done;
3940
3941                         alias.members = NULL;
3942
3943                         if (alias.num_members > 0) {
3944                                 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3945
3946                                 for (j = 0; j < alias.num_members; j++)
3947                                         sid_copy(&alias.members[j],
3948                                                  &members[j]);
3949                         }
3950
3951                         sid_copy(&alias.sid, domain_sid);
3952                         sid_append_rid(&alias.sid, groups[i].rid);
3953
3954                         push_alias(mem_ctx, &alias);
3955                 }
3956         } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3957
3958         result = NT_STATUS_OK;
3959
3960  done:
3961         rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
3962
3963         return result;
3964 }
3965
3966 /*
3967  * Dump server_aliases as names for debugging purposes.
3968  */
3969
3970 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
3971                                 const char *domain_name,
3972                                 struct cli_state *cli,
3973                                 struct rpc_pipe_client *pipe_hnd,
3974                                 TALLOC_CTX *mem_ctx, 
3975                                 int argc,
3976                                 const char **argv)
3977 {
3978         int i;
3979         NTSTATUS result;
3980         POLICY_HND lsa_pol;
3981
3982         result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True, 
3983                                      SEC_RIGHTS_MAXIMUM_ALLOWED,
3984                                      &lsa_pol);
3985         if (!NT_STATUS_IS_OK(result))
3986                 return result;
3987
3988         for (i=0; i<num_server_aliases; i++) {
3989                 char **names;
3990                 char **domains;
3991                 enum lsa_SidType *types;
3992                 int j;
3993
3994                 struct full_alias *alias = &server_aliases[i];
3995
3996                 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3997                                              &alias->sid,
3998                                              &domains, &names, &types);
3999                 if (!NT_STATUS_IS_OK(result))
4000                         continue;
4001
4002                 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4003
4004                 if (alias->num_members == 0) {
4005                         DEBUG(1, ("\n"));
4006                         continue;
4007                 }
4008
4009                 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4010                                              alias->num_members,
4011                                              alias->members,
4012                                              &domains, &names, &types);
4013
4014                 if (!NT_STATUS_IS_OK(result) &&
4015                     !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4016                         continue;
4017
4018                 for (j=0; j<alias->num_members; j++)
4019                         DEBUG(1, ("%s\\%s (%d); ",
4020                                   domains[j] ? domains[j] : "*unknown*", 
4021                                   names[j] ? names[j] : "*unknown*",types[j]));
4022                 DEBUG(1, ("\n"));
4023         }
4024
4025         rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4026
4027         return NT_STATUS_OK;
4028 }
4029
4030 /*
4031  * Fetch a list of all server aliases and their members into
4032  * server_aliases.
4033  */
4034
4035 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4036                                         const char *domain_name,
4037                                         struct cli_state *cli,
4038                                         struct rpc_pipe_client *pipe_hnd,
4039                                         TALLOC_CTX *mem_ctx, 
4040                                         int argc,
4041                                         const char **argv)
4042 {
4043         NTSTATUS result;
4044         POLICY_HND connect_pol;
4045
4046         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS, 
4047                                   &connect_pol);
4048
4049         if (!NT_STATUS_IS_OK(result))
4050                 goto done;
4051         
4052         result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4053                                           &global_sid_Builtin);
4054
4055         if (!NT_STATUS_IS_OK(result))
4056                 goto done;
4057         
4058         result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4059                                           domain_sid);
4060
4061         rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4062  done:
4063         return result;
4064 }
4065
4066 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4067 {
4068         token->num_sids = 4;
4069
4070         token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4071
4072         token->user_sids[0] = *user_sid;
4073         sid_copy(&token->user_sids[1], &global_sid_World);
4074         sid_copy(&token->user_sids[2], &global_sid_Network);
4075         sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4076 }
4077
4078 static void free_user_token(NT_USER_TOKEN *token)
4079 {
4080         SAFE_FREE(token->user_sids);
4081 }
4082
4083 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4084 {
4085         int i;
4086
4087         for (i=0; i<token->num_sids; i++) {
4088                 if (sid_compare(sid, &token->user_sids[i]) == 0)
4089                         return True;
4090         }
4091         return False;
4092 }
4093
4094 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4095 {
4096         if (is_sid_in_token(token, sid))
4097                 return;
4098
4099         token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4100         if (!token->user_sids) {
4101                 return;
4102         }
4103
4104         sid_copy(&token->user_sids[token->num_sids], sid);
4105
4106         token->num_sids += 1;
4107 }
4108
4109 struct user_token {
4110         fstring name;
4111         NT_USER_TOKEN token;
4112 };
4113
4114 static void dump_user_token(struct user_token *token)
4115 {
4116         int i;
4117
4118         d_printf("%s\n", token->name);
4119
4120         for (i=0; i<token->token.num_sids; i++) {
4121                 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4122         }
4123 }
4124
4125 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4126 {
4127         int i;
4128
4129         for (i=0; i<alias->num_members; i++) {
4130                 if (sid_compare(sid, &alias->members[i]) == 0)
4131                         return True;
4132         }
4133
4134         return False;
4135 }
4136
4137 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4138 {
4139         int i;
4140
4141         for (i=0; i<num_server_aliases; i++) {
4142                 if (is_alias_member(&sid, &server_aliases[i]))
4143                         add_sid_to_token(token, &server_aliases[i].sid);
4144         }
4145 }
4146
4147 /*
4148  * We got a user token with all the SIDs we can know about without asking the
4149  * server directly. These are the user and domain group sids. All of these can
4150  * be members of aliases. So scan the list of aliases for each of the SIDs and
4151  * add them to the token.
4152  */
4153
4154 static void collect_alias_memberships(NT_USER_TOKEN *token)
4155 {
4156         int num_global_sids = token->num_sids;
4157         int i;
4158
4159         for (i=0; i<num_global_sids; i++) {
4160                 collect_sid_memberships(token, token->user_sids[i]);
4161         }
4162 }
4163
4164 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4165 {
4166         struct winbindd_request request;
4167         struct winbindd_response response;
4168         fstring full_name;
4169         NSS_STATUS result;
4170
4171         DOM_SID user_sid;
4172
4173         int i;
4174
4175         fstr_sprintf(full_name, "%s%c%s",
4176                      domain, *lp_winbind_separator(), user);
4177
4178         /* First let's find out the user sid */
4179
4180         ZERO_STRUCT(request);
4181         ZERO_STRUCT(response);
4182
4183         fstrcpy(request.data.name.dom_name, domain);
4184         fstrcpy(request.data.name.name, user);
4185
4186         result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4187
4188         if (result != NSS_STATUS_SUCCESS) {
4189                 DEBUG(1, ("winbind could not find %s\n", full_name));
4190                 return False;
4191         }
4192
4193         if (response.data.sid.type != SID_NAME_USER) {
4194                 DEBUG(1, ("%s is not a user\n", full_name));
4195                 return False;
4196         }
4197
4198         string_to_sid(&user_sid, response.data.sid.sid);
4199
4200         init_user_token(token, &user_sid);
4201
4202         /* And now the groups winbind knows about */
4203
4204         ZERO_STRUCT(response);
4205
4206         fstrcpy(request.data.username, full_name);
4207
4208         result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4209
4210         if (result != NSS_STATUS_SUCCESS) {
4211                 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4212                 return False;
4213         }
4214
4215         for (i = 0; i < response.data.num_entries; i++) {
4216                 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4217                 DOM_SID sid;
4218
4219                 struct winbindd_request sidrequest;
4220                 struct winbindd_response sidresponse;
4221
4222                 ZERO_STRUCT(sidrequest);
4223                 ZERO_STRUCT(sidresponse);
4224
4225                 sidrequest.data.gid = gid;
4226
4227                 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4228                                           &sidrequest, &sidresponse);
4229
4230                 if (result != NSS_STATUS_SUCCESS) {
4231                         DEBUG(1, ("winbind could not find SID of gid %d\n",
4232                                   gid));
4233                         return False;
4234                 }
4235
4236                 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4237
4238                 string_to_sid(&sid, sidresponse.data.sid.sid);
4239                 add_sid_to_token(token, &sid);
4240         }
4241
4242         SAFE_FREE(response.extra_data.data);
4243
4244         return True;
4245 }
4246         
4247 /**
4248  * Get a list of all user tokens we want to look at
4249  **/
4250
4251 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4252 {
4253         struct winbindd_request request;
4254         struct winbindd_response response;
4255         const char *extra_data;
4256         fstring name;
4257         int i;
4258         struct user_token *result;
4259
4260         if (lp_winbind_use_default_domain() &&
4261             (opt_target_workgroup == NULL)) {
4262                 d_fprintf(stderr, "winbind use default domain = yes set, "
4263                          "please specify a workgroup\n");
4264                 return False;
4265         }
4266
4267         /* Send request to winbind daemon */
4268
4269         ZERO_STRUCT(request);
4270         ZERO_STRUCT(response);
4271         
4272         if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4273             NSS_STATUS_SUCCESS)
4274                 return False;
4275
4276         /* Look through extra data */
4277
4278         if (!response.extra_data.data)
4279                 return False;
4280
4281         extra_data = (const char *)response.extra_data.data;
4282         *num_tokens = 0;
4283
4284         while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4285                 *num_tokens += 1;
4286         }
4287
4288         result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4289
4290         if (result == NULL) {
4291                 DEBUG(1, ("Could not malloc sid array\n"));
4292                 return False;
4293         }
4294
4295         extra_data = (const char *)response.extra_data.data;
4296         i=0;
4297
4298         while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4299
4300                 fstring domain, user;
4301                 char *p;
4302
4303                 fstrcpy(result[i].name, name);
4304
4305                 p = strchr(name, *lp_winbind_separator());
4306
4307                 DEBUG(3, ("%s\n", name));
4308
4309                 if (p == NULL) {
4310                         fstrcpy(domain, opt_target_workgroup);
4311                         fstrcpy(user, name);
4312                 } else {
4313                         *p++ = '\0';
4314                         fstrcpy(domain, name);
4315                         strupper_m(domain);
4316                         fstrcpy(user, p);
4317                 }
4318
4319                 get_user_sids(domain, user, &(result[i].token));
4320                 i+=1;
4321         }
4322         
4323         SAFE_FREE(response.extra_data.data);
4324
4325         *user_tokens = result;
4326
4327         return True;
4328 }
4329
4330 static BOOL get_user_tokens_from_file(FILE *f,
4331                                       int *num_tokens,
4332                                       struct user_token **tokens)
4333 {
4334         struct user_token *token = NULL;
4335
4336         while (!feof(f)) {
4337                 fstring line;
4338
4339                 if (fgets(line, sizeof(line)-1, f) == NULL) {
4340                         return True;
4341                 }
4342
4343                 if (line[strlen(line)-1] == '\n')
4344                         line[strlen(line)-1] = '\0';
4345
4346                 if (line[0] == ' ') {
4347                         /* We have a SID */
4348
4349                         DOM_SID sid;
4350                         string_to_sid(&sid, &line[1]);
4351
4352                         if (token == NULL) {
4353                                 DEBUG(0, ("File does not begin with username"));
4354                                 return False;
4355                         }
4356
4357                         add_sid_to_token(&token->token, &sid);
4358                         continue;
4359                 }
4360
4361                 /* And a new user... */
4362
4363                 *num_tokens += 1;
4364                 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4365                 if (*tokens == NULL) {
4366                         DEBUG(0, ("Could not realloc tokens\n"));
4367                         return False;
4368                 }
4369
4370                 token = &((*tokens)[*num_tokens-1]);
4371
4372                 fstrcpy(token->name, line);
4373                 token->token.num_sids = 0;
4374                 token->token.user_sids = NULL;
4375                 continue;
4376         }
4377         
4378         return False;
4379 }
4380
4381
4382 /*
4383  * Show the list of all users that have access to a share
4384  */
4385
4386 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4387                         TALLOC_CTX *mem_ctx,
4388                         const char *netname,
4389                         int num_tokens,
4390                         struct user_token *tokens)
4391 {
4392         int fnum;
4393         SEC_DESC *share_sd = NULL;
4394         SEC_DESC *root_sd = NULL;
4395         struct cli_state *cli = pipe_hnd->cli;
4396         int i;
4397         union srvsvc_NetShareInfo info;
4398         NTSTATUS result;
4399         uint16 cnum;
4400
4401         result = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx, NULL, netname,
4402                                                502, &info);
4403
4404         if (!NT_STATUS_IS_OK(result)) {
4405                 DEBUG(1, ("Coult not query secdesc for share %s\n",
4406                           netname));
4407                 return;
4408         }
4409
4410         share_sd = info.info502->sd;
4411         if (share_sd == NULL) {
4412                 DEBUG(1, ("Got no secdesc for share %s\n",
4413                           netname));
4414         }
4415
4416         cnum = cli->cnum;
4417
4418         if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4419                 return;
4420         }
4421
4422         fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4423
4424         if (fnum != -1) {
4425                 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4426         }
4427
4428         for (i=0; i<num_tokens; i++) {
4429                 uint32 acc_granted;
4430                 NTSTATUS status;
4431
4432                 if (share_sd != NULL) {
4433                         if (!se_access_check(share_sd, &tokens[i].token,
4434                                              1, &acc_granted, &status)) {
4435                                 DEBUG(1, ("Could not check share_sd for "
4436                                           "user %s\n",
4437                                           tokens[i].name));
4438                                 continue;
4439                         }
4440
4441                         if (!NT_STATUS_IS_OK(status))
4442                                 continue;
4443                 }
4444
4445                 if (root_sd == NULL) {
4446                         d_printf(" %s\n", tokens[i].name);
4447                         continue;
4448                 }
4449
4450                 if (!se_access_check(root_sd, &tokens[i].token,
4451                                      1, &acc_granted, &status)) {
4452                         DEBUG(1, ("Could not check root_sd for user %s\n",
4453                                   tokens[i].name));
4454                         continue;
4455                 }
4456
4457                 if (!NT_STATUS_IS_OK(status))
4458                         continue;
4459
4460                 d_printf(" %s\n", tokens[i].name);
4461         }
4462
4463         if (fnum != -1)
4464                 cli_close(cli, fnum);
4465         cli_tdis(cli);
4466         cli->cnum = cnum;
4467         
4468         return;
4469 }
4470
4471 struct share_list {
4472         int num_shares;
4473         char **shares;
4474 };
4475
4476 static void collect_share(const char *name, uint32 m,
4477                           const char *comment, void *state)
4478 {
4479         struct share_list *share_list = (struct share_list *)state;
4480
4481         if (m != STYPE_DISKTREE)
4482                 return;
4483
4484         share_list->num_shares += 1;
4485         share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4486         if (!share_list->shares) {
4487                 share_list->num_shares = 0;
4488                 return;
4489         }
4490         share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4491 }
4492
4493 static void rpc_share_userlist_usage(void)
4494 {
4495         return;
4496 }
4497         
4498 /** 
4499  * List shares on a remote RPC server, including the security descriptors
4500  *
4501  * All parameters are provided by the run_rpc_command function, except for
4502  * argc, argv which are passes through. 
4503  *
4504  * @param domain_sid The domain sid acquired from the remote server
4505  * @param cli A cli_state connected to the server.
4506  * @param mem_ctx Talloc context, destoyed on completion of the function.
4507  * @param argc  Standard main() style argc
4508  * @param argv  Standard main() style argv.  Initial components are already
4509  *              stripped
4510  *
4511  * @return Normal NTSTATUS return.
4512  **/
4513
4514 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4515                                                 const char *domain_name,
4516                                                 struct cli_state *cli,
4517                                                 struct rpc_pipe_client *pipe_hnd,
4518                                                 TALLOC_CTX *mem_ctx,
4519                                                 int argc,
4520                                                 const char **argv)
4521 {
4522         int ret;
4523         BOOL r;
4524         uint32 hnd;
4525         uint32 i;
4526         FILE *f;
4527
4528         struct user_token *tokens = NULL;
4529         int num_tokens = 0;
4530
4531         struct share_list share_list;
4532
4533         if (argc > 1) {
4534                 rpc_share_userlist_usage();
4535                 return NT_STATUS_UNSUCCESSFUL;
4536         }
4537
4538         if (argc == 0) {
4539                 f = stdin;
4540         } else {
4541                 f = fopen(argv[0], "r");
4542         }
4543
4544         if (f == NULL) {
4545                 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4546                 return NT_STATUS_UNSUCCESSFUL;
4547         }
4548
4549         r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4550
4551         if (f != stdin)
4552                 fclose(f);
4553
4554         if (!r) {
4555                 DEBUG(0, ("Could not read users from file\n"));
4556                 return NT_STATUS_UNSUCCESSFUL;
4557         }
4558
4559         for (i=0; i<num_tokens; i++)
4560                 collect_alias_memberships(&tokens[i].token);
4561
4562         hnd = 0;
4563         share_list.num_shares = 0;
4564         share_list.shares = NULL;
4565
4566         ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4567
4568         if (ret == -1) {
4569                 DEBUG(0, ("Error returning browse list: %s\n",
4570                           cli_errstr(cli)));
4571                 goto done;
4572         }
4573
4574         for (i = 0; i < share_list.num_shares; i++) {
4575                 char *netname = share_list.shares[i];
4576
4577                 if (netname[strlen(netname)-1] == '$')
4578                         continue;
4579
4580                 d_printf("%s\n", netname);
4581
4582                 show_userlist(pipe_hnd, mem_ctx, netname,
4583                               num_tokens, tokens);
4584         }
4585  done:
4586         for (i=0; i<num_tokens; i++) {
4587                 free_user_token(&tokens[i].token);
4588         }
4589         SAFE_FREE(tokens);
4590         SAFE_FREE(share_list.shares);
4591
4592         return NT_STATUS_OK;
4593 }
4594
4595 static int rpc_share_allowedusers(int argc, const char **argv)
4596 {
4597         int result;
4598
4599         result = run_rpc_command(NULL, PI_SAMR, 0,
4600                                  rpc_aliaslist_internals,
4601                                  argc, argv);
4602         if (result != 0)
4603                 return result;
4604
4605         result = run_rpc_command(NULL, PI_LSARPC, 0,
4606                                  rpc_aliaslist_dump,
4607                                  argc, argv);
4608         if (result != 0)
4609                 return result;
4610
4611         return run_rpc_command(NULL, PI_SRVSVC, 0,
4612                                rpc_share_allowedusers_internals,
4613                                argc, argv);
4614 }
4615
4616 int net_usersidlist(int argc, const char **argv)
4617 {
4618         int num_tokens = 0;
4619         struct user_token *tokens = NULL;
4620         int i;
4621
4622         if (argc != 0) {
4623                 net_usersidlist_usage(argc, argv);
4624                 return 0;
4625         }
4626
4627         if (!get_user_tokens(&num_tokens, &tokens)) {
4628                 DEBUG(0, ("Could not get the user/sid list\n"));
4629                 return 0;
4630         }
4631
4632         for (i=0; i<num_tokens; i++) {
4633                 dump_user_token(&tokens[i]);
4634                 free_user_token(&tokens[i].token);
4635         }
4636
4637         SAFE_FREE(tokens);
4638         return 1;
4639 }
4640
4641 int net_usersidlist_usage(int argc, const char **argv)
4642 {
4643         d_printf("net usersidlist\n"
4644                  "\tprints out a list of all users the running winbind knows\n"
4645                  "\tabout, together with all their SIDs. This is used as\n"
4646                  "\tinput to the 'net rpc share allowedusers' command.\n\n");
4647
4648         net_common_flags_usage(argc, argv);
4649         return -1;
4650 }
4651
4652 /** 
4653  * 'net rpc share' entrypoint.
4654  * @param argc  Standard main() style argc
4655  * @param argv  Standard main() style argv.  Initial components are already
4656  *              stripped
4657  **/
4658
4659 int net_rpc_share(int argc, const char **argv) 
4660 {
4661         struct functable func[] = {
4662                 {"add", rpc_share_add},
4663                 {"delete", rpc_share_delete},
4664                 {"allowedusers", rpc_share_allowedusers},
4665                 {"migrate", rpc_share_migrate},
4666                 {"list", rpc_share_list},
4667                 {NULL, NULL}
4668         };
4669
4670         if (argc == 0)
4671                 return run_rpc_command(NULL, PI_SRVSVC, 0, 
4672                                        rpc_share_list_internals,
4673                                        argc, argv);
4674
4675         return net_run_function(argc, argv, func, rpc_share_usage);
4676 }
4677
4678 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4679                                   struct rpc_sh_ctx *ctx,
4680                                   struct rpc_pipe_client *pipe_hnd,
4681                                   int argc, const char **argv)
4682 {
4683         return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4684                                         ctx->cli, pipe_hnd, mem_ctx,
4685                                         argc, argv);
4686 }
4687
4688 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4689                                  struct rpc_sh_ctx *ctx,
4690                                  struct rpc_pipe_client *pipe_hnd,
4691                                  int argc, const char **argv)
4692 {
4693         union srvsvc_NetShareInfo info;
4694         struct srvsvc_NetShareInfo2 info2;
4695         NTSTATUS result;
4696         uint32 *parm_error = NULL;
4697
4698         if ((argc < 2) || (argc > 3)) {
4699                 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4700                           ctx->whoami);
4701                 return NT_STATUS_INVALID_PARAMETER;
4702         }
4703
4704         info.info2 = &info2;
4705         info2.name = argv[0];
4706         info2.type = STYPE_DISKTREE;
4707         info2.comment = (argc == 3) ? argv[2] : "";
4708
4709         result = rpccli_srvsvc_NetShareAdd(
4710                 pipe_hnd, mem_ctx, NULL, 2, info, &parm_error); 
4711                                              
4712         return result;
4713 }
4714
4715 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4716                                     struct rpc_sh_ctx *ctx,
4717                                     struct rpc_pipe_client *pipe_hnd,
4718                                     int argc, const char **argv)
4719 {
4720         NTSTATUS result;
4721
4722         if (argc != 1) {
4723                 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4724                 return NT_STATUS_INVALID_PARAMETER;
4725         }
4726
4727         result = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx, NULL, argv[0], 0);
4728         return result;
4729 }
4730
4731 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4732                                   struct rpc_sh_ctx *ctx,
4733                                   struct rpc_pipe_client *pipe_hnd,
4734                                   int argc, const char **argv)
4735 {
4736         union srvsvc_NetShareInfo info;
4737         NTSTATUS result;
4738
4739         if (argc != 1) {
4740                 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4741                 return NT_STATUS_INVALID_PARAMETER;
4742         }
4743
4744         result = rpccli_srvsvc_NetShareGetInfo(
4745                 pipe_hnd, mem_ctx, NULL, argv[0], 2, &info);
4746         if (!NT_STATUS_IS_OK(result)) {
4747                 goto done;
4748         }
4749
4750         d_printf("Name:     %s\n", info.info2->name);
4751         d_printf("Comment:  %s\n", info.info2->comment);
4752         d_printf("Path:     %s\n", info.info2->path);
4753         d_printf("Password: %s\n", info.info2->password);
4754
4755  done:
4756         return result;
4757 }
4758
4759 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4760                                       struct rpc_sh_ctx *ctx)
4761 {
4762         static struct rpc_sh_cmd cmds[] = {
4763
4764         { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4765           "List available shares" },
4766
4767         { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4768           "Add a share" },
4769
4770         { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4771           "Delete a share" },
4772
4773         { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4774           "Get information about a share" },
4775
4776         { NULL, NULL, 0, NULL, NULL }
4777         };
4778
4779         return cmds;
4780 }
4781
4782 /****************************************************************************/
4783
4784 static int rpc_file_usage(int argc, const char **argv)
4785 {
4786         return net_help_file(argc, argv);
4787 }
4788
4789 /** 
4790  * Close a file on a remote RPC server
4791  *
4792  * All parameters are provided by the run_rpc_command function, except for
4793  * argc, argv which are passes through. 
4794  *
4795  * @param domain_sid The domain sid acquired from the remote server
4796  * @param cli A cli_state connected to the server.
4797  * @param mem_ctx Talloc context, destoyed on completion of the function.
4798  * @param argc  Standard main() style argc
4799  * @param argv  Standard main() style argv.  Initial components are already
4800  *              stripped
4801  *
4802  * @return Normal NTSTATUS return.
4803  **/
4804 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4805                                         const char *domain_name, 
4806                                         struct cli_state *cli,
4807                                         struct rpc_pipe_client *pipe_hnd,
4808                                         TALLOC_CTX *mem_ctx,
4809                                         int argc,
4810                                         const char **argv)
4811 {
4812         NTSTATUS result;
4813         result = rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx, NULL, atoi(argv[0]));
4814         return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4815 }
4816
4817 /** 
4818  * Close a file on a remote RPC server
4819  *
4820  * @param argc  Standard main() style argc
4821  * @param argv  Standard main() style argv.  Initial components are already
4822  *              stripped
4823  *
4824  * @return A shell status integer (0 for success)
4825  **/
4826 static int rpc_file_close(int argc, const char **argv)
4827 {
4828         if (argc < 1) {
4829                 DEBUG(1, ("No fileid given on close\n"));
4830                 return(rpc_file_usage(argc, argv));
4831         }
4832
4833         return run_rpc_command(NULL, PI_SRVSVC, 0, 
4834                                rpc_file_close_internals,
4835                                argc, argv);
4836 }
4837
4838 /** 
4839  * Formatted print of open file info 
4840  *
4841  * @param info3  FILE_INFO_3 contents
4842  * @param str3   strings for FILE_INFO_3
4843  **/
4844
4845 static void display_file_info_3( struct srvsvc_NetFileInfo3 *info3 )
4846 {
4847         d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4848                  info3->fid, info3->user, info3->permissions, info3->num_locks, info3->path);
4849 }
4850
4851 /** 
4852  * List open files on a remote RPC server
4853  *
4854  * All parameters are provided by the run_rpc_command function, except for
4855  * argc, argv which are passes through. 
4856  *
4857  * @param domain_sid The domain sid acquired from the remote server
4858  * @param cli A cli_state connected to the server.
4859  * @param mem_ctx Talloc context, destoyed on completion of the function.
4860  * @param argc  Standard main() style argc
4861  * @param argv  Standard main() style argv.  Initial components are already
4862  *              stripped
4863  *
4864  * @return Normal NTSTATUS return.
4865  **/
4866
4867 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4868                                         const char *domain_name, 
4869                                         struct cli_state *cli,
4870                                         struct rpc_pipe_client *pipe_hnd,
4871                                         TALLOC_CTX *mem_ctx,
4872                                         int argc,
4873                                         const char **argv)
4874 {
4875         union srvsvc_NetFileCtr ctr;
4876         NTSTATUS result;
4877         uint32 hnd;
4878         uint32 *phnd = &hnd;
4879         uint32 preferred_len = 0xffffffff, i;
4880         const char *username=NULL;
4881         uint32 level = 3;
4882         uint32 numentries;
4883
4884         hnd = 0;
4885
4886         /* if argc > 0, must be user command */
4887         if (argc > 0)
4888                 username = smb_xstrdup(argv[0]);
4889                 
4890         result = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx, NULL, NULL,
4891                                            username, &level, &ctr,
4892                                            preferred_len, &numentries, &phnd);
4893
4894         if (!NT_STATUS_IS_OK(result))
4895                 goto done;
4896
4897         /* Display results */
4898
4899         d_printf(
4900                  "\nEnumerating open files on remote server:\n\n"\
4901                  "\nFileId  Opened by            Perms  Locks  Path"\
4902                  "\n------  ---------            -----  -----  ---- \n");
4903         for (i = 0; i < numentries; i++)
4904                 display_file_info_3(&ctr.ctr3->array[i]);
4905  done:
4906         return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4907 }
4908
4909 /** 
4910  * List files for a user on a remote RPC server
4911  *
4912  * @param argc  Standard main() style argc
4913  * @param argv  Standard main() style argv.  Initial components are already
4914  *              stripped
4915  *
4916  * @return A shell status integer (0 for success)
4917  **/
4918
4919 static int rpc_file_user(int argc, const char **argv)
4920 {
4921         if (argc < 1) {
4922                 DEBUG(1, ("No username given\n"));
4923                 return(rpc_file_usage(argc, argv));
4924         }
4925
4926         return run_rpc_command(NULL, PI_SRVSVC, 0, 
4927                                rpc_file_list_internals,
4928                                argc, argv);
4929 }
4930
4931 /** 
4932  * 'net rpc file' entrypoint.
4933  * @param argc  Standard main() style argc
4934  * @param argv  Standard main() style argv.  Initial components are already
4935  *              stripped
4936  **/
4937
4938 int net_rpc_file(int argc, const char **argv) 
4939 {
4940         struct functable func[] = {
4941                 {"close", rpc_file_close},
4942                 {"user", rpc_file_user},
4943 #if 0
4944                 {"info", rpc_file_info},
4945 #endif
4946                 {NULL, NULL}
4947         };
4948
4949         if (argc == 0)
4950                 return run_rpc_command(NULL, PI_SRVSVC, 0, 
4951                                        rpc_file_list_internals,
4952                                        argc, argv);
4953
4954         return net_run_function(argc, argv, func, rpc_file_usage);
4955 }
4956
4957 /** 
4958  * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4959  *
4960  * All parameters are provided by the run_rpc_command function, except for
4961  * argc, argv which are passed through. 
4962  *
4963  * @param domain_sid The domain sid aquired from the remote server
4964  * @param cli A cli_state connected to the server.
4965  * @param mem_ctx Talloc context, destoyed on compleation of the function.
4966  * @param argc  Standard main() style argc
4967  * @param argv  Standard main() style argv.  Initial components are already
4968  *              stripped
4969  *
4970  * @return Normal NTSTATUS return.
4971  **/
4972
4973 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, 
4974                                         const char *domain_name, 
4975                                         struct cli_state *cli, 
4976                                         struct rpc_pipe_client *pipe_hnd,
4977                                         TALLOC_CTX *mem_ctx, 
4978                                         int argc,
4979                                         const char **argv) 
4980 {
4981         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4982         
4983         result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL);
4984         
4985         if (NT_STATUS_IS_OK(result)) {
4986                 d_printf("\nShutdown successfully aborted\n");
4987                 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4988         } else
4989                 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4990         
4991         return result;
4992 }
4993
4994 /** 
4995  * ABORT the shutdown of a remote RPC Server,  over winreg pipe
4996  *
4997  * All parameters are provided by the run_rpc_command function, except for
4998  * argc, argv which are passed through. 
4999  *
5000  * @param domain_sid The domain sid aquired from the remote server
5001  * @param cli A cli_state connected to the server.
5002  * @param mem_ctx Talloc context, destoyed on compleation of the function.
5003  * @param argc  Standard main() style argc
5004  * @param argv  Standard main() style argv.  Initial components are already
5005  *              stripped
5006  *
5007  * @return Normal NTSTATUS return.
5008  **/
5009
5010 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid, 
5011                                                 const char *domain_name, 
5012                                                 struct cli_state *cli, 
5013                                                 struct rpc_pipe_client *pipe_hnd,
5014                                                 TALLOC_CTX *mem_ctx, 
5015                                                 int argc,
5016                                                 const char **argv) 
5017 {
5018         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5019         
5020         result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL);
5021         
5022         if (NT_STATUS_IS_OK(result)) {
5023                 d_printf("\nShutdown successfully aborted\n");
5024                 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5025         } else
5026                 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5027         
5028         return result;
5029 }
5030
5031 /** 
5032  * ABORT the Shut down of a remote RPC server
5033  *
5034  * @param argc  Standard main() style argc
5035  * @param argv  Standard main() style argv.  Initial components are already
5036  *              stripped
5037  *
5038  * @return A shell status integer (0 for success)
5039  **/
5040
5041 static int rpc_shutdown_abort(int argc, const char **argv) 
5042 {
5043         int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0, 
5044                                  rpc_shutdown_abort_internals,
5045                                  argc, argv);
5046
5047         if (rc == 0)
5048                 return rc;
5049
5050         DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5051
5052         return run_rpc_command(NULL, PI_WINREG, 0, 
5053                                rpc_reg_shutdown_abort_internals,
5054                                argc, argv);
5055 }
5056
5057 /** 
5058  * Shut down a remote RPC Server via initshutdown pipe
5059  *
5060  * All parameters are provided by the run_rpc_command function, except for
5061  * argc, argv which are passes through. 
5062  *
5063  * @param domain_sid The domain sid aquired from the remote server
5064  * @param cli A cli_state connected to the server.
5065  * @param mem_ctx Talloc context, destoyed on compleation of the function.
5066  * @param argc  Standard main() style argc
5067  * @param argc  Standard main() style argv.  Initial components are already
5068  *              stripped
5069  *
5070  * @return Normal NTSTATUS return.
5071  **/
5072
5073 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid, 
5074                                                 const char *domain_name, 
5075                                                 struct cli_state *cli, 
5076                                                 struct rpc_pipe_client *pipe_hnd,
5077                                                 TALLOC_CTX *mem_ctx, 
5078                                                 int argc,
5079                                                 const char **argv) 
5080 {
5081         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5082         const char *msg = "This machine will be shutdown shortly";
5083         uint32 timeout = 20;
5084         struct initshutdown_String msg_string;
5085         struct initshutdown_String_sub s;
5086
5087         if (opt_comment) {
5088                 msg = opt_comment;
5089         }
5090         if (opt_timeout) {
5091                 timeout = opt_timeout;
5092         }
5093
5094         s.name = msg;
5095         msg_string.name = &s;
5096
5097         /* create an entry */
5098         result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5099                         &msg_string, timeout, opt_force, opt_reboot);
5100
5101         if (NT_STATUS_IS_OK(result)) {
5102                 d_printf("\nShutdown of remote machine succeeded\n");
5103                 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5104         } else {
5105                 DEBUG(1,("Shutdown of remote machine failed!\n"));
5106         }
5107         return result;
5108 }
5109
5110 /** 
5111  * Shut down a remote RPC Server via winreg pipe
5112  *
5113  * All parameters are provided by the run_rpc_command function, except for
5114  * argc, argv which are passes through. 
5115  *
5116  * @param domain_sid The domain sid aquired from the remote server
5117  * @param cli A cli_state connected to the server.
5118  * @param mem_ctx Talloc context, destoyed on compleation of the function.
5119  * @param argc  Standard main() style argc
5120  * @param argc  Standard main() style argv.  Initial components are already
5121  *              stripped
5122  *
5123  * @return Normal NTSTATUS return.
5124  **/
5125
5126 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid, 
5127                                                 const char *domain_name, 
5128                                                 struct cli_state *cli, 
5129                                                 struct rpc_pipe_client *pipe_hnd,
5130                                                 TALLOC_CTX *mem_ctx, 
5131                                                 int argc,
5132                                                 const char **argv) 
5133 {
5134         const char *msg = "This machine will be shutdown shortly";
5135         uint32 timeout = 20;
5136         struct initshutdown_String msg_string;
5137         struct initshutdown_String_sub s;
5138         NTSTATUS result;
5139
5140         if (opt_comment) {
5141                 msg = opt_comment;
5142         }
5143         s.name = msg;
5144         msg_string.name = &s;
5145
5146         if (opt_timeout) {
5147                 timeout = opt_timeout;
5148         }
5149
5150         /* create an entry */
5151         result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5152                         &msg_string, timeout, opt_force, opt_reboot);
5153
5154         if (NT_STATUS_IS_OK(result)) {
5155                 d_printf("\nShutdown of remote machine succeeded\n");
5156         } else {
5157                 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5158                 if ( W_ERROR_EQUAL(ntstatus_to_werror(result),WERR_MACHINE_LOCKED) )
5159                         d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5160                 else
5161                         d_fprintf(stderr, "\nresult was: %s\n", nt_errstr(result));
5162         }
5163
5164         return result;
5165 }
5166
5167 /** 
5168  * Shut down a remote RPC server
5169  *
5170  * @param argc  Standard main() style argc
5171  * @param argc  Standard main() style argv.  Initial components are already
5172  *              stripped
5173  *
5174  * @return A shell status integer (0 for success)
5175  **/
5176
5177 static int rpc_shutdown(int argc, const char **argv) 
5178 {
5179         int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0, 
5180                                  rpc_init_shutdown_internals,
5181                                  argc, argv);
5182
5183         if (rc) {
5184                 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5185                 rc = run_rpc_command(NULL, PI_WINREG, 0, 
5186                                      rpc_reg_shutdown_internals, argc, argv);
5187         }
5188
5189         return rc;
5190 }
5191
5192 /***************************************************************************
5193   NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5194   
5195  ***************************************************************************/
5196
5197 /**
5198  * Add interdomain trust account to the RPC server.
5199  * All parameters (except for argc and argv) are passed by run_rpc_command
5200  * function.
5201  *
5202  * @param domain_sid The domain sid acquired from the server
5203  * @param cli A cli_state connected to the server.
5204  * @param mem_ctx Talloc context, destoyed on completion of the function.
5205  * @param argc  Standard main() style argc
5206  * @param argc  Standard main() style argv.  Initial components are already
5207  *              stripped
5208  *
5209  * @return normal NTSTATUS return code
5210  */
5211
5212 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, 
5213                                                 const char *domain_name, 
5214                                                 struct cli_state *cli,
5215                                                 struct rpc_pipe_client *pipe_hnd,
5216                                                 TALLOC_CTX *mem_ctx, 
5217                                                 int argc,
5218                                                 const char **argv)
5219 {
5220         POLICY_HND connect_pol, domain_pol, user_pol;
5221         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5222         char *acct_name;
5223         uint32 acb_info;
5224         uint32 unknown, user_rid;
5225
5226         if (argc != 2) {
5227                 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5228                 return NT_STATUS_INVALID_PARAMETER;
5229         }
5230
5231         /* 
5232          * Make valid trusting domain account (ie. uppercased and with '$' appended)
5233          */
5234          
5235         if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5236                 return NT_STATUS_NO_MEMORY;
5237         }
5238
5239         strupper_m(acct_name);
5240
5241         /* Get samr policy handle */
5242         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5243                                   &connect_pol);
5244         if (!NT_STATUS_IS_OK(result)) {
5245                 goto done;
5246         }
5247         
5248         /* Get domain policy handle */
5249         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5250                                       MAXIMUM_ALLOWED_ACCESS,
5251                                       domain_sid, &domain_pol);
5252         if (!NT_STATUS_IS_OK(result)) {
5253                 goto done;
5254         }
5255
5256         /* Create trusting domain's account */
5257         acb_info = ACB_NORMAL; 
5258         unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5259                                  mimir: yes, most probably it is */
5260
5261         result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5262                                           acct_name, acb_info, unknown,
5263                                           &user_pol, &user_rid);
5264         if (!NT_STATUS_IS_OK(result)) {
5265                 goto done;
5266         }
5267
5268         {
5269                 SAM_USERINFO_CTR ctr;
5270                 SAM_USER_INFO_23 p23;
5271                 NTTIME notime;
5272                 char nostr[] = "";
5273                 LOGON_HRS hrs;
5274                 uchar pwbuf[516];
5275
5276                 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5277
5278                 ZERO_STRUCT(ctr);
5279                 ZERO_STRUCT(p23);
5280                 ZERO_STRUCT(notime);
5281                 hrs.max_len = 1260;
5282                 hrs.offset = 0;
5283                 hrs.len = 21;
5284                 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5285                 acb_info = ACB_DOMTRUST;
5286
5287                 init_sam_user_info23A(&p23, &notime, &notime, &notime,
5288                                       &notime, &notime, &notime,
5289                                       nostr, nostr, nostr, nostr, nostr,
5290                                       nostr, nostr, nostr, nostr, nostr,
5291                                       0, 0, acb_info, ACCT_FLAGS, 168, &hrs, 
5292                                       0, 0, (char *)pwbuf);
5293                 ctr.switch_value = 23;
5294                 ctr.info.id23 = &p23;
5295                 p23.passmustchange = 0;
5296
5297                 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5298                                                &cli->user_session_key, &ctr);
5299
5300                 if (!NT_STATUS_IS_OK(result)) {
5301                         DEBUG(0,("Could not set trust account password: %s\n",
5302                                  nt_errstr(result)));
5303                         goto done;
5304                 }
5305         }
5306
5307  done:
5308         SAFE_FREE(acct_name);
5309         return result;
5310 }
5311
5312 /**
5313  * Create interdomain trust account for a remote domain.
5314  *
5315  * @param argc standard argc
5316  * @param argv standard argv without initial components
5317  *
5318  * @return Integer status (0 means success)
5319  **/
5320
5321 static int rpc_trustdom_add(int argc, const char **argv)
5322 {
5323         if (argc > 0) {
5324                 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5325                                        argc, argv);
5326         } else {
5327                 d_printf("Usage: net rpc trustdom add <domain>\n");
5328                 return -1;
5329         }
5330 }
5331
5332
5333 /**
5334  * Remove interdomain trust account from the RPC server.
5335  * All parameters (except for argc and argv) are passed by run_rpc_command
5336  * function.
5337  *
5338  * @param domain_sid The domain sid acquired from the server
5339  * @param cli A cli_state connected to the server.
5340  * @param mem_ctx Talloc context, destoyed on completion of the function.
5341  * @param argc  Standard main() style argc
5342  * @param argc  Standard main() style argv.  Initial components are already
5343  *              stripped
5344  *
5345  * @return normal NTSTATUS return code
5346  */
5347
5348 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid, 
5349                                         const char *domain_name, 
5350                                         struct cli_state *cli,
5351                                         struct rpc_pipe_client *pipe_hnd,
5352                                         TALLOC_CTX *mem_ctx, 
5353                                         int argc,
5354                                         const char **argv)
5355 {
5356         POLICY_HND connect_pol, domain_pol, user_pol;
5357         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5358         char *acct_name;
5359         const char **names;
5360         DOM_SID trust_acct_sid;
5361         uint32 *user_rids, num_rids, *name_types;
5362         uint32 flags = 0x000003e8; /* Unknown */
5363
5364         if (argc != 1) {
5365                 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5366                 return NT_STATUS_INVALID_PARAMETER;
5367         }
5368
5369         /* 
5370          * Make valid trusting domain account (ie. uppercased and with '$' appended)
5371          */
5372         acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5373
5374         if (acct_name == NULL)
5375                 return NT_STATUS_NO_MEMORY;
5376
5377         strupper_m(acct_name);
5378
5379         if ((names = TALLOC_ARRAY(mem_ctx, const char *, 1)) == NULL) {
5380                 return NT_STATUS_NO_MEMORY;
5381         }
5382         names[0] = acct_name;
5383
5384
5385         /* Get samr policy handle */
5386         result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5387                                   &connect_pol);
5388         if (!NT_STATUS_IS_OK(result)) {
5389                 goto done;
5390         }
5391         
5392         /* Get domain policy handle */
5393         result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5394                                       MAXIMUM_ALLOWED_ACCESS,
5395                                       domain_sid, &domain_pol);
5396         if (!NT_STATUS_IS_OK(result)) {
5397                 goto done;
5398         }
5399
5400         result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5401                                        names, &num_rids,
5402                                        &user_rids, &name_types);
5403         
5404         if (!NT_STATUS_IS_OK(result)) {
5405                 goto done;
5406         }
5407
5408         result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5409                                     MAXIMUM_ALLOWED_ACCESS,
5410                                     user_rids[0], &user_pol);
5411
5412         if (!NT_STATUS_IS_OK(result)) {
5413                 goto done;
5414         }
5415
5416         /* append the rid to the domain sid */
5417         sid_copy(&trust_acct_sid, domain_sid);
5418         if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5419                 goto done;
5420         }
5421
5422         /* remove the sid */
5423
5424         result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5425                                                     &trust_acct_sid);
5426
5427         if (!NT_STATUS_IS_OK(result)) {
5428                 goto done;
5429         }
5430
5431         /* Delete user */
5432
5433         result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5434
5435         if (!NT_STATUS_IS_OK(result)) {
5436                 goto done;
5437         }
5438
5439         if (!NT_STATUS_IS_OK(result)) {
5440           DEBUG(0,("Could not set trust account password: %s\n",
5441                    nt_errstr(result)));
5442           goto done;
5443         }
5444
5445  done:
5446         return result;
5447 }
5448
5449 /**
5450  * Delete interdomain trust account for a remote domain.
5451  *
5452  * @param argc standard argc
5453  * @param argv standard argv without initial components
5454  *
5455  * @return Integer status (0 means success)
5456  **/
5457
5458 static int rpc_trustdom_del(int argc, const char **argv)
5459 {
5460         if (argc > 0) {
5461                 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5462                                        argc, argv);
5463         } else {
5464                 d_printf("Usage: net rpc trustdom del <domain>\n");
5465                 return -1;
5466         }
5467 }
5468  
5469
5470 /**
5471  * Establish trust relationship to a trusting domain.
5472  * Interdomain account must already be created on remote PDC.
5473  *
5474  * @param argc standard argc
5475  * @param argv standard argv without initial components
5476  *
5477  * @return Integer status (0 means success)
5478  **/
5479
5480 static int rpc_trustdom_establish(int argc, const char **argv)
5481 {
5482         struct cli_state *cli = NULL;
5483         struct in_addr server_ip;
5484         struct rpc_pipe_client *pipe_hnd = NULL;
5485         POLICY_HND connect_hnd;
5486         TALLOC_CTX *mem_ctx;
5487         NTSTATUS nt_status;
5488         DOM_SID *domain_sid;
5489         
5490         char* domain_name;
5491         char* domain_name_pol;
5492         char* acct_name;
5493         fstring pdc_name;
5494
5495         /*
5496          * Connect to \\server\ipc$ as 'our domain' account with password
5497          */
5498
5499         if (argc != 1) {
5500                 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5501                 return -1;
5502         }
5503
5504         domain_name = smb_xstrdup(argv[0]);
5505         strupper_m(domain_name);
5506
5507         /* account name used at first is our domain's name with '$' */
5508         asprintf(&acct_name, "%s$", lp_workgroup());
5509         strupper_m(acct_name);
5510         
5511         /*
5512          * opt_workgroup will be used by connection functions further,
5513          * hence it should be set to remote domain name instead of ours
5514          */
5515         if (opt_workgroup) {
5516                 opt_workgroup = smb_xstrdup(domain_name);
5517         };
5518         
5519         opt_user_name = acct_name;
5520
5521         /* find the domain controller */
5522         if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5523                 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5524                 return -1;
5525         }
5526
5527         /* connect to ipc$ as username/password */
5528         nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5529         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5530
5531                 /* Is it trusting domain account for sure ? */
5532                 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5533                         nt_errstr(nt_status)));
5534                 return -1;
5535         }
5536
5537         /* store who we connected to */
5538
5539         saf_store( domain_name, pdc_name );
5540         
5541         /*
5542          * Connect to \\server\ipc$ again (this time anonymously)
5543          */
5544         
5545         nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5546         
5547         if (NT_STATUS_IS_ERR(nt_status)) {
5548                 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5549                         domain_name, nt_errstr(nt_status)));
5550         }
5551
5552         /*
5553          * Use NetServerEnum2 to make sure we're talking to a proper server
5554          */
5555          
5556         if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5557                 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5558                          for domain %s\n", domain_name));
5559         }
5560          
5561         if (!(mem_ctx = talloc_init("establishing trust relationship to "
5562                                     "domain %s", domain_name))) {
5563                 DEBUG(0, ("talloc_init() failed\n"));
5564                 cli_shutdown(cli);
5565                 return -1;
5566         }
5567
5568         /*
5569          * Call LsaOpenPolicy and LsaQueryInfo
5570          */
5571          
5572         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5573         if (!pipe_hnd) {
5574                 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5575                 cli_shutdown(cli);
5576                 return -1;
5577         }
5578
5579         nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5580                                          &connect_hnd);
5581         if (NT_STATUS_IS_ERR(nt_status)) {
5582                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5583                         nt_errstr(nt_status)));
5584                 cli_shutdown(cli);
5585                 return -1;
5586         }
5587
5588         /* Querying info level 5 */
5589         
5590         nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5591                                               5 /* info level */,
5592                                               &domain_name_pol, &domain_sid);
5593         if (NT_STATUS_IS_ERR(nt_status)) {
5594                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5595                         nt_errstr(nt_status)));
5596                 cli_shutdown(cli);
5597                 return -1;
5598         }
5599
5600         /* There should be actually query info level 3 (following nt serv behaviour),
5601            but I still don't know if it's _really_ necessary */
5602                         
5603         /*
5604          * Store the password in secrets db
5605          */
5606
5607         if (!secrets_store_trusted_domain_password(domain_name,
5608                                                    opt_password,
5609                                                    domain_sid)) {
5610                 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5611                 cli_shutdown(cli);
5612                 return -1;
5613         }
5614         
5615         /*
5616          * Close the pipes and clean up
5617          */
5618          
5619         nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5620         if (NT_STATUS_IS_ERR(nt_status)) {
5621                 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5622                         nt_errstr(nt_status)));
5623                 cli_shutdown(cli);
5624                 return -1;
5625         }
5626
5627         cli_shutdown(cli);
5628          
5629         talloc_destroy(mem_ctx);
5630          
5631         d_printf("Trust to domain %s established\n", domain_name);
5632         return 0;
5633 }
5634
5635 /**
5636  * Revoke trust relationship to the remote domain
5637  *
5638  * @param argc standard argc
5639  * @param argv standard argv without initial components
5640  *
5641  * @return Integer status (0 means success)
5642  **/
5643
5644 static int rpc_trustdom_revoke(int argc, const char **argv)
5645 {
5646         char* domain_name;
5647
5648         if (argc < 1) return -1;
5649         
5650         /* generate upper cased domain name */
5651         domain_name = smb_xstrdup(argv[0]);
5652         strupper_m(domain_name);
5653
5654         /* delete password of the trust */
5655         if (!trusted_domain_password_delete(domain_name)) {
5656                 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5657                           domain_name));
5658                 return -1;
5659         };
5660         
5661         return 0;
5662 }
5663
5664 /**
5665  * Usage for 'net rpc trustdom' command
5666  *
5667  * @param argc standard argc
5668  * @param argv standard argv without inital components
5669  *
5670  * @return Integer status returned to shell
5671  **/
5672  
5673 static int rpc_trustdom_usage(int argc, const char **argv)
5674 {
5675         d_printf("  net rpc trustdom add \t\t add trusting domain's account\n");
5676         d_printf("  net rpc trustdom del \t\t delete trusting domain's account\n");
5677         d_printf("  net rpc trustdom establish \t establish relationship to trusted domain\n");
5678         d_printf("  net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5679         d_printf("  net rpc trustdom list \t show current interdomain trust relationships\n");
5680         d_printf("  net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5681         return -1;
5682 }
5683
5684
5685 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, 
5686                                         const char *domain_name, 
5687                                         struct cli_state *cli,
5688                                         struct rpc_pipe_client *pipe_hnd,
5689                                         TALLOC_CTX *mem_ctx,
5690                                         int argc,
5691                                         const char **argv)
5692 {
5693         fstring str_sid;
5694         sid_to_string(str_sid, domain_sid);
5695         d_printf("%s\n", str_sid);
5696         return NT_STATUS_OK;
5697 }
5698
5699 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5700 {
5701         fstring ascii_sid, padding;
5702         int pad_len, col_len = 20;
5703
5704         /* convert sid into ascii string */
5705         sid_to_string(ascii_sid, dom_sid);
5706
5707         /* calculate padding space for d_printf to look nicer */
5708         pad_len = col_len - strlen(trusted_dom_name);
5709         padding[pad_len] = 0;
5710         do padding[--pad_len] = ' '; while (pad_len);
5711                         
5712         d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5713 }
5714
5715 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5716                                       TALLOC_CTX *mem_ctx, 
5717                                       POLICY_HND *pol, 
5718                                       DOM_SID dom_sid, 
5719                                       const char *trusted_dom_name)
5720 {
5721         NTSTATUS nt_status;
5722         LSA_TRUSTED_DOMAIN_INFO *info;
5723         char *cleartextpwd = NULL;
5724         DATA_BLOB data;
5725
5726         nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5727         
5728         if (NT_STATUS_IS_ERR(nt_status)) {
5729                 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5730                 nt_errstr(nt_status)));
5731                 goto done;
5732         }
5733
5734         data = data_blob(NULL, info->password.password.length);
5735
5736         memcpy(data.data, info->password.password.data, info->password.password.length);
5737         data.length     = info->password.password.length;
5738                                 
5739         cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5740
5741         if (cleartextpwd == NULL) {
5742                 DEBUG(0,("retrieved NULL password\n"));
5743                 nt_status = NT_STATUS_UNSUCCESSFUL;
5744                 goto done;
5745         }
5746         
5747         if (!secrets_store_trusted_domain_password(trusted_dom_name,
5748                                                    cleartextpwd,
5749                                                    &dom_sid)) {
5750                 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5751                 nt_status = NT_STATUS_UNSUCCESSFUL;
5752                 goto done;
5753         }
5754
5755 #ifdef DEBUG_PASSWORD
5756         DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",  
5757                 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5758 #endif
5759
5760 done:
5761         SAFE_FREE(cleartextpwd);
5762         data_blob_free(&data);
5763
5764         return nt_status;
5765 }
5766
5767 static int rpc_trustdom_vampire(int argc, const char **argv)
5768 {
5769         /* common variables */
5770         TALLOC_CTX* mem_ctx;
5771         struct cli_state *cli = NULL;
5772         struct rpc_pipe_client *pipe_hnd = NULL;
5773         NTSTATUS nt_status;
5774         const char *domain_name = NULL;
5775         DOM_SID *queried_dom_sid;
5776         POLICY_HND connect_hnd;
5777
5778         /* trusted domains listing variables */
5779         unsigned int num_domains, enum_ctx = 0;
5780         int i;
5781         DOM_SID *domain_sids;
5782         char **trusted_dom_names;
5783         fstring pdc_name;
5784         char *dummy;
5785
5786         /*
5787          * Listing trusted domains (stored in secrets.tdb, if local)
5788          */
5789
5790         mem_ctx = talloc_init("trust relationships vampire");
5791
5792         /*
5793          * set domain and pdc name to local samba server (default)
5794          * or to remote one given in command line
5795          */
5796
5797         if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5798                 domain_name = opt_workgroup;
5799                 opt_target_workgroup = opt_workgroup;
5800         } else {
5801                 fstrcpy(pdc_name, global_myname());
5802                 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5803                 opt_target_workgroup = domain_name;
5804         };
5805
5806         /* open \PIPE\lsarpc and open policy handle */
5807         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5808                 DEBUG(0, ("Couldn't connect to domain controller\n"));
5809                 return -1;
5810         };
5811
5812         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5813         if (!pipe_hnd) {
5814                 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5815                         nt_errstr(nt_status) ));
5816                 cli_shutdown(cli);
5817                 return -1;
5818         };
5819
5820         nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5821                                         &connect_hnd);
5822         if (NT_STATUS_IS_ERR(nt_status)) {
5823                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5824                         nt_errstr(nt_status)));
5825                 cli_shutdown(cli);
5826                 return -1;
5827         };
5828
5829         /* query info level 5 to obtain sid of a domain being queried */
5830         nt_status = rpccli_lsa_query_info_policy(
5831                 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */, 
5832                 &dummy, &queried_dom_sid);
5833
5834         if (NT_STATUS_IS_ERR(nt_status)) {
5835                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5836                         nt_errstr(nt_status)));
5837                 cli_shutdown(cli);
5838                 return -1;
5839         }
5840
5841         /*
5842          * Keep calling LsaEnumTrustdom over opened pipe until
5843          * the end of enumeration is reached
5844          */
5845
5846         d_printf("Vampire trusted domains:\n\n");
5847
5848         do {
5849                 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5850                                                    &num_domains,
5851                                                    &trusted_dom_names, &domain_sids);
5852                 
5853                 if (NT_STATUS_IS_ERR(nt_status)) {
5854                         DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5855                                 nt_errstr(nt_status)));
5856                         cli_shutdown(cli);
5857                         return -1;
5858                 };
5859                 
5860                 for (i = 0; i < num_domains; i++) {
5861
5862                         print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5863
5864                         nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd, 
5865                                                            domain_sids[i], trusted_dom_names[i]);
5866                         if (!NT_STATUS_IS_OK(nt_status)) {
5867                                 cli_shutdown(cli);
5868                                 return -1;
5869                         }
5870                 };
5871
5872                 /*
5873                  * in case of no trusted domains say something rather
5874                  * than just display blank line
5875                  */
5876                 if (!num_domains) d_printf("none\n");
5877
5878         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5879
5880         /* close this connection before doing next one */
5881         nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5882         if (NT_STATUS_IS_ERR(nt_status)) {
5883                 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5884                         nt_errstr(nt_status)));
5885                 cli_shutdown(cli);
5886                 return -1;
5887         };
5888
5889         /* close lsarpc pipe and connection to IPC$ */
5890         cli_shutdown(cli);
5891
5892         talloc_destroy(mem_ctx);         
5893         return 0;
5894 }
5895
5896 static int rpc_trustdom_list(int argc, const char **argv)
5897 {
5898         /* common variables */
5899         TALLOC_CTX* mem_ctx;
5900         struct cli_state *cli = NULL, *remote_cli = NULL;
5901         struct rpc_pipe_client *pipe_hnd = NULL;
5902         NTSTATUS nt_status;
5903         const char *domain_name = NULL;
5904         DOM_SID *queried_dom_sid;
5905         fstring padding;
5906         int ascii_dom_name_len;
5907         POLICY_HND connect_hnd;
5908         
5909         /* trusted domains listing variables */
5910         unsigned int num_domains, enum_ctx = 0;
5911         int i, pad_len, col_len = 20;
5912         DOM_SID *domain_sids;
5913         char **trusted_dom_names;
5914         fstring pdc_name;
5915         char *dummy;
5916         
5917         /* trusting domains listing variables */
5918         POLICY_HND domain_hnd;
5919         char **trusting_dom_names;
5920         uint32 *trusting_dom_rids;
5921         
5922         /*
5923          * Listing trusted domains (stored in secrets.tdb, if local)
5924          */
5925
5926         mem_ctx = talloc_init("trust relationships listing");
5927
5928         /*
5929          * set domain and pdc name to local samba server (default)
5930          * or to remote one given in command line
5931          */
5932         
5933         if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5934                 domain_name = opt_workgroup;
5935                 opt_target_workgroup = opt_workgroup;
5936         } else {
5937                 fstrcpy(pdc_name, global_myname());
5938                 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5939                 opt_target_workgroup = domain_name;
5940         };
5941
5942         /* open \PIPE\lsarpc and open policy handle */
5943         if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5944                 DEBUG(0, ("Couldn't connect to domain controller\n"));
5945                 return -1;
5946         };
5947
5948         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5949         if (!pipe_hnd) {
5950                 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5951                         nt_errstr(nt_status) ));
5952                 return -1;
5953         };
5954
5955         nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5956                                         &connect_hnd);
5957         if (NT_STATUS_IS_ERR(nt_status)) {
5958                 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5959                         nt_errstr(nt_status)));
5960                 return -1;
5961         };
5962         
5963         /* query info level 5 to obtain sid of a domain being queried */
5964         nt_status = rpccli_lsa_query_info_policy(
5965                 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */, 
5966                 &dummy, &queried_dom_sid);
5967
5968         if (NT_STATUS_IS_ERR(nt_status)) {
5969                 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5970                         nt_errstr(nt_status)));
5971                 return -1;
5972         }
5973                 
5974         /*
5975          * Keep calling LsaEnumTrustdom over opened pipe until
5976          * the end of enumeration is reached
5977          */
5978          
5979         d_printf("Trusted domains list:\n\n");
5980
5981         do {
5982                 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5983                                                    &num_domains,
5984                                                    &trusted_dom_names, &domain_sids);
5985                 
5986                 if (NT_STATUS_IS_ERR(nt_status)) {
5987                         DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5988                                 nt_errstr(nt_status)));
5989                         return -1;
5990                 };
5991                 
5992                 for (i = 0; i < num_domains; i++) {
5993                         print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5994                 };
5995                 
5996                 /*
5997                  * in case of no trusted domains say something rather
5998                  * than just display blank line
5999                  */
6000                 if (!num_domains) d_printf("none\n");
6001
6002         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6003
6004         /* close this connection before doing next one */
6005         nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6006         if (NT_STATUS_IS_ERR(nt_status)) {
6007                 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6008                         nt_errstr(nt_status)));
6009                 return -1;
6010         };
6011         
6012         cli_rpc_pipe_close(pipe_hnd);
6013
6014         /*
6015          * Listing trusting domains (stored in passdb backend, if local)
6016          */
6017         
6018         d_printf("\nTrusting domains list:\n\n");
6019
6020         /*
6021          * Open \PIPE\samr and get needed policy handles
6022          */
6023         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6024         if (!pipe_hnd) {
6025                 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6026                 return -1;
6027         };
6028         
6029         /* SamrConnect */
6030         nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6031                                                                  &connect_hnd);
6032         if (!NT_STATUS_IS_OK(nt_status)) {
6033                 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6034                         nt_errstr(nt_status)));
6035                 return -1;
6036         };
6037         
6038         /* SamrOpenDomain - we have to open domain policy handle in order to be
6039            able to enumerate accounts*/
6040         nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6041                                          SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6042                                          queried_dom_sid, &domain_hnd);                                                                  
6043         if (!NT_STATUS_IS_OK(nt_status)) {
6044                 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6045                         nt_errstr(nt_status)));
6046                 return -1;
6047         };
6048         
6049         /*
6050          * perform actual enumeration
6051          */
6052          
6053         enum_ctx = 0;   /* reset enumeration context from last enumeration */
6054         do {
6055                         
6056                 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6057                                                     &enum_ctx, ACB_DOMTRUST, 0xffff,
6058                                                     &trusting_dom_names, &trusting_dom_rids,
6059                                                     &num_domains);
6060                 if (NT_STATUS_IS_ERR(nt_status)) {
6061                         DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6062                                 nt_errstr(nt_status)));
6063                         return -1;
6064                 };
6065                 
6066                 for (i = 0; i < num_domains; i++) {
6067
6068                         /*
6069                          * get each single domain's sid (do we _really_ need this ?):
6070                          *  1) connect to domain's pdc
6071                          *  2) query the pdc for domain's sid
6072                          */
6073
6074                         /* get rid of '$' tail */
6075                         ascii_dom_name_len = strlen(trusting_dom_names[i]);
6076                         if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6077                                 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6078                         
6079                         /* calculate padding space for d_printf to look nicer */
6080                         pad_len = col_len - strlen(trusting_dom_names[i]);
6081                         padding[pad_len] = 0;
6082                         do padding[--pad_len] = ' '; while (pad_len);
6083
6084                         /* set opt_* variables to remote domain */
6085                         strupper_m(trusting_dom_names[i]);
6086                         opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6087                         opt_target_workgroup = opt_workgroup;
6088                         
6089                         d_printf("%s%s", trusting_dom_names[i], padding);
6090                         
6091                         /* connect to remote domain controller */
6092                         remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6093                         if (remote_cli) {                       
6094                                 /* query for domain's sid */
6095                                 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6096                                         d_fprintf(stderr, "couldn't get domain's sid\n");
6097
6098                                 cli_shutdown(remote_cli);
6099                         
6100                         } else {
6101                                 d_fprintf(stderr, "domain controller is not responding\n");
6102                         };
6103                 };
6104                 
6105                 if (!num_domains) d_printf("none\n");
6106                 
6107         } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6108
6109         /* close opened samr and domain policy handles */
6110         nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6111         if (!NT_STATUS_IS_OK(nt_status)) {
6112                 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6113         };
6114         
6115         nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6116         if (!NT_STATUS_IS_OK(nt_status)) {
6117                 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6118         };
6119         
6120         /* close samr pipe and connection to IPC$ */
6121         cli_shutdown(cli);
6122
6123         talloc_destroy(mem_ctx);         
6124         return 0;
6125 }
6126
6127 /**
6128  * Entrypoint for 'net rpc trustdom' code
6129  *
6130  * @param argc standard argc
6131  * @param argv standard argv without initial components
6132  *
6133  * @return Integer status (0 means success)
6134  */
6135
6136 static int rpc_trustdom(int argc, const char **argv)
6137 {
6138         struct functable func[] = {
6139                 {"add", rpc_trustdom_add},
6140                 {"del", rpc_trustdom_del},
6141                 {"establish", rpc_trustdom_establish},
6142                 {"revoke", rpc_trustdom_revoke},
6143                 {"help", rpc_trustdom_usage},
6144                 {"list", rpc_trustdom_list},
6145                 {"vampire", rpc_trustdom_vampire},
6146                 {NULL, NULL}
6147         };
6148
6149         if (argc == 0) {
6150                 rpc_trustdom_usage(argc, argv);
6151                 return -1;
6152         }
6153
6154         return (net_run_function(argc, argv, func, rpc_user_usage));
6155 }
6156
6157 /**
6158  * Check if a server will take rpc commands
6159  * @param flags Type of server to connect to (PDC, DMB, localhost)
6160  *              if the host is not explicitly specified
6161  * @return  BOOL (true means rpc supported)
6162  */
6163 BOOL net_rpc_check(unsigned flags)
6164 {
6165         struct cli_state *cli;
6166         BOOL ret = False;
6167         struct in_addr server_ip;
6168         char *server_name = NULL;
6169
6170         /* flags (i.e. server type) may depend on command */
6171         if (!net_find_server(NULL, flags, &server_ip, &server_name))
6172                 return False;
6173
6174         if ((cli = cli_initialise()) == NULL) {
6175                 return False;
6176         }
6177
6178         if (!cli_connect(cli, server_name, &server_ip))
6179                 goto done;
6180         if (!attempt_netbios_session_request(&cli, global_myname(), 
6181                                              server_name, &server_ip))
6182                 goto done;
6183         if (!cli_negprot(cli))
6184                 goto done;
6185         if (cli->protocol < PROTOCOL_NT1)
6186                 goto done;
6187
6188         ret = True;
6189  done:
6190         cli_shutdown(cli);
6191         return ret;
6192 }
6193
6194 /* dump sam database via samsync rpc calls */
6195 static int rpc_samdump(int argc, const char **argv) {
6196                 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6197                                argc, argv);
6198 }
6199
6200 /* syncronise sam database via samsync rpc calls */
6201 static int rpc_vampire(int argc, const char **argv) {
6202         return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6203                                argc, argv);
6204 }
6205
6206 /** 
6207  * Migrate everything from a print-server
6208  *
6209  * @param argc  Standard main() style argc
6210  * @param argv  Standard main() style argv.  Initial components are already
6211  *              stripped
6212  *
6213  * @return A shell status integer (0 for success)
6214  *
6215  * The order is important !
6216  * To successfully add drivers the print-queues have to exist !
6217  * Applying ACLs should be the last step, because you're easily locked out
6218  *
6219  **/
6220 static int rpc_printer_migrate_all(int argc, const char **argv)
6221 {
6222         int ret;
6223
6224         if (!opt_host) {
6225                 printf("no server to migrate\n");
6226                 return -1;
6227         }
6228
6229         ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6230         if (ret)
6231                 return ret;
6232
6233         ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6234         if (ret)
6235                 return ret;
6236
6237         ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6238         if (ret)
6239                 return ret;
6240
6241         ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6242         if (ret)
6243                 return ret;
6244
6245         return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6246
6247 }
6248
6249 /** 
6250  * Migrate print-drivers from a print-server
6251  *
6252  * @param argc  Standard main() style argc
6253  * @param argv  Standard main() style argv.  Initial components are already
6254  *              stripped
6255  *
6256  * @return A shell status integer (0 for success)
6257  **/
6258 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6259 {
6260         if (!opt_host) {
6261                 printf("no server to migrate\n");
6262                 return -1;
6263         }
6264
6265         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6266                                rpc_printer_migrate_drivers_internals,
6267                                argc, argv);
6268 }
6269
6270 /** 
6271  * Migrate print-forms from a print-server
6272  *
6273  * @param argc  Standard main() style argc
6274  * @param argv  Standard main() style argv.  Initial components are already
6275  *              stripped
6276  *
6277  * @return A shell status integer (0 for success)
6278  **/
6279 static int rpc_printer_migrate_forms(int argc, const char **argv)
6280 {
6281         if (!opt_host) {
6282                 printf("no server to migrate\n");
6283                 return -1;
6284         }
6285
6286         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6287                                rpc_printer_migrate_forms_internals,
6288                                argc, argv);
6289 }
6290
6291 /** 
6292  * Migrate printers from a print-server
6293  *
6294  * @param argc  Standard main() style argc
6295  * @param argv  Standard main() style argv.  Initial components are already
6296  *              stripped
6297  *
6298  * @return A shell status integer (0 for success)
6299  **/
6300 static int rpc_printer_migrate_printers(int argc, const char **argv)
6301 {
6302         if (!opt_host) {
6303                 printf("no server to migrate\n");
6304                 return -1;
6305         }
6306
6307         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6308                                rpc_printer_migrate_printers_internals,
6309                                argc, argv);
6310 }
6311
6312 /** 
6313  * Migrate printer-ACLs from a print-server
6314  *
6315  * @param argc  Standard main() style argc
6316  * @param argv  Standard main() style argv.  Initial components are already
6317  *              stripped
6318  *
6319  * @return A shell status integer (0 for success)
6320  **/
6321 static int rpc_printer_migrate_security(int argc, const char **argv)
6322 {
6323         if (!opt_host) {
6324                 printf("no server to migrate\n");
6325                 return -1;
6326         }
6327
6328         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6329                                rpc_printer_migrate_security_internals,
6330                                argc, argv);
6331 }
6332
6333 /** 
6334  * Migrate printer-settings from a print-server
6335  *
6336  * @param argc  Standard main() style argc
6337  * @param argv  Standard main() style argv.  Initial components are already
6338  *              stripped
6339  *
6340  * @return A shell status integer (0 for success)
6341  **/
6342 static int rpc_printer_migrate_settings(int argc, const char **argv)
6343 {
6344         if (!opt_host) {
6345                 printf("no server to migrate\n");
6346                 return -1;
6347         }
6348
6349         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6350                                rpc_printer_migrate_settings_internals,
6351                                argc, argv);
6352 }
6353
6354 /** 
6355  * 'net rpc printer' entrypoint.
6356  * @param argc  Standard main() style argc
6357  * @param argv  Standard main() style argv.  Initial components are already
6358  *              stripped
6359  **/
6360
6361 int rpc_printer_migrate(int argc, const char **argv) 
6362 {
6363
6364         /* ouch: when addriver and setdriver are called from within
6365            rpc_printer_migrate_drivers_internals, the printer-queue already
6366            *has* to exist */
6367
6368         struct functable func[] = {
6369                 {"all",         rpc_printer_migrate_all},
6370                 {"drivers",     rpc_printer_migrate_drivers},
6371                 {"forms",       rpc_printer_migrate_forms},
6372                 {"help",        rpc_printer_usage},
6373                 {"printers",    rpc_printer_migrate_printers},
6374                 {"security",    rpc_printer_migrate_security},
6375                 {"settings",    rpc_printer_migrate_settings},
6376                 {NULL, NULL}
6377         };
6378
6379         return net_run_function(argc, argv, func, rpc_printer_usage);
6380 }
6381
6382
6383 /** 
6384  * List printers on a remote RPC server
6385  *
6386  * @param argc  Standard main() style argc
6387  * @param argv  Standard main() style argv.  Initial components are already
6388  *              stripped
6389  *
6390  * @return A shell status integer (0 for success)
6391  **/
6392 static int rpc_printer_list(int argc, const char **argv)
6393 {
6394
6395         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6396                                rpc_printer_list_internals,
6397                                argc, argv);
6398 }
6399
6400 /** 
6401  * List printer-drivers on a remote RPC server
6402  *
6403  * @param argc  Standard main() style argc
6404  * @param argv  Standard main() style argv.  Initial components are already
6405  *              stripped
6406  *
6407  * @return A shell status integer (0 for success)
6408  **/
6409 static int rpc_printer_driver_list(int argc, const char **argv)
6410 {
6411
6412         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6413                                rpc_printer_driver_list_internals,
6414                                argc, argv);
6415 }
6416
6417 /** 
6418  * Publish printer in ADS via MSRPC
6419  *
6420  * @param argc  Standard main() style argc
6421  * @param argv  Standard main() style argv.  Initial components are already
6422  *              stripped
6423  *
6424  * @return A shell status integer (0 for success)
6425  **/
6426 static int rpc_printer_publish_publish(int argc, const char **argv)
6427 {
6428
6429         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6430                                rpc_printer_publish_publish_internals,
6431                                argc, argv);
6432 }
6433
6434 /** 
6435  * Update printer in ADS via MSRPC
6436  *
6437  * @param argc  Standard main() style argc
6438  * @param argv  Standard main() style argv.  Initial components are already
6439  *              stripped
6440  *
6441  * @return A shell status integer (0 for success)
6442  **/
6443 static int rpc_printer_publish_update(int argc, const char **argv)
6444 {
6445
6446         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6447                                rpc_printer_publish_update_internals,
6448                                argc, argv);
6449 }
6450
6451 /** 
6452  * UnPublish printer in ADS via MSRPC
6453  *
6454  * @param argc  Standard main() style argc
6455  * @param argv  Standard main() style argv.  Initial components are already
6456  *              stripped
6457  *
6458  * @return A shell status integer (0 for success)
6459  **/
6460 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6461 {
6462
6463         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6464                                rpc_printer_publish_unpublish_internals,
6465                                argc, argv);
6466 }
6467
6468 /** 
6469  * List published printers via MSRPC
6470  *
6471  * @param argc  Standard main() style argc
6472  * @param argv  Standard main() style argv.  Initial components are already
6473  *              stripped
6474  *
6475  * @return A shell status integer (0 for success)
6476  **/
6477 static int rpc_printer_publish_list(int argc, const char **argv)
6478 {
6479
6480         return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6481                                rpc_printer_publish_list_internals,
6482                                argc, argv);
6483 }
6484
6485
6486 /** 
6487  * Publish printer in ADS
6488  *
6489  * @param argc  Standard main() style argc
6490  * @param argv  Standard main() style argv.  Initial components are already
6491  *              stripped
6492  *
6493  * @return A shell status integer (0 for success)
6494  **/
6495 static int rpc_printer_publish(int argc, const char **argv)
6496 {
6497
6498         struct functable func[] = {
6499                 {"publish",     rpc_printer_publish_publish},
6500                 {"update",      rpc_printer_publish_update},
6501                 {"unpublish",   rpc_printer_publish_unpublish},
6502                 {"list",        rpc_printer_publish_list},
6503                 {"help",        rpc_printer_usage},
6504                 {NULL, NULL}
6505         };
6506
6507         if (argc == 0)
6508                 return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6509                                rpc_printer_publish_list_internals,
6510                                argc, argv);
6511
6512         return net_run_function(argc, argv, func, rpc_printer_usage);
6513
6514 }
6515
6516
6517 /** 
6518  * Display rpc printer help page.
6519  * @param argc  Standard main() style argc
6520  * @param argv  Standard main() style argv.  Initial components are already
6521  *              stripped
6522  **/
6523 int rpc_printer_usage(int argc, const char **argv)
6524 {
6525         return net_help_printer(argc, argv);
6526 }
6527
6528 /** 
6529  * 'net rpc printer' entrypoint.
6530  * @param argc  Standard main() style argc
6531  * @param argv  Standard main() style argv.  Initial components are already
6532  *              stripped
6533  **/
6534 int net_rpc_printer(int argc, const char **argv) 
6535 {
6536         struct functable func[] = {
6537                 {"list", rpc_printer_list},
6538                 {"migrate", rpc_printer_migrate},
6539                 {"driver", rpc_printer_driver_list},
6540                 {"publish", rpc_printer_publish},
6541                 {NULL, NULL}
6542         };
6543
6544         if (argc == 0)
6545                 return run_rpc_command(NULL, PI_SPOOLSS, 0, 
6546                                rpc_printer_list_internals,
6547                                argc, argv);
6548
6549         return net_run_function(argc, argv, func, rpc_printer_usage);
6550 }
6551
6552 /****************************************************************************/
6553
6554
6555 /** 
6556  * Basic usage function for 'net rpc'
6557  * @param argc  Standard main() style argc
6558  * @param argv  Standard main() style argv.  Initial components are already
6559  *              stripped
6560  **/
6561
6562 int net_rpc_usage(int argc, const char **argv) 
6563 {
6564         d_printf("  net rpc info \t\t\tshow basic info about a domain \n");
6565         d_printf("  net rpc join \t\t\tto join a domain \n");
6566         d_printf("  net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6567         d_printf("  net rpc testjoin \t\ttests that a join is valid\n");
6568         d_printf("  net rpc user \t\t\tto add, delete and list users\n");
6569         d_printf("  net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6570         d_printf("  net rpc group \t\tto list groups\n");
6571         d_printf("  net rpc share \t\tto add, delete, list and migrate shares\n");
6572         d_printf("  net rpc printer \t\tto list and migrate printers\n");
6573         d_printf("  net rpc file \t\t\tto list open files\n");
6574         d_printf("  net rpc changetrustpw \tto change the trust account password\n");
6575         d_printf("  net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6576         d_printf("  net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6577         d_printf("  net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6578         d_printf("  net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6579         d_printf("  net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6580         d_printf("  net rpc shutdown \t\tto shutdown a remote server\n");
6581         d_printf("  net rpc rights\t\tto manage privileges assigned to SIDs\n");
6582         d_printf("  net rpc registry\t\tto manage registry hives\n");
6583         d_printf("  net rpc service\t\tto start, stop and query services\n");
6584         d_printf("  net rpc audit\t\t\tto modify global auditing settings\n");
6585         d_printf("  net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6586         d_printf("\n");
6587         d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6588         d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6589         d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6590         d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6591         d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6592         return -1;
6593 }
6594
6595
6596 /**
6597  * Help function for 'net rpc'.  Calls command specific help if requested
6598  * or displays usage of net rpc
6599  * @param argc  Standard main() style argc
6600  * @param argv  Standard main() style argv.  Initial components are already
6601  *              stripped
6602  **/
6603
6604 int net_rpc_help(int argc, const char **argv)
6605 {
6606         struct functable func[] = {
6607                 {"join", rpc_join_usage},
6608                 {"user", rpc_user_usage},
6609                 {"group", rpc_group_usage},
6610                 {"share", rpc_share_usage},
6611                 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6612                 {"trustdom", rpc_trustdom_usage},
6613                 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6614                 /*{"shutdown", rpc_shutdown_usage}, */
6615                 {"vampire", rpc_vampire_usage},
6616                 {NULL, NULL}
6617         };
6618
6619         if (argc == 0) {
6620                 net_rpc_usage(argc, argv);
6621                 return -1;
6622         }
6623
6624         return (net_run_function(argc, argv, func, rpc_user_usage));
6625 }
6626
6627 /** 
6628  * 'net rpc' entrypoint.
6629  * @param argc  Standard main() style argc
6630  * @param argv  Standard main() style argv.  Initial components are already
6631  *              stripped
6632  **/
6633
6634 int net_rpc(int argc, const char **argv)
6635 {
6636         struct functable func[] = {
6637                 {"audit", net_rpc_audit},
6638                 {"info", net_rpc_info},
6639                 {"join", net_rpc_join},
6640                 {"oldjoin", net_rpc_oldjoin},
6641                 {"testjoin", net_rpc_testjoin},
6642                 {"user", net_rpc_user},
6643                 {"password", rpc_user_password},
6644                 {"group", net_rpc_group},
6645                 {"share", net_rpc_share},
6646                 {"file", net_rpc_file},
6647                 {"printer", net_rpc_printer},
6648                 {"changetrustpw", net_rpc_changetrustpw},
6649                 {"trustdom", rpc_trustdom},
6650                 {"abortshutdown", rpc_shutdown_abort},
6651                 {"shutdown", rpc_shutdown},
6652                 {"samdump", rpc_samdump},
6653                 {"vampire", rpc_vampire},
6654                 {"getsid", net_rpc_getsid},
6655                 {"rights", net_rpc_rights},
6656                 {"service", net_rpc_service},
6657                 {"registry", net_rpc_registry},
6658                 {"shell", net_rpc_shell},
6659                 {"help", net_rpc_help},
6660                 {NULL, NULL}
6661         };
6662         return net_run_function(argc, argv, func, net_rpc_usage);
6663 }