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