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