]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/console/console.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / console / console.c
index de84cb93952dd7fe301fc08ee253aca2d5139f43..9bea604bd5f126bb26c5a0cce5137dc92818e858 100644 (file)
@@ -6,24 +6,18 @@
  *
  *     Version $Id$
  */
-
 /*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
+   Copyright (C) 2000-2005 Kern Sibbald
 
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as amended with additional clauses defined in the
+   file LICENSE in the main source directory.
 
-   This library is distributed in the hope that it will be useful,
+   This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
-   MA 02111-1307, USA.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+   the file LICENSE for additional details.
 
  */
 
 #ifdef HAVE_CONIO
 #include "conio.h"
 #else
-#define con_init(x) 
+#define con_init(x)
 #define con_term()
 #define con_set_zed_keys();
 #define trapctlc()
 #define clrbrk()
-#define usrbrk() 0  
+#define usrbrk() 0
 #endif
+
+#ifdef HAVE_WIN32
+#include <windows.h>
+#include "../lib/winapi.h"
+#define isatty(fd) (fd==0)
+#endif
+
 /* Exported variables */
 
 #ifdef HAVE_CYGWIN
@@ -53,23 +53,32 @@ extern int rl_catch_signals;
 /* Imported functions */
 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons);
 
-
+/* Dummy functions */
+int generate_daemon_event(JCR *jcr, const char *event) { return 1; }
 
 /* Forward referenced functions */
 static void terminate_console(int sig);
-int get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec);
+static int check_resources();
+int get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec);
 static int do_outputcmd(FILE *input, BSOCK *UA_sock);
-void senditf(char *fmt, ...);
-void sendit(char *buf);
+void senditf(const char *fmt, ...);
+void sendit(const char *buf);
+
+extern "C" void got_sigstop(int sig);
+extern "C" void got_sigcontinue(int sig);
+extern "C" void got_sigtout(int sig);
+extern "C" void got_sigtin(int sig);
+
 
 /* Static variables */
 static char *configfile = NULL;
 static BSOCK *UA_sock = NULL;
-static DIRRES *dir; 
+static DIRRES *dir;
 static FILE *output = stdout;
-static bool tee = false;                 /* output to output and stdout */
+static bool tee = false;                  /* output to output and stdout */
 static bool stop = false;
 static int argc;
+static int numdir;
 static POOLMEM *args;
 static char *argk[MAX_CMD_ARGS];
 static char *argv[MAX_CMD_ARGS];
@@ -90,36 +99,43 @@ static int sleepcmd(FILE *input, BSOCK *UA_sock);
 static void usage()
 {
    fprintf(stderr, _(
+"Copyright (C) 2000-2005 Kern Sibbald\n"
 "\nVersion: " VERSION " (" BDATE ") %s %s %s\n\n"
-"Usage: bconsole [-s] [-c config_file] [-d debug_level] [config_file]\n"
+"Usage: bconsole [-s] [-c config_file] [-d debug_level]\n"
 "       -c <file>   set configuration file to file\n"
 "       -dnn        set debug level to nn\n"
 "       -s          no signals\n"
 "       -t          test - read configuration and exit\n"
-"       -?          print this message.\n"  
+"       -?          print this message.\n"
 "\n"), HOST_OS, DISTNAME, DISTVER);
 }
 
+
+extern "C"
 void got_sigstop(int sig)
 {
    stop = true;
 }
 
+extern "C"
 void got_sigcontinue(int sig)
 {
    stop = false;
 }
 
-void got_sigtout(int sig) 
+extern "C"
+void got_sigtout(int sig)
 {
 // printf("Got tout\n");
 }
 
+extern "C"
 void got_sigtin(int sig)
-{   
+{
 // printf("Got tin\n");
 }
 
+
 static int zed_keyscmd(FILE *input, BSOCK *UA_sock)
 {
    con_set_zed_keys();
@@ -129,7 +145,7 @@ static int zed_keyscmd(FILE *input, BSOCK *UA_sock)
 /*
  * These are the @command
  */
-struct cmdstruct { char *key; int (*func)(FILE *input, BSOCK *UA_sock); char *help; }; 
+struct cmdstruct { const char *key; int (*func)(FILE *input, BSOCK *UA_sock); const char *help; };
 static struct cmdstruct commands[] = {
  { N_("input"),      inputcmd,     _("input from file")},
  { N_("output"),     outputcmd,    _("output to file")},
@@ -140,7 +156,7 @@ static struct cmdstruct commands[] = {
  { N_("version"),    versioncmd,   _("print Console's version")},
  { N_("exit"),       quitcmd,      _("exit = quit")},
  { N_("zed_keyst"),  zed_keyscmd,  _("zed_keys = use zed keys instead of bash keys")},
-            };
+             };
 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
 
 static int do_a_command(FILE *input, BSOCK *UA_sock)
@@ -164,11 +180,11 @@ static int do_a_command(FILE *input, BSOCK *UA_sock)
       return 1;
    }
    len = strlen(cmd);
-   for (i=0; i<comsize; i++) {    /* search for command */
+   for (i=0; i<comsize; i++) {     /* search for command */
       if (strncasecmp(cmd,  _(commands[i].key), len) == 0) {
-        stat = (*commands[i].func)(input, UA_sock);   /* go execute command */
-        found = 1;
-        break;
+         stat = (*commands[i].func)(input, UA_sock);   /* go execute command */
+         found = 1;
+         break;
       }
    }
    if (!found) {
@@ -180,115 +196,137 @@ static int do_a_command(FILE *input, BSOCK *UA_sock)
 }
 
 
-static void read_and_process_input(FILE *input, BSOCK *UA_sock) 
+static void read_and_process_input(FILE *input, BSOCK *UA_sock)
 {
-   char *prompt = "*";
+   const char *prompt = "*";
    bool at_prompt = false;
    int tty_input = isatty(fileno(input));
    int stat;
 
-   for ( ;; ) { 
+   for ( ;; ) {
       if (at_prompt) {                /* don't prompt multiple times */
          prompt = "";
       } else {
          prompt = "*";
-        at_prompt = true;
+         at_prompt = true;
       }
       if (tty_input) {
-        stat = get_cmd(input, prompt, UA_sock, 30);
-        if (usrbrk() == 1) {
-           clrbrk();
-        }
-        if (usrbrk()) {
-           break;
-        }
+         stat = get_cmd(input, prompt, UA_sock, 30);
+         if (usrbrk() == 1) {
+            clrbrk();
+         }
+         if (usrbrk()) {
+            break;
+         }
       } else {
-        /* Reading input from a file */
-        int len = sizeof_pool_memory(UA_sock->msg) - 1;
-        if (usrbrk()) {
-           break;
-        }
-        if (fgets(UA_sock->msg, len, input) == NULL) {
-           stat = -1;
-        } else {
-           sendit(UA_sock->msg);  /* echo to terminal */
-           strip_trailing_junk(UA_sock->msg);
-           UA_sock->msglen = strlen(UA_sock->msg);
-           stat = 1;
-        }
+         /* Reading input from a file */
+         int len = sizeof_pool_memory(UA_sock->msg) - 1;
+         if (usrbrk()) {
+            break;
+         }
+         if (fgets(UA_sock->msg, len, input) == NULL) {
+            stat = -1;
+         } else {
+            sendit(UA_sock->msg);  /* echo to terminal */
+            strip_trailing_junk(UA_sock->msg);
+            UA_sock->msglen = strlen(UA_sock->msg);
+            stat = 1;
+         }
       }
       if (stat < 0) {
-        break;                       /* error or interrupt */
-      } else if (stat == 0) {        /* timeout */
+         break;                       /* error or interrupt */
+      } else if (stat == 0) {         /* timeout */
          if (strcmp(prompt, "*") == 0) {
             bnet_fsend(UA_sock, ".messages");
-        } else {
-           continue;
-        }
+         } else {
+            continue;
+         }
       } else {
-        at_prompt = FALSE;
-        /* @ => internal command for us */
+         at_prompt = FALSE;
+         /* @ => internal command for us */
          if (UA_sock->msg[0] == '@') {
-           parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
-           if (!do_a_command(input, UA_sock)) {
-              break;
-           }
-           continue;
-        }
-        if (!bnet_send(UA_sock)) {   /* send command */
-           break;                    /* error */
-        }
+            parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
+            if (!do_a_command(input, UA_sock)) {
+               break;
+            }
+            continue;
+         }
+         if (!bnet_send(UA_sock)) {   /* send command */
+            break;                    /* error */
+         }
       }
       if (strcmp(UA_sock->msg, ".quit") == 0 || strcmp(UA_sock->msg, ".exit") == 0) {
-        break;
+         break;
       }
       while ((stat = bnet_recv(UA_sock)) >= 0) {
-        if (at_prompt) {
-           if (!stop) {
+         if (at_prompt) {
+            if (!stop) {
                sendit("\n");
-           }
-           at_prompt = false;
-        }
-        /* Suppress output if running in background or user hit ctl-c */
-        if (!stop && !usrbrk()) {
-           sendit(UA_sock->msg);
-        }
+            }
+            at_prompt = false;
+         }
+         /* Suppress output if running in background or user hit ctl-c */
+         if (!stop && !usrbrk()) {
+            sendit(UA_sock->msg);
+         }
       }
       if (usrbrk() > 1) {
-        break;
+         break;
       } else {
-        clrbrk();
+         clrbrk();
       }
       if (!stop) {
-        fflush(stdout);
+         fflush(stdout);
       }
       if (is_bnet_stop(UA_sock)) {
-        break;                       /* error or term */
+         break;                       /* error or term */
       } else if (stat == BNET_SIGNAL) {
-        if (UA_sock->msglen == BNET_PROMPT) {
-           at_prompt = true;
-        }
+         if (UA_sock->msglen == BNET_PROMPT) {
+            at_prompt = true;
+         }
          Dmsg1(100, "Got poll %s\n", bnet_sig_to_ascii(UA_sock));
       }
    }
 }
 
+/*
+ * Call-back for reading a passphrase for an encrypted PEM file
+ * This function uses getpass(), which uses a static buffer and is NOT thread-safe.
+ */
+static int tls_pem_callback(char *buf, int size, const void *userdata)
+{
+#ifdef HAVE_TLS
+   const char *prompt = (const char *) userdata;
+   char *passwd;
+
+   passwd = getpass(prompt);
+   bstrncpy(buf, passwd, size);
+   return (strlen(buf));
+#else
+   buf[0] = 0;
+   return 0;
+#endif
+}
+
 
 /*********************************************************************
  *
- *        Main Bacula Console -- User Interface Program
+ *         Main Bacula Console -- User Interface Program
  *
  */
 int main(int argc, char *argv[])
 {
-   int ch, i, ndir, item;
+   int ch, i, item;
    bool no_signals = false;
    bool test_config = false;
    JCR jcr;
 
+   setlocale(LC_ALL, "");
+   bindtextdomain("bacula", LOCALEDIR);
+   textdomain("bacula");
+
    init_stack_dump();
    my_name_is(argc, argv, "bconsole");
-   textdomain("bacula-console");
    init_msg(NULL, NULL);
    working_directory = "/tmp";
    args = get_pool_memory(PM_FNAME);
@@ -297,33 +335,33 @@ int main(int argc, char *argv[])
    while ((ch = getopt(argc, argv, "bc:d:r:st?")) != -1) {
       switch (ch) {
       case 'c':                    /* configuration file */
-        if (configfile != NULL) {
-           free(configfile);
-        }
-        configfile = bstrdup(optarg);
-        break;
+         if (configfile != NULL) {
+            free(configfile);
+         }
+         configfile = bstrdup(optarg);
+         break;
 
       case 'd':
-        debug_level = atoi(optarg);
-        if (debug_level <= 0) {
-           debug_level = 1;
-        }
-        break;
+         debug_level = atoi(optarg);
+         if (debug_level <= 0) {
+            debug_level = 1;
+         }
+         break;
 
       case 's':                    /* turn off signals */
-        no_signals = true;
-        break;
+         no_signals = true;
+         break;
 
       case 't':
-        test_config = true;
-        break;
+         test_config = true;
+         break;
 
       case '?':
       default:
-        usage();
-        con_term();
-        exit(1);
-      }  
+         usage();
+         con_term();
+         exit(1);
+      }
    }
    argc -= optind;
    argv += optind;
@@ -332,14 +370,19 @@ int main(int argc, char *argv[])
       init_signals(terminate_console);
    }
 
+#if !defined(HAVE_WIN32)
    /* Override Bacula default signals */
-   signal(SIGCHLD, SIG_IGN);
+// signal(SIGCHLD, SIG_IGN);
    signal(SIGQUIT, SIG_IGN);
    signal(SIGTSTP, got_sigstop);
    signal(SIGCONT, got_sigcontinue);
    signal(SIGTTIN, got_sigtin);
    signal(SIGTTOU, got_sigtout);
    trapctlc();
+#else
+   InitWinAPIWrapper();
+#endif
+     
 
    if (argc) {
       usage();
@@ -353,16 +396,12 @@ int main(int argc, char *argv[])
 
    parse_config(configfile);
 
-   LockRes();
-   ndir = 0;
-   foreach_res(dir, R_DIRECTOR) {
-      ndir++;
+   if (init_tls() != 0) {
+      Emsg0(M_ERROR_TERM, 0, _("TLS library initialization failed.\n"));
    }
-   UnlockRes();
-   if (ndir == 0) {
-      con_term();
-      Emsg1(M_ERROR_TERM, 0, _("No Director resource defined in %s\n\
-Without that I don't how to speak to the Director :-(\n"), configfile);
+
+   if (!check_resources()) {
+      Emsg1(M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
    }
 
    if (test_config) {
@@ -372,32 +411,34 @@ Without that I don't how to speak to the Director :-(\n"), configfile);
 
    memset(&jcr, 0, sizeof(jcr));
 
+   (void)WSA_Init();                        /* Initialize Windows sockets */
 
-   if (ndir > 1) {
-      struct sockaddr_in client_addr;
+   if (numdir > 1) {
+      struct sockaddr client_addr;
       memset(&client_addr, 0, sizeof(client_addr));
       UA_sock = init_bsock(NULL, 0, "", "", 0, &client_addr);
 try_again:
       sendit(_("Available Directors:\n"));
       LockRes();
-      ndir = 0;
+      numdir = 0;
       foreach_res(dir, R_DIRECTOR) {
-         senditf( _("%d  %s at %s:%d\n"), 1+ndir++, dir->hdr.name, dir->address,
-           dir->DIRport);
+         senditf( _("%d  %s at %s:%d\n"), 1+numdir++, dir->hdr.name, dir->address,
+            dir->DIRport);
       }
       UnlockRes();
       if (get_cmd(stdin, _("Select Director: "), UA_sock, 600) < 0) {
-        return 1;
+         (void)WSACleanup();               /* Cleanup Windows sockets */
+         return 1;
       }
       item = atoi(UA_sock->msg);
-      if (item < 0 || item > ndir) {
-         senditf(_("You must enter a number between 1 and %d\n"), ndir);
-        goto try_again;
+      if (item < 0 || item > numdir) {
+         senditf(_("You must enter a number between 1 and %d\n"), numdir);
+         goto try_again;
       }
       LockRes();
       dir = NULL;
       for (i=0; i<item; i++) {
-        dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir);
+         dir = (DIRRES *)GetNextRes(R_DIRECTOR, (RES *)dir);
       }
       UnlockRes();
       term_bsock(UA_sock);
@@ -406,22 +447,65 @@ try_again:
       dir = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
       UnlockRes();
    }
-      
+
+   LockRes();
+   CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
+   UnlockRes();
+
    senditf(_("Connecting to Director %s:%d\n"), dir->address,dir->DIRport);
-   UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address, 
-                         NULL, dir->DIRport, 0);
+
+   char buf[1024];
+   /* Initialize Console TLS context */
+   if (cons && (cons->tls_enable || cons->tls_require)) {
+      /* Generate passphrase prompt */
+      bsnprintf(buf, sizeof(buf), "Passphrase for Console \"%s\" TLS private key: ", cons->hdr.name);
+
+      /* Initialize TLS context:
+       * Args: CA certfile, CA certdir, Certfile, Keyfile,
+       * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
+      cons->tls_ctx = new_tls_context(cons->tls_ca_certfile,
+         cons->tls_ca_certdir, cons->tls_certfile,
+         cons->tls_keyfile, tls_pem_callback, &buf, NULL, true);
+
+      if (!cons->tls_ctx) {
+         senditf(_("Failed to initialize TLS context for Console \"%s\".\n"),
+            dir->hdr.name);
+         terminate_console(0);
+         return 1;
+      }
+
+   }
+
+   /* Initialize Director TLS context */
+   if (dir->tls_enable || dir->tls_require) {
+      /* Generate passphrase prompt */
+      bsnprintf(buf, sizeof(buf), "Passphrase for Director \"%s\" TLS private key: ", dir->hdr.name);
+
+      /* Initialize TLS context:
+       * Args: CA certfile, CA certdir, Certfile, Keyfile,
+       * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
+      dir->tls_ctx = new_tls_context(dir->tls_ca_certfile,
+         dir->tls_ca_certdir, dir->tls_certfile,
+         dir->tls_keyfile, tls_pem_callback, &buf, NULL, true);
+
+      if (!dir->tls_ctx) {
+         senditf(_("Failed to initialize TLS context for Director \"%s\".\n"),
+            dir->hdr.name);
+         terminate_console(0);
+         return 1;
+      }
+   }
+
+   UA_sock = bnet_connect(NULL, 5, 15, "Director daemon", dir->address,
+                          NULL, dir->DIRport, 0);
    if (UA_sock == NULL) {
       terminate_console(0);
       return 1;
    }
    jcr.dir_bsock = UA_sock;
 
-   LockRes();
-   CONRES *cons = (CONRES *)GetNextRes(R_CONSOLE, (RES *)NULL);
-   UnlockRes();
    /* If cons==NULL, default console will be used */
    if (!authenticate_director(&jcr, dir, cons)) {
-      fprintf(stderr, "ERR=%s", UA_sock->msg);
       terminate_console(0);
       return 1;
    }
@@ -438,8 +522,8 @@ try_again:
       pm_strcat(&UA_sock->msg, "/.bconsolerc");
       fd = fopen(UA_sock->msg, "r");
       if (fd) {
-        read_and_process_input(fd, UA_sock);
-        fclose(fd);
+         read_and_process_input(fd, UA_sock);
+         fclose(fd);
       }
    }
 
@@ -458,20 +542,92 @@ try_again:
 /* Cleanup and then exit */
 static void terminate_console(int sig)
 {
+
    static bool already_here = false;
 
-   if (already_here) {               /* avoid recursive temination problems */
+   if (already_here) {                /* avoid recursive temination problems */
       exit(1);
    }
    already_here = true;
+   cleanup_tls();
    free_pool_memory(args);
    con_term();
+   (void)WSACleanup();               /* Cleanup Windows sockets */
    if (sig != 0) {
       exit(1);
    }
    return;
 }
 
+/*
+ * Make a quick check to see that we have all the
+ * resources needed.
+ */
+static int check_resources()
+{
+   bool OK = true;
+   DIRRES *director;
+
+   LockRes();
+
+   numdir = 0;
+   foreach_res(director, R_DIRECTOR) {
+
+      numdir++;
+      /* tls_require implies tls_enable */
+      if (director->tls_require) {
+         if (have_tls) {
+            director->tls_enable = true;
+         } else {
+            Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
+            OK = false;
+            continue;
+         }
+      }
+
+      if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable) {
+         Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
+                             " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
+                             " At least one CA certificate store is required.\n"),
+                             director->hdr.name, configfile);
+         OK = false;
+      }
+   }
+   
+   if (numdir == 0) {
+      Emsg1(M_FATAL, 0, _("No Director resource defined in %s\n"
+                          "Without that I don't how to speak to the Director :-(\n"), configfile);
+      OK = false;
+   }
+
+   CONRES *cons;
+   /* Loop over Consoles */
+   foreach_res(cons, R_CONSOLE) {
+      /* tls_require implies tls_enable */
+      if (cons->tls_require) {
+         if (have_tls) {
+            cons->tls_enable = true;
+         } else {
+            Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
+            OK = false;
+            continue;
+         }
+      }
+
+      if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir) && cons->tls_enable) {
+         Emsg2(M_FATAL, 0, _("Neither \"TLS CA Certificate\""
+                             " or \"TLS CA Certificate Dir\" are defined for Console \"%s\" in %s.\n"),
+                             cons->hdr.name, configfile);
+         OK = false;
+      }
+   }
+
+   UnlockRes();
+
+   return OK;
+}
+
+
 #ifdef HAVE_READLINE
 #define READLINE_LIBRARY 1
 #undef free
@@ -479,13 +635,13 @@ static void terminate_console(int sig)
 #include "history.h"
 
 
-int 
-get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
+int
+get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
 {
    char *line;
 
-   rl_catch_signals = 0;             /* do it ourselves */
-   line = readline(prompt);
+   rl_catch_signals = 0;              /* do it ourselves */
+   line = readline((char *)prompt);   /* cast needed for old readlines */
 
    if (!line) {
       exit(1);
@@ -503,70 +659,82 @@ get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
 
 /*
  *   Returns: 1 if data available
- *           0 if timeout
- *          -1 if error
+ *            0 if timeout
+ *           -1 if error
  */
 static int
 wait_for_data(int fd, int sec)
 {
    fd_set fdset;
    struct timeval tv;
+#ifdef HAVE_WIN32
+   return 1;                          /* select doesn't seem to work on Win32 */
+#endif
 
    tv.tv_sec = sec;
    tv.tv_usec = 0;
    for ( ;; ) {
       FD_ZERO(&fdset);
-      FD_SET(fd, &fdset);
+      FD_SET((unsigned)fd, &fdset);
       switch(select(fd + 1, &fdset, NULL, NULL, &tv)) {
-      case 0:                        /* timeout */
-        return 0;
+      case 0:                         /* timeout */
+         return 0;
       case -1:
-        if (errno == EINTR || errno == EAGAIN) {
-           continue;
-        }
-        return -1;                  /* error return */
+         if (errno == EINTR || errno == EAGAIN) {
+            continue;
+         }
+         return -1;                  /* error return */
       default:
-        return 1;
+         return 1;
       }
    }
 }
 
-/*     
- * Get next input command from terminal. 
+/*
+ * Get next input command from terminal.
  *
  *   Returns: 1 if got input
- *           0 if timeout
- *          -1 if EOF or error
+ *            0 if timeout
+ *           -1 if EOF or error
  */
-int 
-get_cmd(FILE *input, char *prompt, BSOCK *sock, int sec)
+int
+get_cmd(FILE *input, const char *prompt, BSOCK *sock, int sec)
 {
-   int len;  
+   int len;
    if (!stop) {
       if (output == stdout || tee) {
-        sendit(prompt);
+         sendit(prompt);
       }
    }
 again:
    switch (wait_for_data(fileno(input), sec)) {
    case 0:
-      return 0;                   /* timeout */
-   case -1: 
-      return -1;                  /* error */
+      return 0;                    /* timeout */
+   case -1:
+      return -1;                   /* error */
    default:
       len = sizeof_pool_memory(sock->msg) - 1;
       if (stop) {
-        sleep(1);
-        goto again;
+         sleep(1);
+         goto again;
       }
 #ifdef HAVE_CONIO
       if (isatty(fileno(input))) {
-        input_line(sock->msg, len);
-        break;
+         input_line(sock->msg, len);
+         break;
       }
+#endif
+#ifdef HAVE_WIN32 /* use special console for input on win32 */
+      if (input == stdin) {
+         if (win32_cgets(sock->msg, len) == NULL) {
+            return -1;
+         }
+      }
+      else
 #endif
       if (fgets(sock->msg, len, input) == NULL) {
-        return -1;
+         return -1;
+
       }
       break;
    }
@@ -601,9 +769,9 @@ static int inputcmd(FILE *input, BSOCK *UA_sock)
    }
    fd = fopen(argk[1], "r");
    if (!fd) {
-      senditf(_("Cannot open file %s for input. ERR=%s\n"), 
-        argk[1], strerror(errno));
-      return 1; 
+      senditf(_("Cannot open file %s for input. ERR=%s\n"),
+         argk[1], strerror(errno));
+      return 1;
    }
    read_and_process_input(fd, UA_sock);
    fclose(fd);
@@ -628,7 +796,7 @@ static int outputcmd(FILE *input, BSOCK *UA_sock)
 static int do_outputcmd(FILE *input, BSOCK *UA_sock)
 {
    FILE *fd;
-   char *mode = "a+";
+   const char *mode = "a+";
 
    if (argc > 3) {
       sendit(_("Too many arguments on output/tee command.\n"));
@@ -636,9 +804,9 @@ static int do_outputcmd(FILE *input, BSOCK *UA_sock)
    }
    if (argc == 1) {
       if (output != stdout) {
-        fclose(output);
-        output = stdout;
-        tee = false;
+         fclose(output);
+         output = stdout;
+         tee = false;
       }
       return 1;
    }
@@ -647,9 +815,9 @@ static int do_outputcmd(FILE *input, BSOCK *UA_sock)
    }
    fd = fopen(argk[1], mode);
    if (!fd) {
-      senditf(_("Cannot open file %s for output. ERR=%s\n"), 
-        argk[1], strerror(errno));
-      return 1; 
+      senditf(_("Cannot open file %s for output. ERR=%s\n"),
+         argk[1], strerror(errno));
+      return 1;
    }
    output = fd;
    return 1;
@@ -683,7 +851,7 @@ static int timecmd(FILE *input, BSOCK *UA_sock)
 /*
  * Send a line to the output file and or the terminal
  */
-void senditf(char *fmt,...)
+void senditf(const char *fmt,...)
 {
     char buf[3000];
     va_list arg_ptr;
@@ -694,35 +862,39 @@ void senditf(char *fmt,...)
     sendit(buf);
 }
 
-void sendit(char *buf)
+void sendit(const char *buf)
 {
 #ifdef xHAVE_CONIO
     if (output == stdout || tee) {
-       char *p, *q;    
+       char *p, *q;
        /*
         * Here, we convert every \n into \r\n because the
-       *  terminal is in raw mode when we are using 
-       *  conio.
-       */
+        *  terminal is in raw mode when we are using
+        *  conio.
+        */
        for (p=q=buf; (p=strchr(q, '\n')); ) {
-         if (p-q > 0) {
-            t_sendl(q, p-q);
-         }
+          if (p-q > 0) {
+             t_sendl(q, p-q);
+          }
           t_sendl("\r\n", 2);
           q = ++p;                    /* point after \n */
        }
        if (*q) {
-         t_send(q);
+          t_send(q);
        }
     }
     if (output != stdout) {
        fputs(buf, output);
     }
 #else
+
     fputs(buf, output);
+    fflush(output);
     if (tee) {
        fputs(buf, stdout);
     }
-    fflush(stdout);
+    if (output != stdout || tee) {
+       fflush(stdout);
+    }
 #endif
 }