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