3 * Bacula Console interface to the Director
5 * Kern Sibbald, September MM
11 Copyright (C) 2000-2004 Kern Sibbald and John Walker
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 2.1 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include "console_conf.h"
39 #define con_set_zed_keys();
47 #define isatty(fd) (fd==0)
48 DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
51 /* Exported variables */
56 extern int rl_catch_signals;
59 /* Imported functions */
60 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
64 /* Forward referenced functions */
65 static void terminate_console(int sig);
66 int get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec);
67 static int do_outputcmd(FILE *input, BSOCK *UA_sock);
68 void senditf(char *fmt, ...);
69 void sendit(char *buf);
71 /* Static variables */
72 static char *configfile = NULL;
73 static BSOCK *UA_sock = NULL;
75 static FILE *output = stdout;
76 static bool tee = false; /* output to output and stdout */
77 static bool stop = false;
80 static char *argk[MAX_CMD_ARGS];
81 static char *argv[MAX_CMD_ARGS];
84 /* Command prototypes */
85 static int versioncmd(FILE *input, BSOCK *UA_sock);
86 static int inputcmd(FILE *input, BSOCK *UA_sock);
87 static int outputcmd(FILE *input, BSOCK *UA_sock);
88 static int teecmd(FILE *input, BSOCK *UA_sock);
89 static int quitcmd(FILE *input, BSOCK *UA_sock);
90 static int timecmd(FILE *input, BSOCK *UA_sock);
91 static int sleepcmd(FILE *input, BSOCK *UA_sock);
94 #define CONFIG_FILE "./bconsole.conf" /* default configuration file */
99 "\nVersion: " VERSION " (" BDATE ") %s %s %s\n\n"
100 "Usage: bconsole [-s] [-c config_file] [-d debug_level] [config_file]\n"
101 " -c <file> set configuration file to file\n"
102 " -dnn set debug level to nn\n"
104 " -t test - read configuration and exit\n"
105 " -? print this message.\n"
106 "\n"), HOST_OS, DISTNAME, DISTVER);
109 void got_sigstop(int sig)
114 void got_sigcontinue(int sig)
119 void got_sigtout(int sig)
121 // printf("Got tout\n");
124 void got_sigtin(int sig)
126 // printf("Got tin\n");
129 static int zed_keyscmd(FILE *input, BSOCK *UA_sock)
136 * These are the @command
138 struct cmdstruct { char *key; int (*func)(FILE *input, BSOCK *UA_sock); char *help; };
139 static struct cmdstruct commands[] = {
140 { N_("input"), inputcmd, _("input from file")},
141 { N_("output"), outputcmd, _("output to file")},
142 { N_("quit"), quitcmd, _("quit")},
143 { N_("tee"), teecmd, _("output to file and terminal")},
144 { N_("sleep"), sleepcmd, _("sleep specified time")},
145 { N_("time"), timecmd, _("print current time")},
146 { N_("version"), versioncmd, _("print Console's version")},
147 { N_("exit"), quitcmd, _("exit = quit")},
148 { N_("zed_keyst"), zed_keyscmd, _("zed_keys = use zed keys instead of bash keys")},
150 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
152 static int do_a_command(FILE *input, BSOCK *UA_sock)
163 Dmsg1(120, "Command: %s\n", UA_sock->msg);
169 if (*cmd == '#') { /* comment */
173 for (i=0; i<comsize; i++) { /* search for command */
174 if (strncasecmp(cmd, _(commands[i].key), len) == 0) {
175 stat = (*commands[i].func)(input, UA_sock); /* go execute command */
181 pm_strcat(&UA_sock->msg, _(": is an illegal command\n"));
182 UA_sock->msglen = strlen(UA_sock->msg);
183 sendit(UA_sock->msg);
189 static void read_and_process_input(FILE *input, BSOCK *UA_sock)
192 bool at_prompt = false;
193 int tty_input = isatty(fileno(input));
197 if (at_prompt) { /* don't prompt multiple times */
204 stat = get_cmd(input, prompt, UA_sock, 30);
212 /* Reading input from a file */
213 int len = sizeof_pool_memory(UA_sock->msg) - 1;
217 if (fgets(UA_sock->msg, len, input) == NULL) {
220 sendit(UA_sock->msg); /* echo to terminal */
221 strip_trailing_junk(UA_sock->msg);
222 UA_sock->msglen = strlen(UA_sock->msg);
227 break; /* error or interrupt */
228 } else if (stat == 0) { /* timeout */
229 if (strcmp(prompt, "*") == 0) {
230 bnet_fsend(UA_sock, ".messages");
236 /* @ => internal command for us */
237 if (UA_sock->msg[0] == '@') {
238 parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
239 if (!do_a_command(input, UA_sock)) {
244 if (!bnet_send(UA_sock)) { /* send command */
248 if (strcmp(UA_sock->msg, ".quit") == 0 || strcmp(UA_sock->msg, ".exit") == 0) {
251 while ((stat = bnet_recv(UA_sock)) >= 0) {
258 /* Suppress output if running in background or user hit ctl-c */
259 if (!stop && !usrbrk()) {
260 sendit(UA_sock->msg);
271 if (is_bnet_stop(UA_sock)) {
272 break; /* error or term */
273 } else if (stat == BNET_SIGNAL) {
274 if (UA_sock->msglen == BNET_PROMPT) {
277 Dmsg1(100, "Got poll %s\n", bnet_sig_to_ascii(UA_sock));
283 /*********************************************************************
285 * Main Bacula Console -- User Interface Program
288 int main(int argc, char *argv[])
290 int ch, i, ndir, item;
291 bool no_signals = false;
292 bool test_config = false;
296 my_name_is(argc, argv, "bconsole");
297 textdomain("bacula");
298 init_msg(NULL, NULL);
299 working_directory = "/tmp";
300 args = get_pool_memory(PM_FNAME);
303 while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
305 case 'c': /* configuration file */
306 if (configfile != NULL) {
309 configfile = bstrdup(optarg);
313 debug_level = atoi(optarg);
314 if (debug_level <= 0) {
319 case 's': /* turn off signals */
338 init_signals(terminate_console);
341 #if !defined(HAVE_WIN32)
342 /* Override Bacula default signals */
343 signal(SIGCHLD, SIG_IGN);
344 signal(SIGQUIT, SIG_IGN);
345 signal(SIGTSTP, got_sigstop);
346 signal(SIGCONT, got_sigcontinue);
347 signal(SIGTTIN, got_sigtin);
348 signal(SIGTTOU, got_sigtout);
358 if (configfile == NULL) {
359 configfile = bstrdup(CONFIG_FILE);
362 parse_config(configfile);
366 foreach_res(dir, R_DIRECTOR) {
372 Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s\n\
373 Without that I don't how to speak to the Director :-(\n"), configfile);
377 terminate_console(0);
381 memset(&jcr, 0, sizeof(jcr));
384 WSA_Init(); /* Initialize Windows sockets */
387 struct sockaddr_in client_addr;
388 memset(&client_addr, 0, sizeof(client_addr));
389 UA_sock = init_bsock(NULL, 0, "", "", 0, &client_addr);
391 sendit(_("Available Directors:\n"));
394 foreach_res(dir, R_DIRECTOR) {
395 senditf( _("%d %s at %s:%d\n"), 1+ndir++, dir->hdr.name, dir->address,
399 if (get_cmd(stdin, _("Select Director: "), UA_sock, 600) < 0) {
400 WSACleanup(); /* Cleanup Windows sockets */
403 item = atoi(UA_sock->msg);
404 if (item < 0 || item > ndir) {
405 senditf(_("You must enter a number between 1 and %d\n"), ndir);
410 for (i=0; i<item; i++) {
411 dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir);
417 dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
421 senditf(_("Connecting to Director %s:%d\n"), dir->address,dir->DIRport);
422 UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address,
423 NULL, dir->DIRport, 0);
424 if (UA_sock == NULL) {
425 terminate_console(0);
428 jcr.dir_bsock = UA_sock;
431 CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
433 /* If cons==NULL, default console will be used */
434 if (!authenticate_director(&jcr, dir, cons)) {
435 fprintf(stderr, "ERR=%s", UA_sock->msg);
436 terminate_console(0);
440 Dmsg0(40, "Opened connection with Director daemon\n");
442 sendit(_("Enter a period to cancel a command.\n"));
444 /* Run commands in ~/.bconsolerc if any */
445 char *env = getenv("HOME");
448 pm_strcpy(&UA_sock->msg, env);
449 pm_strcat(&UA_sock->msg, "/.bconsolerc");
450 fd = fopen(UA_sock->msg, "r");
452 read_and_process_input(fd, UA_sock);
457 read_and_process_input(stdin, UA_sock);
460 bnet_sig(UA_sock, BNET_TERMINATE); /* send EOF */
464 terminate_console(0);
469 /* Cleanup and then exit */
470 static void terminate_console(int sig)
472 WSACleanup(); /* Cleanup Windows sockets */
474 static bool already_here = false;
476 if (already_here) { /* avoid recursive temination problems */
480 free_pool_memory(args);
489 #define READLINE_LIBRARY 1
491 #include "readline.h"
496 get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
500 rl_catch_signals = 0; /* do it ourselves */
501 line = readline(prompt);
506 strip_trailing_junk(line);
507 sock->msglen = pm_strcpy(&sock->msg, line);
509 add_history(sock->msg);
515 #else /* no readline, do it ourselves */
518 * Returns: 1 if data available
523 wait_for_data(int fd, int sec)
528 return 1; /* select doesn't seem to work on Win32 */
536 switch(select(fd + 1, &fdset, NULL, NULL, &tv)) {
537 case 0: /* timeout */
540 if (errno == EINTR || errno == EAGAIN) {
543 return -1; /* error return */
551 * Get next input command from terminal.
553 * Returns: 1 if got input
558 get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
562 if (output == stdout || tee) {
567 switch (wait_for_data(fileno(input), sec)) {
569 return 0; /* timeout */
571 return -1; /* error */
573 len = sizeof_pool_memory(sock->msg) - 1;
579 if (isatty(fileno(input))) {
580 input_line(sock->msg, len);
584 if (fgets(sock->msg, len, input) == NULL) {
592 strip_trailing_junk(sock->msg);
593 sock->msglen = strlen(sock->msg);
599 static int versioncmd(FILE *input, BSOCK *UA_sock)
601 senditf("Version: " VERSION " (" BDATE ") %s %s %s\n",
602 HOST_OS, DISTNAME, DISTVER);
606 static int inputcmd(FILE *input, BSOCK *UA_sock)
611 sendit(_("Too many arguments on input command.\n"));
615 sendit(_("First argument to input command must be a filename.\n"));
618 fd = fopen(argk[1], "r");
620 senditf(_("Cannot open file %s for input. ERR=%s\n"),
621 argk[1], strerror(errno));
624 read_and_process_input(fd, UA_sock);
629 /* Send output to both termina and specified file */
630 static int teecmd(FILE *input, BSOCK *UA_sock)
633 return do_outputcmd(input, UA_sock);
636 /* Send output to specified "file" */
637 static int outputcmd(FILE *input, BSOCK *UA_sock)
640 return do_outputcmd(input, UA_sock);
644 static int do_outputcmd(FILE *input, BSOCK *UA_sock)
650 sendit(_("Too many arguments on output/tee command.\n"));
654 if (output != stdout) {
664 fd = fopen(argk[1], mode);
666 senditf(_("Cannot open file %s for output. ERR=%s\n"),
667 argk[1], strerror(errno));
674 static int quitcmd(FILE *input, BSOCK *UA_sock)
679 static int sleepcmd(FILE *input, BSOCK *UA_sock)
682 sleep(atoi(argk[1]));
688 static int timecmd(FILE *input, BSOCK *UA_sock)
691 time_t ttime = time(NULL);
693 localtime_r(&ttime, &tm);
694 strftime(sdt, sizeof(sdt), "%d-%b-%Y %H:%M:%S", &tm);
700 * Send a line to the output file and or the terminal
702 void senditf(char *fmt,...)
707 va_start(arg_ptr, fmt);
708 bvsnprintf(buf, sizeof(buf), (char *)fmt, arg_ptr);
713 void sendit(char *buf)
716 if (output == stdout || tee) {
719 * Here, we convert every \n into \r\n because the
720 * terminal is in raw mode when we are using
723 for (p=q=buf; (p=strchr(q, '\n')); ) {
728 q = ++p; /* point after \n */
734 if (output != stdout) {
742 if (output == stdout || tee) {