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