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