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