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