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