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