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