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