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