Remove strip-links.pl, not needed anymore
[Samba/gebeck_regimport.git] / source / rpcclient / rpcclient.c
blobb01e2d694c55b8573bc89d68018ef61f3d17f2b9
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(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 /* Built in rpcclient commands */
309 static struct cmd_set rpcclient_commands[] = {
311 { "GENERAL OPTIONS" },
313 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
314 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
315 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, "Set debug level", "level" },
316 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, -1, "List available commands on <pipe>", "pipe" },
317 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
318 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
320 { NULL }
323 static struct cmd_set separator_command[] = {
324 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, -1, "----------------------" },
325 { NULL }
329 /* Various pipe commands */
331 extern struct cmd_set lsarpc_commands[];
332 extern struct cmd_set samr_commands[];
333 extern struct cmd_set spoolss_commands[];
334 extern struct cmd_set netlogon_commands[];
335 extern struct cmd_set srvsvc_commands[];
336 extern struct cmd_set dfs_commands[];
337 extern struct cmd_set reg_commands[];
338 extern struct cmd_set ds_commands[];
339 extern struct cmd_set echo_commands[];
341 static struct cmd_set *rpcclient_command_list[] = {
342 rpcclient_commands,
343 lsarpc_commands,
344 ds_commands,
345 samr_commands,
346 spoolss_commands,
347 netlogon_commands,
348 srvsvc_commands,
349 dfs_commands,
350 reg_commands,
351 echo_commands,
352 NULL
355 static void add_command_set(struct cmd_set *cmd_set)
357 struct cmd_list *entry;
359 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
360 DEBUG(0, ("out of memory\n"));
361 return;
364 ZERO_STRUCTP(entry);
366 entry->cmd_set = cmd_set;
367 DLIST_ADD(cmd_list, entry);
372 * Call an rpcclient function, passing an argv array.
374 * @param cmd Command to run, as a single string.
376 static NTSTATUS do_cmd(struct cli_state *cli,
377 struct cmd_set *cmd_entry,
378 int argc, char **argv)
380 NTSTATUS ntresult;
381 WERROR wresult;
383 TALLOC_CTX *mem_ctx;
385 /* Create mem_ctx */
387 if (!(mem_ctx = talloc_init("do_cmd"))) {
388 DEBUG(0, ("talloc_init() failed\n"));
389 return NT_STATUS_UNSUCCESSFUL;
392 /* Open pipe */
394 if (cmd_entry->pipe_idx == PI_NETLOGON) {
395 uchar trust_password[16];
396 uint32 sec_channel_type;
398 if (!secrets_fetch_trust_account_password(lp_workgroup(),
399 trust_password,
400 NULL, &sec_channel_type)) {
401 return NT_STATUS_UNSUCCESSFUL;
404 if (!cli_nt_open_netlogon(cli, trust_password,
405 sec_channel_type)) {
406 DEBUG(0, ("Could not initialise NETLOGON pipe\n"));
407 return NT_STATUS_UNSUCCESSFUL;
409 } else {
410 if (cmd_entry->pipe_idx != -1) {
411 if (!cli_nt_session_open(cli, cmd_entry->pipe_idx)) {
412 DEBUG(0, ("Could not initialise %s\n",
413 get_pipe_name_from_index(cmd_entry->pipe_idx)));
414 return NT_STATUS_UNSUCCESSFUL;
419 /* Run command */
421 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
422 ntresult = cmd_entry->ntfn(cli, mem_ctx, argc, (const char **) argv);
423 if (!NT_STATUS_IS_OK(ntresult)) {
424 printf("result was %s\n", nt_errstr(ntresult));
426 } else {
427 wresult = cmd_entry->wfn( cli, mem_ctx, argc, (const char **) argv);
428 /* print out the DOS error */
429 if (!W_ERROR_IS_OK(wresult)) {
430 printf( "result was %s\n", dos_errstr(wresult));
432 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
436 /* Cleanup */
438 if (cmd_entry->pipe_idx != -1)
439 cli_nt_session_close(cli);
441 talloc_destroy(mem_ctx);
443 return ntresult;
448 * Process a command entered at the prompt or as part of -c
450 * @returns The NTSTATUS from running the command.
452 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
454 struct cmd_list *temp_list;
455 NTSTATUS result = NT_STATUS_OK;
456 int ret;
457 int argc;
458 char **argv = NULL;
460 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
461 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
462 return NT_STATUS_UNSUCCESSFUL;
466 /* Walk through a dlist of arrays of commands. */
467 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
468 struct cmd_set *temp_set = temp_list->cmd_set;
470 while (temp_set->name) {
471 if (strequal(argv[0], temp_set->name)) {
472 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
473 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
474 fprintf (stderr, "Invalid command\n");
475 goto out_free;
478 result = do_cmd(cli, temp_set, argc, argv);
480 goto out_free;
482 temp_set++;
486 if (argv[0]) {
487 printf("command not found: %s\n", argv[0]);
490 out_free:
491 /* moved to do_cmd()
492 if (!NT_STATUS_IS_OK(result)) {
493 printf("result was %s\n", nt_errstr(result));
497 if (argv) {
498 /* NOTE: popt allocates the whole argv, including the
499 * strings, as a single block. So a single free is
500 * enough to release it -- we don't free the
501 * individual strings. rtfm. */
502 free(argv);
505 return result;
509 /* Main function */
511 int main(int argc, char *argv[])
513 BOOL interactive = True;
514 int opt;
515 static char *cmdstr = NULL;
516 const char *server;
517 struct cli_state *cli;
518 static char *opt_ipaddr=NULL;
519 struct cmd_set **cmd_set;
520 struct in_addr server_ip;
521 NTSTATUS nt_status;
523 /* make sure the vars that get altered (4th field) are in
524 a fixed location or certain compilers complain */
525 poptContext pc;
526 struct poptOption long_options[] = {
527 POPT_AUTOHELP
528 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
529 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
530 POPT_COMMON_SAMBA
531 POPT_COMMON_CONNECTION
532 POPT_COMMON_CREDENTIALS
533 POPT_TABLEEND
536 ZERO_STRUCT(server_ip);
538 setlinebuf(stdout);
540 /* the following functions are part of the Samba debugging
541 facilities. See lib/debug.c */
542 setup_logging("rpcclient", interactive);
543 if (!interactive)
544 reopen_logs();
546 /* Load smb.conf file */
548 if (!lp_load(dyn_CONFIGFILE,True,False,False))
549 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
551 /* Parse options */
553 pc = poptGetContext("rpcclient", argc, (const char **) argv,
554 long_options, 0);
556 if (argc == 1) {
557 poptPrintHelp(pc, stderr, 0);
558 return 0;
561 while((opt = poptGetNextOpt(pc)) != -1) {
562 switch (opt) {
564 case 'I':
565 if ( (server_ip.s_addr=inet_addr(opt_ipaddr)) == INADDR_NONE ) {
566 fprintf(stderr, "%s not a valid IP address\n",
567 opt_ipaddr);
568 return 1;
573 /* Get server as remaining unparsed argument. Print usage if more
574 than one unparsed argument is present. */
576 server = poptGetArg(pc);
578 if (!server || poptGetArg(pc)) {
579 poptPrintHelp(pc, stderr, 0);
580 return 1;
583 poptFreeContext(pc);
585 load_interfaces();
587 if (!init_names())
588 return 1;
591 * Get password
592 * from stdin if necessary
595 if (!cmdline_auth_info.got_pass) {
596 char *pass = getpass("Password:");
597 if (pass) {
598 pstrcpy(cmdline_auth_info.password, pass);
602 nt_status = cli_full_connection(&cli, global_myname(), server,
603 opt_ipaddr ? &server_ip : NULL, 0,
604 "IPC$", "IPC",
605 cmdline_auth_info.username, lp_workgroup(),
606 cmdline_auth_info.password, 0, NULL);
608 if (!NT_STATUS_IS_OK(nt_status)) {
609 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
610 return 1;
613 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
615 /* Load command lists */
617 cmd_set = rpcclient_command_list;
619 while(*cmd_set) {
620 add_command_set(*cmd_set);
621 add_command_set(separator_command);
622 cmd_set++;
625 fetch_machine_sid(cli);
627 /* Do anything specified with -c */
628 if (cmdstr && cmdstr[0]) {
629 char *cmd;
630 char *p = cmdstr;
631 int result = 0;
633 while((cmd=next_command(&p)) != NULL) {
634 NTSTATUS cmd_result = process_cmd(cli, cmd);
635 result = NT_STATUS_IS_ERR(cmd_result);
638 cli_shutdown(cli);
639 return result;
642 /* Loop around accepting commands */
644 while(1) {
645 pstring prompt;
646 char *line;
648 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
650 line = smb_readline(prompt, NULL, completion_fn);
652 if (line == NULL)
653 break;
655 if (line[0] != '\n')
656 process_cmd(cli, line);
659 cli_shutdown(cli);
660 return 0;