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