Alias charset 646 internally as it is same as ASCII. Should solve Solaris problems...
[Samba/gebeck_regimport.git] / source3 / rpcclient / rpcclient.c
blob515489292bc2b0c6a9ff704454830166e52d218e
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Tim Potter 2000-2001
6 Copyright (C) Martin Pool 2003
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 2 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "rpcclient.h"
26 DOM_SID domain_sid;
29 /* List to hold groups of commands.
31 * Commands are defined in a list of arrays: arrays are easy to
32 * statically declare, and lists are easier to dynamically extend.
35 static struct cmd_list {
36 struct cmd_list *prev, *next;
37 struct cmd_set *cmd_set;
38 } *cmd_list;
40 /****************************************************************************
41 handle completion of commands for readline
42 ****************************************************************************/
43 static char **completion_fn(const char *text, int start, int end)
45 #define MAX_COMPLETIONS 100
46 char **matches;
47 int i, count=0;
48 struct cmd_list *commands = cmd_list;
50 #if 0 /* JERRY */
51 /* FIXME!!! -- what to do when completing argument? */
52 /* for words not at the start of the line fallback
53 to filename completion */
54 if (start)
55 return NULL;
56 #endif
58 /* make sure we have a list of valid commands */
59 if (!commands)
60 return NULL;
62 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
63 if (!matches) return NULL;
65 matches[count++] = strdup(text);
66 if (!matches[0]) return NULL;
68 while (commands && count < MAX_COMPLETIONS-1)
70 if (!commands->cmd_set)
71 break;
73 for (i=0; commands->cmd_set[i].name; i++)
75 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
76 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
77 commands->cmd_set[i].ntfn ) ||
78 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
79 commands->cmd_set[i].wfn)))
81 matches[count] = strdup(commands->cmd_set[i].name);
82 if (!matches[count])
83 return NULL;
84 count++;
88 commands = commands->next;
92 if (count == 2) {
93 SAFE_FREE(matches[0]);
94 matches[0] = strdup(matches[1]);
96 matches[count] = NULL;
97 return matches;
100 static char* next_command (char** cmdstr)
102 static pstring command;
103 char *p;
105 if (!cmdstr || !(*cmdstr))
106 return NULL;
108 p = strchr_m(*cmdstr, ';');
109 if (p)
110 *p = '\0';
111 pstrcpy(command, *cmdstr);
112 if (p)
113 *cmdstr = p + 1;
114 else
115 *cmdstr = NULL;
117 return command;
120 /* Fetch the SID for this computer */
122 static void fetch_machine_sid(struct cli_state *cli)
124 POLICY_HND pol;
125 NTSTATUS result = NT_STATUS_OK;
126 uint32 info_class = 5;
127 fstring domain_name;
128 static BOOL got_domain_sid;
129 TALLOC_CTX *mem_ctx;
131 if (got_domain_sid) return;
133 if (!(mem_ctx=talloc_init("fetch_machine_sid")))
135 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
136 goto error;
140 if (!cli_nt_session_open (cli, PI_LSARPC)) {
141 fprintf(stderr, "could not initialise lsa pipe\n");
142 goto error;
145 result = cli_lsa_open_policy(cli, mem_ctx, True,
146 SEC_RIGHTS_MAXIMUM_ALLOWED,
147 &pol);
148 if (!NT_STATUS_IS_OK(result)) {
149 goto error;
152 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
153 domain_name, &domain_sid);
154 if (!NT_STATUS_IS_OK(result)) {
155 goto error;
158 got_domain_sid = True;
160 cli_lsa_close(cli, mem_ctx, &pol);
161 cli_nt_session_close(cli);
162 talloc_destroy(mem_ctx);
164 return;
166 error:
167 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
169 if (!NT_STATUS_IS_OK(result)) {
170 fprintf(stderr, "error: %s\n", nt_errstr(result));
173 exit(1);
176 /* List the available commands on a given pipe */
178 static NTSTATUS cmd_listcommands(struct cli_state *cli, TALLOC_CTX *mem_ctx,
179 int argc, const char **argv)
181 struct cmd_list *tmp;
182 struct cmd_set *tmp_set;
183 int i;
185 /* Usage */
187 if (argc != 2) {
188 printf("Usage: %s <pipe>\n", argv[0]);
189 return NT_STATUS_OK;
192 /* Help on one command */
194 for (tmp = cmd_list; tmp; tmp = tmp->next)
196 tmp_set = tmp->cmd_set;
198 if (!StrCaseCmp(argv[1], tmp_set->name))
200 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
202 i = 0;
203 tmp_set++;
204 while(tmp_set->name) {
205 printf("%20s", tmp_set->name);
206 tmp_set++;
207 i++;
208 if (i%4 == 0)
209 printf("\n");
212 /* drop out of the loop */
213 break;
216 printf("\n\n");
218 return NT_STATUS_OK;
221 /* Display help on commands */
223 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
224 int argc, const char **argv)
226 struct cmd_list *tmp;
227 struct cmd_set *tmp_set;
229 /* Usage */
231 if (argc > 2) {
232 printf("Usage: %s [command]\n", argv[0]);
233 return NT_STATUS_OK;
236 /* Help on one command */
238 if (argc == 2) {
239 for (tmp = cmd_list; tmp; tmp = tmp->next) {
241 tmp_set = tmp->cmd_set;
243 while(tmp_set->name) {
244 if (strequal(argv[1], tmp_set->name)) {
245 if (tmp_set->usage &&
246 tmp_set->usage[0])
247 printf("%s\n", tmp_set->usage);
248 else
249 printf("No help for %s\n", tmp_set->name);
251 return NT_STATUS_OK;
254 tmp_set++;
258 printf("No such command: %s\n", argv[1]);
259 return NT_STATUS_OK;
262 /* List all commands */
264 for (tmp = cmd_list; tmp; tmp = tmp->next) {
266 tmp_set = tmp->cmd_set;
268 while(tmp_set->name) {
270 printf("%15s\t\t%s\n", tmp_set->name,
271 tmp_set->description ? tmp_set->description:
272 "");
274 tmp_set++;
278 return NT_STATUS_OK;
281 /* Change the debug level */
283 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
284 int argc, const char **argv)
286 if (argc > 2) {
287 printf("Usage: %s [debuglevel]\n", argv[0]);
288 return NT_STATUS_OK;
291 if (argc == 2) {
292 DEBUGLEVEL = atoi(argv[1]);
295 printf("debuglevel is %d\n", DEBUGLEVEL);
297 return NT_STATUS_OK;
300 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
301 int argc, const char **argv)
303 exit(0);
304 return NT_STATUS_OK; /* NOTREACHED */
307 static NTSTATUS cmd_sign(struct cli_state *cli, TALLOC_CTX *mem_ctx,
308 int argc, const char **argv)
310 if (cli->pipe_auth_flags == (AUTH_PIPE_NTLMSSP|AUTH_PIPE_SIGN)) {
311 return NT_STATUS_OK;
312 } else {
313 /* still have session, just need to use it again */
314 cli->pipe_auth_flags = AUTH_PIPE_NTLMSSP;
315 cli->pipe_auth_flags |= AUTH_PIPE_SIGN;
316 if (cli->nt_pipe_fnum != 0)
317 cli_nt_session_close(cli);
320 return NT_STATUS_OK;
323 static NTSTATUS cmd_seal(struct cli_state *cli, TALLOC_CTX *mem_ctx,
324 int argc, const char **argv)
326 if (cli->pipe_auth_flags == (AUTH_PIPE_NTLMSSP|AUTH_PIPE_SIGN|AUTH_PIPE_SEAL)) {
327 return NT_STATUS_OK;
328 } else {
329 /* still have session, just need to use it again */
330 cli->pipe_auth_flags = AUTH_PIPE_NTLMSSP;
331 cli->pipe_auth_flags |= AUTH_PIPE_SIGN;
332 cli->pipe_auth_flags |= AUTH_PIPE_SEAL;
333 if (cli->nt_pipe_fnum != 0)
334 cli_nt_session_close(cli);
336 return NT_STATUS_OK;
339 static NTSTATUS cmd_none(struct cli_state *cli, TALLOC_CTX *mem_ctx,
340 int argc, const char **argv)
342 if (cli->pipe_auth_flags == 0) {
343 return NT_STATUS_OK;
344 } else {
345 /* still have session, just need to use it again */
346 cli->pipe_auth_flags = 0;
347 if (cli->nt_pipe_fnum != 0)
348 cli_nt_session_close(cli);
350 cli->pipe_auth_flags = 0;
352 return NT_STATUS_OK;
355 static NTSTATUS cmd_schannel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
356 int argc, const char **argv)
358 NTSTATUS ret;
359 uchar trust_password[16];
360 uint32 sec_channel_type;
361 static uchar zeros[16];
363 if (argc == 2) {
364 strhex_to_str((char *)cli->auth_info.sess_key,
365 strlen(argv[1]),
366 argv[1]);
367 memcpy(cli->sess_key, cli->auth_info.sess_key, sizeof(cli->sess_key));
369 cli->pipe_auth_flags = AUTH_PIPE_NETSEC;
370 cli->pipe_auth_flags |= AUTH_PIPE_SIGN;
371 cli->pipe_auth_flags |= AUTH_PIPE_SEAL;
373 return NT_STATUS_OK;
376 /* Cleanup */
378 if ((memcmp(cli->auth_info.sess_key, zeros, sizeof(cli->auth_info.sess_key)) != 0)) {
379 if (cli->pipe_auth_flags == (AUTH_PIPE_NETSEC|AUTH_PIPE_SIGN|AUTH_PIPE_SEAL)) {
380 /* already in this mode nothing to do */
381 return NT_STATUS_OK;
382 } else {
383 /* schannel is setup, just need to use it again */
384 cli->pipe_auth_flags = AUTH_PIPE_NETSEC;
385 cli->pipe_auth_flags |= AUTH_PIPE_SIGN;
386 cli->pipe_auth_flags |= AUTH_PIPE_SEAL;
387 if (cli->nt_pipe_fnum != 0)
388 cli_nt_session_close(cli);
389 return NT_STATUS_OK;
393 if (cli->nt_pipe_fnum != 0)
394 cli_nt_session_close(cli);
396 cli->pipe_auth_flags = AUTH_PIPE_NETSEC;
397 cli->pipe_auth_flags |= AUTH_PIPE_SIGN;
398 cli->pipe_auth_flags |= AUTH_PIPE_SEAL;
400 if (!secrets_fetch_trust_account_password(lp_workgroup(),
401 trust_password,
402 NULL, &sec_channel_type)) {
403 return NT_STATUS_UNSUCCESSFUL;
406 ret = cli_nt_setup_netsec(cli, sec_channel_type, trust_password);
407 if (NT_STATUS_IS_OK(ret)) {
408 char *hex_session_key;
409 hex_encode(cli->auth_info.sess_key,
410 sizeof(cli->auth_info.sess_key),
411 &hex_session_key);
412 printf("Got Session key: %s\n", hex_session_key);
413 SAFE_FREE(hex_session_key);
415 return ret;
418 /* Built in rpcclient commands */
420 static struct cmd_set rpcclient_commands[] = {
422 { "GENERAL OPTIONS" },
424 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
425 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
426 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, "Set debug level", "level" },
427 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, -1, "List available commands on <pipe>", "pipe" },
428 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
429 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
430 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, -1, "Force RPC pipe connections to be signed", "" },
431 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, -1, "Force RPC pipe connections to be sealed", "" },
432 { "schannel", RPC_RTYPE_NTSTATUS, cmd_schannel, NULL, -1, "Force RPC pipe connections to be sealed with 'schannel' (NETSEC). Assumes valid machine account to this domain controller.", "" },
433 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, -1, "Force RPC pipe connections to have no special properties", "" },
435 { NULL }
438 static struct cmd_set separator_command[] = {
439 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, -1, "----------------------" },
440 { NULL }
444 /* Various pipe commands */
446 extern struct cmd_set lsarpc_commands[];
447 extern struct cmd_set samr_commands[];
448 extern struct cmd_set spoolss_commands[];
449 extern struct cmd_set netlogon_commands[];
450 extern struct cmd_set srvsvc_commands[];
451 extern struct cmd_set dfs_commands[];
452 extern struct cmd_set reg_commands[];
453 extern struct cmd_set ds_commands[];
454 extern struct cmd_set echo_commands[];
456 static struct cmd_set *rpcclient_command_list[] = {
457 rpcclient_commands,
458 lsarpc_commands,
459 ds_commands,
460 samr_commands,
461 spoolss_commands,
462 netlogon_commands,
463 srvsvc_commands,
464 dfs_commands,
465 reg_commands,
466 echo_commands,
467 NULL
470 static void add_command_set(struct cmd_set *cmd_set)
472 struct cmd_list *entry;
474 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
475 DEBUG(0, ("out of memory\n"));
476 return;
479 ZERO_STRUCTP(entry);
481 entry->cmd_set = cmd_set;
482 DLIST_ADD(cmd_list, entry);
487 * Call an rpcclient function, passing an argv array.
489 * @param cmd Command to run, as a single string.
491 static NTSTATUS do_cmd(struct cli_state *cli,
492 struct cmd_set *cmd_entry,
493 int argc, char **argv)
495 NTSTATUS ntresult;
496 WERROR wresult;
497 uchar trust_password[16];
499 TALLOC_CTX *mem_ctx;
501 /* Create mem_ctx */
503 if (!(mem_ctx = talloc_init("do_cmd"))) {
504 DEBUG(0, ("talloc_init() failed\n"));
505 return NT_STATUS_NO_MEMORY;
508 /* Open pipe */
510 if (cmd_entry->pipe_idx != -1
511 && cmd_entry->pipe_idx != cli->pipe_idx) {
512 if (cli->nt_pipe_fnum != 0)
513 cli_nt_session_close(cli);
515 if (!cli_nt_session_open(cli, cmd_entry->pipe_idx)) {
516 DEBUG(0, ("Could not initialise %s\n",
517 get_pipe_name_from_index(cmd_entry->pipe_idx)));
518 return NT_STATUS_UNSUCCESSFUL;
522 /* some of the DsXXX commands use the netlogon pipe */
524 if (lp_client_schannel() && (cmd_entry->pipe_idx == PI_NETLOGON) && !(cli->pipe_auth_flags & AUTH_PIPE_NETSEC)) {
525 /* The 7 here seems to be required to get Win2k not to downgrade us
526 to NT4. Actually, anything other than 1ff would seem to do... */
527 uint32 neg_flags = 0x000001ff;
528 uint32 sec_channel_type;
530 if (!secrets_fetch_trust_account_password(lp_workgroup(),
531 trust_password,
532 NULL, &sec_channel_type)) {
533 return NT_STATUS_UNSUCCESSFUL;
536 ntresult = cli_nt_setup_creds(cli, sec_channel_type,
537 trust_password,
538 &neg_flags, 2);
539 if (!NT_STATUS_IS_OK(ntresult)) {
540 ZERO_STRUCT(cli->auth_info.sess_key);
541 printf("nt_setup_creds failed with %s\n", nt_errstr(ntresult));
542 return ntresult;
547 /* Run command */
549 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
550 ntresult = cmd_entry->ntfn(cli, mem_ctx, argc, (const char **) argv);
551 if (!NT_STATUS_IS_OK(ntresult)) {
552 printf("result was %s\n", nt_errstr(ntresult));
554 } else {
555 wresult = cmd_entry->wfn( cli, mem_ctx, argc, (const char **) argv);
556 /* print out the DOS error */
557 if (!W_ERROR_IS_OK(wresult)) {
558 printf( "result was %s\n", dos_errstr(wresult));
560 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
564 /* Cleanup */
566 talloc_destroy(mem_ctx);
568 return ntresult;
573 * Process a command entered at the prompt or as part of -c
575 * @returns The NTSTATUS from running the command.
577 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
579 struct cmd_list *temp_list;
580 NTSTATUS result = NT_STATUS_OK;
581 int ret;
582 int argc;
583 char **argv = NULL;
585 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
586 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
587 return NT_STATUS_UNSUCCESSFUL;
591 /* Walk through a dlist of arrays of commands. */
592 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
593 struct cmd_set *temp_set = temp_list->cmd_set;
595 while (temp_set->name) {
596 if (strequal(argv[0], temp_set->name)) {
597 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
598 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
599 fprintf (stderr, "Invalid command\n");
600 goto out_free;
603 result = do_cmd(cli, temp_set, argc, argv);
605 goto out_free;
607 temp_set++;
611 if (argv[0]) {
612 printf("command not found: %s\n", argv[0]);
615 out_free:
616 /* moved to do_cmd()
617 if (!NT_STATUS_IS_OK(result)) {
618 printf("result was %s\n", nt_errstr(result));
622 if (argv) {
623 /* NOTE: popt allocates the whole argv, including the
624 * strings, as a single block. So a single free is
625 * enough to release it -- we don't free the
626 * individual strings. rtfm. */
627 free(argv);
630 return result;
634 /* Main function */
636 int main(int argc, char *argv[])
638 BOOL interactive = True;
639 int opt;
640 static char *cmdstr = NULL;
641 const char *server;
642 struct cli_state *cli;
643 static char *opt_ipaddr=NULL;
644 struct cmd_set **cmd_set;
645 struct in_addr server_ip;
646 NTSTATUS nt_status;
648 /* make sure the vars that get altered (4th field) are in
649 a fixed location or certain compilers complain */
650 poptContext pc;
651 struct poptOption long_options[] = {
652 POPT_AUTOHELP
653 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
654 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
655 POPT_COMMON_SAMBA
656 POPT_COMMON_CONNECTION
657 POPT_COMMON_CREDENTIALS
658 POPT_TABLEEND
661 ZERO_STRUCT(server_ip);
663 setlinebuf(stdout);
665 /* the following functions are part of the Samba debugging
666 facilities. See lib/debug.c */
667 setup_logging("rpcclient", interactive);
668 if (!interactive)
669 reopen_logs();
671 /* Load smb.conf file */
673 if (!lp_load(dyn_CONFIGFILE,True,False,False))
674 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
676 /* Parse options */
678 pc = poptGetContext("rpcclient", argc, (const char **) argv,
679 long_options, 0);
681 if (argc == 1) {
682 poptPrintHelp(pc, stderr, 0);
683 return 0;
686 while((opt = poptGetNextOpt(pc)) != -1) {
687 switch (opt) {
689 case 'I':
690 if ( (server_ip.s_addr=inet_addr(opt_ipaddr)) == INADDR_NONE ) {
691 fprintf(stderr, "%s not a valid IP address\n",
692 opt_ipaddr);
693 return 1;
698 /* Get server as remaining unparsed argument. Print usage if more
699 than one unparsed argument is present. */
701 server = poptGetArg(pc);
703 if (!server || poptGetArg(pc)) {
704 poptPrintHelp(pc, stderr, 0);
705 return 1;
708 poptFreeContext(pc);
710 load_interfaces();
712 if (!init_names())
713 return 1;
716 * Get password
717 * from stdin if necessary
720 if (!cmdline_auth_info.got_pass) {
721 char *pass = getpass("Password:");
722 if (pass) {
723 pstrcpy(cmdline_auth_info.password, pass);
727 nt_status = cli_full_connection(&cli, global_myname(), server,
728 opt_ipaddr ? &server_ip : NULL, 0,
729 "IPC$", "IPC",
730 cmdline_auth_info.username,
731 lp_workgroup(),
732 cmdline_auth_info.password,
733 cmdline_auth_info.use_kerberos ? CLI_FULL_CONNECTION_USE_KERBEROS : 0,
734 cmdline_auth_info.signing_state,NULL);
736 if (!NT_STATUS_IS_OK(nt_status)) {
737 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
738 return 1;
741 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
743 /* Load command lists */
745 cmd_set = rpcclient_command_list;
747 while(*cmd_set) {
748 add_command_set(*cmd_set);
749 add_command_set(separator_command);
750 cmd_set++;
753 fetch_machine_sid(cli);
755 /* Do anything specified with -c */
756 if (cmdstr && cmdstr[0]) {
757 char *cmd;
758 char *p = cmdstr;
759 int result = 0;
761 while((cmd=next_command(&p)) != NULL) {
762 NTSTATUS cmd_result = process_cmd(cli, cmd);
763 result = NT_STATUS_IS_ERR(cmd_result);
766 cli_shutdown(cli);
767 return result;
770 /* Loop around accepting commands */
772 while(1) {
773 pstring prompt;
774 char *line;
776 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
778 line = smb_readline(prompt, NULL, completion_fn);
780 if (line == NULL)
781 break;
783 if (line[0] != '\n')
784 process_cmd(cli, line);
787 cli_shutdown(cli);
788 return 0;