s3-includes: only include ntdomain.h where needed.
[kai/samba.git] / source3 / rpcclient / rpcclient.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4
5    Copyright (C) Tim Potter 2000-2001
6    Copyright (C) Martin Pool 2003
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "popt_common.h"
24 #include "rpcclient.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_lsa_c.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "../librpc/gen_ndr/ndr_netlogon.h"
30 #include "rpc_client/cli_netlogon.h"
31 #include "../libcli/smbreadline/smbreadline.h"
32 #include "../libcli/security/security.h"
33 #include "passdb.h"
34 #include "ntdomain.h"
35
36 enum pipe_auth_type_spnego {
37         PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
38         PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
39         PIPE_AUTH_TYPE_SPNEGO_KRB5
40 };
41
42 struct dom_sid domain_sid;
43
44 static enum dcerpc_AuthType pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
45 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type = 0;
46 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
47 static unsigned int timeout = 0;
48 static enum dcerpc_transport_t default_transport = NCACN_NP;
49
50 struct user_auth_info *rpcclient_auth_info;
51
52 /* List to hold groups of commands.
53  *
54  * Commands are defined in a list of arrays: arrays are easy to
55  * statically declare, and lists are easier to dynamically extend.
56  */
57
58 static struct cmd_list {
59         struct cmd_list *prev, *next;
60         struct cmd_set *cmd_set;
61 } *cmd_list;
62
63 /****************************************************************************
64 handle completion of commands for readline
65 ****************************************************************************/
66 static char **completion_fn(const char *text, int start, int end)
67 {
68 #define MAX_COMPLETIONS 1000
69         char **matches;
70         int i, count=0;
71         struct cmd_list *commands = cmd_list;
72
73 #if 0   /* JERRY */
74         /* FIXME!!!  -- what to do when completing argument? */
75         /* for words not at the start of the line fallback 
76            to filename completion */
77         if (start) 
78                 return NULL;
79 #endif
80
81         /* make sure we have a list of valid commands */
82         if (!commands) {
83                 return NULL;
84         }
85
86         matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
87         if (!matches) {
88                 return NULL;
89         }
90
91         matches[count++] = SMB_STRDUP(text);
92         if (!matches[0]) {
93                 SAFE_FREE(matches);
94                 return NULL;
95         }
96
97         while (commands && count < MAX_COMPLETIONS-1) {
98                 if (!commands->cmd_set) {
99                         break;
100                 }
101
102                 for (i=0; commands->cmd_set[i].name; i++) {
103                         if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
104                                 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
105                         commands->cmd_set[i].ntfn ) || 
106                       ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
107                         commands->cmd_set[i].wfn))) {
108                                 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
109                                 if (!matches[count]) {
110                                         for (i = 0; i < count; i++) {
111                                                 SAFE_FREE(matches[count]);
112                                         }
113                                         SAFE_FREE(matches);
114                                         return NULL;
115                                 }
116                                 count++;
117                         }
118                 }
119                 commands = commands->next;
120         }
121
122         if (count == 2) {
123                 SAFE_FREE(matches[0]);
124                 matches[0] = SMB_STRDUP(matches[1]);
125         }
126         matches[count] = NULL;
127         return matches;
128 }
129
130 static char *next_command (char **cmdstr)
131 {
132         char *command;
133         char                    *p;
134
135         if (!cmdstr || !(*cmdstr))
136                 return NULL;
137
138         p = strchr_m(*cmdstr, ';');
139         if (p)
140                 *p = '\0';
141         command = SMB_STRDUP(*cmdstr);
142         if (p)
143                 *cmdstr = p + 1;
144         else
145                 *cmdstr = NULL;
146
147         return command;
148 }
149
150 /* Fetch the SID for this computer */
151
152 static void fetch_machine_sid(struct cli_state *cli)
153 {
154         struct policy_handle pol;
155         NTSTATUS result = NT_STATUS_OK, status;
156         static bool got_domain_sid;
157         TALLOC_CTX *mem_ctx;
158         struct rpc_pipe_client *lsapipe = NULL;
159         union lsa_PolicyInformation *info = NULL;
160         struct dcerpc_binding_handle *b;
161
162         if (got_domain_sid) return;
163
164         if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
165                 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
166                 goto error;
167         }
168
169         result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
170                                           &lsapipe);
171         if (!NT_STATUS_IS_OK(result)) {
172                 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
173                 goto error;
174         }
175
176         b = lsapipe->binding_handle;
177
178         result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True, 
179                                      SEC_FLAG_MAXIMUM_ALLOWED,
180                                      &pol);
181         if (!NT_STATUS_IS_OK(result)) {
182                 goto error;
183         }
184
185         status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
186                                             &pol,
187                                             LSA_POLICY_INFO_ACCOUNT_DOMAIN,
188                                             &info,
189                                             &result);
190         if (!NT_STATUS_IS_OK(status)) {
191                 result = status;
192                 goto error;
193         }
194         if (!NT_STATUS_IS_OK(result)) {
195                 goto error;
196         }
197
198         got_domain_sid = True;
199         sid_copy(&domain_sid, info->account_domain.sid);
200
201         dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
202         TALLOC_FREE(lsapipe);
203         talloc_destroy(mem_ctx);
204
205         return;
206
207  error:
208
209         if (lsapipe) {
210                 TALLOC_FREE(lsapipe);
211         }
212
213         fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
214
215         if (!NT_STATUS_IS_OK(result)) {
216                 fprintf(stderr, "error: %s\n", nt_errstr(result));
217         }
218
219         exit(1);
220 }
221
222 /* List the available commands on a given pipe */
223
224 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
225                                  int argc, const char **argv)
226 {
227         struct cmd_list *tmp;
228         struct cmd_set *tmp_set;
229         int i;
230
231         /* Usage */
232
233         if (argc != 2) {
234                 printf("Usage: %s <pipe>\n", argv[0]);
235                 return NT_STATUS_OK;
236         }
237
238         /* Help on one command */
239
240         for (tmp = cmd_list; tmp; tmp = tmp->next) 
241         {
242                 tmp_set = tmp->cmd_set;
243
244                 if (!StrCaseCmp(argv[1], tmp_set->name))
245                 {
246                         printf("Available commands on the %s pipe:\n\n", tmp_set->name);
247
248                         i = 0;
249                         tmp_set++;
250                         while(tmp_set->name) {
251                                 printf("%30s", tmp_set->name);
252                                 tmp_set++;
253                                 i++;
254                                 if (i%3 == 0)
255                                         printf("\n");
256                         }
257
258                         /* drop out of the loop */
259                         break;
260                 }
261         }
262         printf("\n\n");
263
264         return NT_STATUS_OK;
265 }
266
267 /* Display help on commands */
268
269 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
270                          int argc, const char **argv)
271 {
272         struct cmd_list *tmp;
273         struct cmd_set *tmp_set;
274
275         /* Usage */
276
277         if (argc > 2) {
278                 printf("Usage: %s [command]\n", argv[0]);
279                 return NT_STATUS_OK;
280         }
281
282         /* Help on one command */
283
284         if (argc == 2) {
285                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
286
287                         tmp_set = tmp->cmd_set;
288
289                         while(tmp_set->name) {
290                                 if (strequal(argv[1], tmp_set->name)) {
291                                         if (tmp_set->usage &&
292                                             tmp_set->usage[0])
293                                                 printf("%s\n", tmp_set->usage);
294                                         else
295                                                 printf("No help for %s\n", tmp_set->name);
296
297                                         return NT_STATUS_OK;
298                                 }
299
300                                 tmp_set++;
301                         }
302                 }
303
304                 printf("No such command: %s\n", argv[1]);
305                 return NT_STATUS_OK;
306         }
307
308         /* List all commands */
309
310         for (tmp = cmd_list; tmp; tmp = tmp->next) {
311
312                 tmp_set = tmp->cmd_set;
313
314                 while(tmp_set->name) {
315
316                         printf("%15s\t\t%s\n", tmp_set->name,
317                                tmp_set->description ? tmp_set->description:
318                                "");
319
320                         tmp_set++;
321                 }
322         }
323
324         return NT_STATUS_OK;
325 }
326
327 /* Change the debug level */
328
329 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
330                                int argc, const char **argv)
331 {
332         if (argc > 2) {
333                 printf("Usage: %s [debuglevel]\n", argv[0]);
334                 return NT_STATUS_OK;
335         }
336
337         if (argc == 2) {
338                 lp_set_cmdline("log level", argv[1]);
339         }
340
341         printf("debuglevel is %d\n", DEBUGLEVEL);
342
343         return NT_STATUS_OK;
344 }
345
346 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
347                          int argc, const char **argv)
348 {
349         exit(0);
350         return NT_STATUS_OK; /* NOTREACHED */
351 }
352
353 static NTSTATUS cmd_set_ss_level(void)
354 {
355         struct cmd_list *tmp;
356
357         /* Close any existing connections not at this level. */
358
359         for (tmp = cmd_list; tmp; tmp = tmp->next) {
360                 struct cmd_set *tmp_set;
361
362                 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
363                         if (tmp_set->rpc_pipe == NULL) {
364                                 continue;
365                         }
366
367                         if ((tmp_set->rpc_pipe->auth->auth_type
368                              != pipe_default_auth_type)
369                             || (tmp_set->rpc_pipe->auth->auth_level
370                                 != pipe_default_auth_level)) {
371                                 TALLOC_FREE(tmp_set->rpc_pipe);
372                                 tmp_set->rpc_pipe = NULL;
373                         }
374                 }
375         }
376         return NT_STATUS_OK;
377 }
378
379 static NTSTATUS cmd_set_transport(void)
380 {
381         struct cmd_list *tmp;
382
383         /* Close any existing connections not at this level. */
384
385         for (tmp = cmd_list; tmp; tmp = tmp->next) {
386                 struct cmd_set *tmp_set;
387
388                 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
389                         if (tmp_set->rpc_pipe == NULL) {
390                                 continue;
391                         }
392
393                         if (tmp_set->rpc_pipe->transport->transport != default_transport) {
394                                 TALLOC_FREE(tmp_set->rpc_pipe);
395                                 tmp_set->rpc_pipe = NULL;
396                         }
397                 }
398         }
399         return NT_STATUS_OK;
400 }
401
402 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
403                          int argc, const char **argv)
404 {
405         const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
406         const char *type = "NTLMSSP";
407
408         pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
409         pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
410
411         if (argc > 2) {
412                 printf("Usage: %s %s\n", argv[0], p);
413                 return NT_STATUS_OK;
414         }
415
416         if (argc == 2) {
417                 type = argv[1];
418                 if (strequal(type, "KRB5")) {
419                         pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
420                 } else if (strequal(type, "KRB5_SPNEGO")) {
421                         pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
422                         pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
423                 } else if (strequal(type, "NTLMSSP")) {
424                         pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
425                 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
426                         pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
427                         pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
428                 } else if (strequal(type, "SCHANNEL")) {
429                         pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
430                 } else {
431                         printf("unknown type %s\n", type);
432                         printf("Usage: %s %s\n", argv[0], p);
433                         return NT_STATUS_INVALID_LEVEL;
434                 }
435         }
436
437         d_printf("Setting %s - sign\n", type);
438
439         return cmd_set_ss_level();
440 }
441
442 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
443                          int argc, const char **argv)
444 {
445         const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
446         const char *type = "NTLMSSP";
447
448         pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
449         pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
450
451         if (argc > 2) {
452                 printf("Usage: %s %s\n", argv[0], p);
453                 return NT_STATUS_OK;
454         }
455
456         if (argc == 2) {
457                 type = argv[1];
458                 if (strequal(type, "KRB5")) {
459                         pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
460                 } else if (strequal(type, "KRB5_SPNEGO")) {
461                         pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
462                         pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
463                 } else if (strequal(type, "NTLMSSP")) {
464                         pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
465                 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
466                         pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
467                         pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
468                 } else if (strequal(type, "SCHANNEL")) {
469                         pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
470                 } else {
471                         printf("unknown type %s\n", type);
472                         printf("Usage: %s %s\n", argv[0], p);
473                         return NT_STATUS_INVALID_LEVEL;
474                 }
475         }
476
477         d_printf("Setting %s - sign and seal\n", type);
478
479         return cmd_set_ss_level();
480 }
481
482 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
483                             int argc, const char **argv)
484 {
485         struct cmd_list *tmp;
486
487         if (argc > 2) {
488                 printf("Usage: %s timeout\n", argv[0]);
489                 return NT_STATUS_OK;
490         }
491
492         if (argc == 2) {
493                 timeout = atoi(argv[1]);
494
495                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
496
497                         struct cmd_set *tmp_set;
498
499                         for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
500                                 if (tmp_set->rpc_pipe == NULL) {
501                                         continue;
502                                 }
503
504                                 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
505                         }
506                 }
507         }
508
509         printf("timeout is %d\n", timeout);
510
511         return NT_STATUS_OK;
512 }
513
514
515 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
516                          int argc, const char **argv)
517 {
518         pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
519         pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
520         pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
521
522         return cmd_set_ss_level();
523 }
524
525 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
526                              int argc, const char **argv)
527 {
528         d_printf("Setting schannel - sign and seal\n");
529         pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
530         pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
531
532         return cmd_set_ss_level();
533 }
534
535 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
536                              int argc, const char **argv)
537 {
538         d_printf("Setting schannel - sign only\n");
539         pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
540         pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
541
542         return cmd_set_ss_level();
543 }
544
545 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
546                                      int argc, const char **argv)
547 {
548         NTSTATUS status;
549
550         if (argc != 2) {
551                 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
552                 return NT_STATUS_OK;
553         }
554
555         if (strequal(argv[1], "NCACN_NP")) {
556                 default_transport = NCACN_NP;
557         } else if (strequal(argv[1], "NCACN_IP_TCP")) {
558                 default_transport = NCACN_IP_TCP;
559         } else {
560                 printf("transport type: %s unknown or not supported\n", argv[1]);
561                 return NT_STATUS_NOT_SUPPORTED;
562         }
563
564         status = cmd_set_transport();
565         if (!NT_STATUS_IS_OK(status)) {
566                 return status;
567         }
568
569         printf("default transport is now: %s\n", argv[1]);
570
571         return NT_STATUS_OK;
572 }
573
574 /* Built in rpcclient commands */
575
576 static struct cmd_set rpcclient_commands[] = {
577
578         { "GENERAL OPTIONS" },
579
580         { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL,     NULL, NULL,   "Get help on commands", "[command]" },
581         { "?",  RPC_RTYPE_NTSTATUS, cmd_help, NULL,       NULL, NULL,   "Get help on commands", "[command]" },
582         { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL,   NULL,       NULL, "Set debug level", "level" },
583         { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL,   NULL,    NULL, "Set debug level", "level" },
584         { "list",       RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL,       NULL, "List available commands on <pipe>", "pipe" },
585         { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL,   NULL,   NULL,   "Exit program", "" },
586         { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL,     NULL, NULL, "Exit program", "" },
587         { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL,     NULL, NULL, "Force RPC pipe connections to be signed", "" },
588         { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL,     NULL, NULL, "Force RPC pipe connections to be sealed", "" },
589         { "schannel", RPC_RTYPE_NTSTATUS, cmd_schannel, NULL,     NULL, NULL,   "Force RPC pipe connections to be sealed with 'schannel'.  Assumes valid machine account to this domain controller.", "" },
590         { "schannelsign", RPC_RTYPE_NTSTATUS, cmd_schannel_sign, NULL,    NULL, NULL, "Force RPC pipe connections to be signed (not sealed) with 'schannel'.  Assumes valid machine account to this domain controller.", "" },
591         { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL,       NULL, NULL, "Set timeout (in milliseconds) for RPC operations", "" },
592         { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL,    NULL, NULL, "Choose ncacn transport for RPC operations", "" },
593         { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL,     NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
594
595         { NULL }
596 };
597
598 static struct cmd_set separator_command[] = {
599         { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL,   NULL, NULL, "----------------------" },
600         { NULL }
601 };
602
603
604 /* Various pipe commands */
605
606 extern struct cmd_set lsarpc_commands[];
607 extern struct cmd_set samr_commands[];
608 extern struct cmd_set spoolss_commands[];
609 extern struct cmd_set netlogon_commands[];
610 extern struct cmd_set srvsvc_commands[];
611 extern struct cmd_set dfs_commands[];
612 extern struct cmd_set ds_commands[];
613 extern struct cmd_set echo_commands[];
614 extern struct cmd_set epmapper_commands[];
615 extern struct cmd_set shutdown_commands[];
616 extern struct cmd_set test_commands[];
617 extern struct cmd_set wkssvc_commands[];
618 extern struct cmd_set ntsvcs_commands[];
619 extern struct cmd_set drsuapi_commands[];
620 extern struct cmd_set eventlog_commands[];
621 extern struct cmd_set winreg_commands[];
622
623 static struct cmd_set *rpcclient_command_list[] = {
624         rpcclient_commands,
625         lsarpc_commands,
626         ds_commands,
627         samr_commands,
628         spoolss_commands,
629         netlogon_commands,
630         srvsvc_commands,
631         dfs_commands,
632         echo_commands,
633         epmapper_commands,
634         shutdown_commands,
635         test_commands,
636         wkssvc_commands,
637         ntsvcs_commands,
638         drsuapi_commands,
639         eventlog_commands,
640         winreg_commands,
641         NULL
642 };
643
644 static void add_command_set(struct cmd_set *cmd_set)
645 {
646         struct cmd_list *entry;
647
648         if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
649                 DEBUG(0, ("out of memory\n"));
650                 return;
651         }
652
653         ZERO_STRUCTP(entry);
654
655         entry->cmd_set = cmd_set;
656         DLIST_ADD(cmd_list, entry);
657 }
658
659
660 /**
661  * Call an rpcclient function, passing an argv array.
662  *
663  * @param cmd Command to run, as a single string.
664  **/
665 static NTSTATUS do_cmd(struct cli_state *cli,
666                        struct user_auth_info *auth_info,
667                        struct cmd_set *cmd_entry,
668                        struct dcerpc_binding *binding,
669                        int argc, char **argv)
670 {
671         NTSTATUS ntresult;
672         WERROR wresult;
673
674         TALLOC_CTX *mem_ctx;
675
676         /* Create mem_ctx */
677
678         if (!(mem_ctx = talloc_init("do_cmd"))) {
679                 DEBUG(0, ("talloc_init() failed\n"));
680                 return NT_STATUS_NO_MEMORY;
681         }
682
683         /* Open pipe */
684
685         if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
686                 switch (pipe_default_auth_type) {
687                 case DCERPC_AUTH_TYPE_NONE:
688                         ntresult = cli_rpc_pipe_open_noauth_transport(
689                                 cli, default_transport,
690                                 cmd_entry->interface,
691                                 &cmd_entry->rpc_pipe);
692                         break;
693                 case DCERPC_AUTH_TYPE_SPNEGO:
694                         switch (pipe_default_auth_spnego_type) {
695                         case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
696                                 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
697                                                 cli, cmd_entry->interface,
698                                                 default_transport,
699                                                 pipe_default_auth_level,
700                                                 get_cmdline_auth_info_domain(auth_info),
701                                                 get_cmdline_auth_info_username(auth_info),
702                                                 get_cmdline_auth_info_password(auth_info),
703                                                 &cmd_entry->rpc_pipe);
704                                 break;
705                         case PIPE_AUTH_TYPE_SPNEGO_KRB5:
706                                 ntresult = cli_rpc_pipe_open_spnego_krb5(
707                                                 cli, cmd_entry->interface,
708                                                 default_transport,
709                                                 pipe_default_auth_level,
710                                                 cli->desthost,
711                                                 NULL, NULL,
712                                                 &cmd_entry->rpc_pipe);
713                                 break;
714                         default:
715                                 ntresult = NT_STATUS_INTERNAL_ERROR;
716                         }
717                         break;
718                 case DCERPC_AUTH_TYPE_NTLMSSP:
719                         ntresult = cli_rpc_pipe_open_ntlmssp(
720                                 cli, cmd_entry->interface,
721                                 default_transport,
722                                 pipe_default_auth_level,
723                                 get_cmdline_auth_info_domain(auth_info),
724                                 get_cmdline_auth_info_username(auth_info),
725                                 get_cmdline_auth_info_password(auth_info),
726                                 &cmd_entry->rpc_pipe);
727                         break;
728                 case DCERPC_AUTH_TYPE_SCHANNEL:
729                         ntresult = cli_rpc_pipe_open_schannel(
730                                 cli, cmd_entry->interface,
731                                 default_transport,
732                                 pipe_default_auth_level,
733                                 get_cmdline_auth_info_domain(auth_info),
734                                 &cmd_entry->rpc_pipe);
735                         break;
736                 case DCERPC_AUTH_TYPE_KRB5:
737                         ntresult = cli_rpc_pipe_open_krb5(
738                                 cli, cmd_entry->interface,
739                                 default_transport,
740                                 pipe_default_auth_level,
741                                 cli->desthost,
742                                 NULL, NULL,
743                                 &cmd_entry->rpc_pipe);
744                         break;
745                 default:
746                         DEBUG(0, ("Could not initialise %s. Invalid "
747                                   "auth type %u\n",
748                                   get_pipe_name_from_syntax(
749                                           talloc_tos(),
750                                           cmd_entry->interface),
751                                   pipe_default_auth_type ));
752                         return NT_STATUS_UNSUCCESSFUL;
753                 }
754                 if (!NT_STATUS_IS_OK(ntresult)) {
755                         DEBUG(0, ("Could not initialise %s. Error was %s\n",
756                                   get_pipe_name_from_syntax(
757                                           talloc_tos(), cmd_entry->interface),
758                                   nt_errstr(ntresult) ));
759                         return ntresult;
760                 }
761
762                 if (ndr_syntax_id_equal(cmd_entry->interface,
763                                         &ndr_table_netlogon.syntax_id)) {
764                         uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
765                         enum netr_SchannelType sec_channel_type;
766                         uchar trust_password[16];
767                         const char *machine_account;
768
769                         if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
770                                                trust_password, &machine_account,
771                                                &sec_channel_type))
772                         {
773                                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
774                         }
775
776                         ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
777                                                 cli->desthost,   /* server name */
778                                                 get_cmdline_auth_info_domain(auth_info),  /* domain */
779                                                 global_myname(), /* client name */
780                                                 machine_account, /* machine account name */
781                                                 trust_password,
782                                                 sec_channel_type,
783                                                 &neg_flags);
784
785                         if (!NT_STATUS_IS_OK(ntresult)) {
786                                 DEBUG(0, ("Could not initialise credentials for %s.\n",
787                                           get_pipe_name_from_syntax(
788                                                   talloc_tos(),
789                                                   cmd_entry->interface)));
790                                 return ntresult;
791                         }
792                 }
793         }
794
795         /* Run command */
796
797         if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
798                 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
799                 if (!NT_STATUS_IS_OK(ntresult)) {
800                         printf("result was %s\n", nt_errstr(ntresult));
801                 }
802         } else {
803                 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
804                 /* print out the DOS error */
805                 if (!W_ERROR_IS_OK(wresult)) {
806                         printf( "result was %s\n", win_errstr(wresult));
807                 }
808                 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
809         }
810
811         /* Cleanup */
812
813         talloc_destroy(mem_ctx);
814
815         return ntresult;
816 }
817
818
819 /**
820  * Process a command entered at the prompt or as part of -c
821  *
822  * @returns The NTSTATUS from running the command.
823  **/
824 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
825                             struct cli_state *cli,
826                             struct dcerpc_binding *binding,
827                             char *cmd)
828 {
829         struct cmd_list *temp_list;
830         NTSTATUS result = NT_STATUS_OK;
831         int ret;
832         int argc;
833         char **argv = NULL;
834
835         if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
836                 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
837                 return NT_STATUS_UNSUCCESSFUL;
838         }
839
840
841         /* Walk through a dlist of arrays of commands. */
842         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
843                 struct cmd_set *temp_set = temp_list->cmd_set;
844
845                 while (temp_set->name) {
846                         if (strequal(argv[0], temp_set->name)) {
847                                 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
848                          !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
849                                         fprintf (stderr, "Invalid command\n");
850                                         goto out_free;
851                                 }
852
853                                 result = do_cmd(cli, auth_info, temp_set,
854                                                 binding, argc, argv);
855
856                                 goto out_free;
857                         }
858                         temp_set++;
859                 }
860         }
861
862         if (argv[0]) {
863                 printf("command not found: %s\n", argv[0]);
864         }
865
866 out_free:
867 /* moved to do_cmd()
868         if (!NT_STATUS_IS_OK(result)) {
869                 printf("result was %s\n", nt_errstr(result));
870         }
871 */
872
873         /* NOTE: popt allocates the whole argv, including the
874          * strings, as a single block.  So a single free is
875          * enough to release it -- we don't free the
876          * individual strings.  rtfm. */
877         free(argv);
878
879         return result;
880 }
881
882
883 /* Main function */
884
885  int main(int argc, char *argv[])
886 {
887         int                     opt;
888         static char             *cmdstr = NULL;
889         const char *server;
890         struct cli_state        *cli = NULL;
891         static char             *opt_ipaddr=NULL;
892         struct cmd_set          **cmd_set;
893         struct sockaddr_storage server_ss;
894         NTSTATUS                nt_status;
895         static int              opt_port = 0;
896         fstring new_workgroup;
897         int result = 0;
898         TALLOC_CTX *frame = talloc_stackframe();
899         uint32_t flags = 0;
900         struct dcerpc_binding *binding = NULL;
901         const char *binding_string = NULL;
902         char *user, *domain, *q;
903
904         /* make sure the vars that get altered (4th field) are in
905            a fixed location or certain compilers complain */
906         poptContext pc;
907         struct poptOption long_options[] = {
908                 POPT_AUTOHELP
909                 {"command",     'c', POPT_ARG_STRING,   &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
910                 {"dest-ip", 'I', POPT_ARG_STRING,   &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
911                 {"port", 'p', POPT_ARG_INT,   &opt_port, 'p', "Specify port number", "PORT"},
912                 POPT_COMMON_SAMBA
913                 POPT_COMMON_CONNECTION
914                 POPT_COMMON_CREDENTIALS
915                 POPT_TABLEEND
916         };
917
918         load_case_tables();
919
920         zero_sockaddr(&server_ss);
921
922         setlinebuf(stdout);
923
924         /* the following functions are part of the Samba debugging
925            facilities.  See lib/debug.c */
926         setup_logging("rpcclient", DEBUG_STDOUT);
927
928         rpcclient_auth_info = user_auth_info_init(frame);
929         if (rpcclient_auth_info == NULL) {
930                 exit(1);
931         }
932         popt_common_set_auth_info(rpcclient_auth_info);
933
934         /* Parse options */
935
936         pc = poptGetContext("rpcclient", argc, (const char **) argv,
937                             long_options, 0);
938
939         if (argc == 1) {
940                 poptPrintHelp(pc, stderr, 0);
941                 goto done;
942         }
943
944         while((opt = poptGetNextOpt(pc)) != -1) {
945                 switch (opt) {
946
947                 case 'I':
948                         if (!interpret_string_addr(&server_ss,
949                                                 opt_ipaddr,
950                                                 AI_NUMERICHOST)) {
951                                 fprintf(stderr, "%s not a valid IP address\n",
952                                         opt_ipaddr);
953                                 result = 1;
954                                 goto done;
955                         }
956                 }
957         }
958
959         /* Get server as remaining unparsed argument.  Print usage if more
960            than one unparsed argument is present. */
961
962         server = poptGetArg(pc);
963
964         if (!server || poptGetArg(pc)) {
965                 poptPrintHelp(pc, stderr, 0);
966                 result = 1;
967                 goto done;
968         }
969
970         poptFreeContext(pc);
971
972         load_interfaces();
973
974         if (!init_names()) {
975                 result = 1;
976                 goto done;
977         }
978
979         /* save the workgroup...
980
981            FIXME!! do we need to do this for other options as well
982            (or maybe a generic way to keep lp_load() from overwriting
983            everything)?  */
984
985         fstrcpy( new_workgroup, lp_workgroup() );
986
987         /* Load smb.conf file */
988
989         if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
990                 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
991
992         if ( strlen(new_workgroup) != 0 )
993                 set_global_myworkgroup( new_workgroup );
994
995         /*
996          * Get password
997          * from stdin if necessary
998          */
999
1000         if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
1001             !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
1002                 result = 1;
1003                 goto done;
1004         }
1005
1006         set_cmdline_auth_info_getpass(rpcclient_auth_info);
1007
1008         if ((server[0] == '/' && server[1] == '/') ||
1009                         (server[0] == '\\' && server[1] ==  '\\')) {
1010                 server += 2;
1011         }
1012
1013         nt_status = dcerpc_parse_binding(frame, server, &binding);
1014
1015         if (!NT_STATUS_IS_OK(nt_status)) {
1016
1017                 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1018                                                  strip_hostname(server));
1019                 if (!binding_string) {
1020                         result = 1;
1021                         goto done;
1022                 }
1023
1024                 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1025                 if (!NT_STATUS_IS_OK(nt_status)) {
1026                         result = -1;
1027                         goto done;
1028                 }
1029         }
1030
1031         if (binding->transport == NCA_UNKNOWN) {
1032                 binding->transport = NCACN_NP;
1033         }
1034
1035         if (binding->flags & DCERPC_SIGN) {
1036                 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1037                 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1038         }
1039         if (binding->flags & DCERPC_SEAL) {
1040                 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1041                 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1042         }
1043         if (binding->flags & DCERPC_AUTH_SPNEGO) {
1044                 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1045                 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1046         }
1047         if (binding->flags & DCERPC_AUTH_NTLM) {
1048                 /* If neither Integrity or Privacy are requested then
1049                  * Use just Connect level */
1050                 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1051                         pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1052                 }
1053
1054                 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1055                         pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1056                 } else {
1057                         pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1058                 }
1059         }
1060         if (binding->flags & DCERPC_AUTH_KRB5) {
1061                 /* If neither Integrity or Privacy are requested then
1062                  * Use just Connect level */
1063                 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1064                         pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1065                 }
1066
1067                 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1068                         pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1069                 } else {
1070                         pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1071                 }
1072         }
1073
1074         if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
1075                 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1076                          CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1077         }
1078         if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
1079                 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1080         }
1081
1082         user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
1083         SMB_ASSERT(user != NULL);
1084         domain = talloc_strdup(frame, lp_workgroup());
1085         SMB_ASSERT(domain != NULL);
1086         set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1087
1088         if ((q = strchr_m(user,'\\'))) {
1089                 *q = 0;
1090                 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1091                 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1092         }
1093
1094
1095         nt_status = cli_full_connection(&cli, global_myname(), binding->host,
1096                                         opt_ipaddr ? &server_ss : NULL, opt_port,
1097                                         "IPC$", "IPC",
1098                                         get_cmdline_auth_info_username(rpcclient_auth_info),
1099                                         get_cmdline_auth_info_domain(rpcclient_auth_info),
1100                                         get_cmdline_auth_info_password(rpcclient_auth_info),
1101                                         flags,
1102                                         get_cmdline_auth_info_signing_state(rpcclient_auth_info));
1103
1104         if (!NT_STATUS_IS_OK(nt_status)) {
1105                 DEBUG(0,("Cannot connect to server.  Error was %s\n", nt_errstr(nt_status)));
1106                 result = 1;
1107                 goto done;
1108         }
1109
1110         if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1111                 nt_status = cli_cm_force_encryption(cli,
1112                                         get_cmdline_auth_info_username(rpcclient_auth_info),
1113                                         get_cmdline_auth_info_password(rpcclient_auth_info),
1114                                         get_cmdline_auth_info_domain(rpcclient_auth_info),
1115                                         "IPC$");
1116                 if (!NT_STATUS_IS_OK(nt_status)) {
1117                         result = 1;
1118                         goto done;
1119                 }
1120         }
1121
1122 #if 0   /* COMMENT OUT FOR TESTING */
1123         memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1124 #endif
1125
1126         /* Load command lists */
1127
1128         timeout = cli_set_timeout(cli, 10000);
1129
1130         cmd_set = rpcclient_command_list;
1131
1132         while(*cmd_set) {
1133                 add_command_set(*cmd_set);
1134                 add_command_set(separator_command);
1135                 cmd_set++;
1136         }
1137
1138         default_transport = binding->transport;
1139
1140         fetch_machine_sid(cli);
1141
1142        /* Do anything specified with -c */
1143         if (cmdstr && cmdstr[0]) {
1144                 char    *cmd;
1145                 char    *p = cmdstr;
1146
1147                 result = 0;
1148
1149                 while((cmd=next_command(&p)) != NULL) {
1150                         NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1151                                                           binding, cmd);
1152                         SAFE_FREE(cmd);
1153                         result = NT_STATUS_IS_ERR(cmd_result);
1154                 }
1155
1156                 goto done;
1157         }
1158
1159         /* Loop around accepting commands */
1160
1161         while(1) {
1162                 char *line = NULL;
1163
1164                 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1165
1166                 if (line == NULL)
1167                         break;
1168
1169                 if (line[0] != '\n')
1170                         process_cmd(rpcclient_auth_info, cli, binding, line);
1171                 SAFE_FREE(line);
1172         }
1173
1174 done:
1175         if (cli != NULL) {
1176                 cli_shutdown(cli);
1177         }
1178         TALLOC_FREE(frame);
1179         return result;
1180 }