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