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