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