Introduce a new checkbox and command-line option to inhibit use of
[u/mdw/putty] / settings.c
index aa234fe..76d02c0 100644 (file)
@@ -2,6 +2,7 @@
  * settings.c: read and write saved sessions. (platform-independent)
  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "putty.h"
@@ -18,6 +19,7 @@ static const struct keyval ciphernames[] = {
     { "blowfish",   CIPHER_BLOWFISH },
     { "3des",      CIPHER_3DES },
     { "WARN",      CIPHER_WARN },
+    { "arcfour",    CIPHER_ARCFOUR },
     { "des",       CIPHER_DES }
 };
 
@@ -28,6 +30,27 @@ static const struct keyval kexnames[] = {
     { "WARN",              KEX_WARN }
 };
 
+/*
+ * All the terminal modes that we know about for the "TerminalModes"
+ * setting. (Also used by config.c for the drop-down list.)
+ * This is currently precisely the same as the set in ssh.c, but could
+ * in principle differ if other backends started to support tty modes
+ * (e.g., the pty backend).
+ */
+const char *const ttymodes[] = {
+    "INTR",    "QUIT",     "ERASE",    "KILL",     "EOF",
+    "EOL",     "EOL2",     "START",    "STOP",     "SUSP",
+    "DSUSP",   "REPRINT",  "WERASE",   "LNEXT",    "FLUSH",
+    "SWTCH",   "STATUS",   "DISCARD",  "IGNPAR",   "PARMRK",
+    "INPCK",   "ISTRIP",   "INLCR",    "IGNCR",    "ICRNL",
+    "IUCLC",   "IXON",     "IXANY",    "IXOFF",    "IMAXBEL",
+    "ISIG",    "ICANON",   "XCASE",    "ECHO",     "ECHOE",
+    "ECHOK",   "ECHONL",   "NOFLSH",   "TOSTOP",   "IEXTEN",
+    "ECHOCTL", "ECHOKE",   "PENDIN",   "OPOST",    "OLCUC",
+    "ONLCR",   "OCRNL",    "ONOCR",    "ONLRET",   "CS7",
+    "CS8",     "PARENB",   "PARODD",   NULL
+};
+
 static void gpps(void *handle, const char *name, const char *def,
                 char *val, int len)
 {
@@ -68,6 +91,61 @@ static void gppi(void *handle, char *name, int def, int *i)
     *i = read_setting_i(handle, name, def);
 }
 
+/*
+ * Read a set of name-value pairs in the format we occasionally use:
+ *   NAME\tVALUE\0NAME\tVALUE\0\0 in memory
+ *   NAME=VALUE,NAME=VALUE, in storage
+ * `def' is in the storage format.
+ */
+static void gppmap(void *handle, char *name, char *def, char *val, int len)
+{
+    char *buf = snewn(2*len, char), *p, *q;
+    gpps(handle, name, def, buf, 2*len);
+    p = buf;
+    q = val;
+    while (*p) {
+       while (*p && *p != ',') {
+           int c = *p++;
+           if (c == '=')
+               c = '\t';
+           if (c == '\\')
+               c = *p++;
+           *q++ = c;
+       }
+       if (*p == ',')
+           p++;
+       *q++ = '\0';
+    }
+    *q = '\0';
+    sfree(buf);
+}
+
+/*
+ * Write a set of name/value pairs in the above format.
+ */
+static void wmap(void *handle, char const *key, char const *value, int len)
+{
+    char *buf = snewn(2*len, char), *p;
+    const char *q;
+    p = buf;
+    q = value;
+    while (*q) {
+       while (*q) {
+           int c = *q++;
+           if (c == '=' || c == ',' || c == '\\')
+               *p++ = '\\';
+           if (c == '\t')
+               c = '=';
+           *p++ = c;
+       }
+       *p++ = ',';
+       q++;
+    }
+    *p = '\0';
+    write_setting_s(handle, key, buf);
+    sfree(buf);
+}
+
 static int key2val(const struct keyval *mapping, int nmaps, char *key)
 {
     int i;
@@ -95,6 +173,7 @@ static void gprefs(void *sesskey, char *name, char *def,
                   int *array)
 {
     char commalist[80];
+    char *tokarg = commalist;
     int n;
     unsigned long seen = 0;           /* bitmap for weeding dups etc */
     gpps(sesskey, name, def, commalist, sizeof(commalist));
@@ -104,7 +183,8 @@ static void gprefs(void *sesskey, char *name, char *def,
     do {
        int v;
        char *key;
-       key = strtok(n==0 ? commalist : NULL, ","); /* sorry */
+       key = strtok(tokarg, ","); /* sorry */
+       tokarg = NULL;
        if (!key) break;
        if (((v = key2val(mapping, nvals, key)) != -1) &&
            !(seen & 1<<v)) {
@@ -117,6 +197,7 @@ static void gprefs(void *sesskey, char *name, char *def,
     {
        int i;
        for (i = 0; i < nvals; i++) {
+           assert(mapping[i].v < 32);
            if (!(seen & 1<<mapping[i].v)) {
                array[n] = mapping[i].v;
                n++;
@@ -196,6 +277,7 @@ void save_open_settings(void *sesskey, int do_host, Config *cfg)
     write_setting_i(sesskey, "TCPKeepalives", cfg->tcp_keepalives);
     write_setting_s(sesskey, "TerminalType", cfg->termtype);
     write_setting_s(sesskey, "TerminalSpeed", cfg->termspeed);
+    wmap(sesskey, "TerminalModes", cfg->ttymodes, lenof(cfg->ttymodes));
 
     /* Address family selection */
     write_setting_i(sesskey, "AddressFamily", cfg->addressfamily);
@@ -210,30 +292,12 @@ void save_open_settings(void *sesskey, int do_host, Config *cfg)
     write_setting_s(sesskey, "ProxyUsername", cfg->proxy_username);
     write_setting_s(sesskey, "ProxyPassword", cfg->proxy_password);
     write_setting_s(sesskey, "ProxyTelnetCommand", cfg->proxy_telnet_command);
-
-    {
-       char buf[2 * sizeof(cfg->environmt)], *p, *q;
-       p = buf;
-       q = cfg->environmt;
-       while (*q) {
-           while (*q) {
-               int c = *q++;
-               if (c == '=' || c == ',' || c == '\\')
-                   *p++ = '\\';
-               if (c == '\t')
-                   c = '=';
-               *p++ = c;
-           }
-           *p++ = ',';
-           q++;
-       }
-       *p = '\0';
-       write_setting_s(sesskey, "Environment", buf);
-    }
+    wmap(sesskey, "Environment", cfg->environmt, lenof(cfg->environmt));
     write_setting_s(sesskey, "UserName", cfg->username);
     write_setting_s(sesskey, "LocalUserName", cfg->localusername);
     write_setting_i(sesskey, "NoPTY", cfg->nopty);
     write_setting_i(sesskey, "Compression", cfg->compression);
+    write_setting_i(sesskey, "TryAgent", cfg->tryagent);
     write_setting_i(sesskey, "AgentFwd", cfg->agentfwd);
     write_setting_i(sesskey, "ChangeUsername", cfg->change_username);
     wprefs(sesskey, "Cipher", ciphernames, CIPHER_MAX,
@@ -241,6 +305,7 @@ void save_open_settings(void *sesskey, int do_host, Config *cfg)
     wprefs(sesskey, "KEX", kexnames, KEX_MAX, cfg->ssh_kexlist);
     write_setting_i(sesskey, "RekeyTime", cfg->ssh_rekey_time);
     write_setting_s(sesskey, "RekeyBytes", cfg->ssh_rekey_data);
+    write_setting_i(sesskey, "SshNoAuth", cfg->ssh_no_userauth);
     write_setting_i(sesskey, "AuthTIS", cfg->try_tis_auth);
     write_setting_i(sesskey, "AuthKI", cfg->try_ki_auth);
     write_setting_i(sesskey, "SshNoShell", cfg->ssh_no_shell);
@@ -308,6 +373,7 @@ void save_open_settings(void *sesskey, int do_host, Config *cfg)
     write_setting_i(sesskey, "TermWidth", cfg->width);
     write_setting_i(sesskey, "TermHeight", cfg->height);
     write_setting_fontspec(sesskey, "Font", cfg->font);
+    write_setting_i(sesskey, "FontQuality", cfg->font_quality);
     write_setting_i(sesskey, "FontVTMode", cfg->vtmode);
     write_setting_i(sesskey, "UseSystemColours", cfg->system_colour);
     write_setting_i(sesskey, "TryPalette", cfg->try_palette);
@@ -339,6 +405,7 @@ void save_open_settings(void *sesskey, int do_host, Config *cfg)
        write_setting_s(sesskey, buf, buf2);
     }
     write_setting_s(sesskey, "LineCodePage", cfg->line_codepage);
+    write_setting_i(sesskey, "CJKAmbigWide", cfg->cjk_ambig_wide);
     write_setting_i(sesskey, "UTF8Override", cfg->utf8_override);
     write_setting_s(sesskey, "Printer", cfg->printer);
     write_setting_i(sesskey, "CapsLockCyr", cfg->xlat_capslockcyr);
@@ -355,25 +422,7 @@ void save_open_settings(void *sesskey, int do_host, Config *cfg)
     write_setting_i(sesskey, "X11AuthType", cfg->x11_auth);
     write_setting_i(sesskey, "LocalPortAcceptAll", cfg->lport_acceptall);
     write_setting_i(sesskey, "RemotePortAcceptAll", cfg->rport_acceptall);
-    {
-       char buf[2 * sizeof(cfg->portfwd)], *p, *q;
-       p = buf;
-       q = cfg->portfwd;
-       while (*q) {
-           while (*q) {
-               int c = *q++;
-               if (c == '=' || c == ',' || c == '\\')
-                   *p++ = '\\';
-               if (c == '\t')
-                   c = '=';
-               *p++ = c;
-           }
-           *p++ = ',';
-           q++;
-       }
-       *p = '\0';
-       write_setting_s(sesskey, "PortForwardings", buf);
-    }
+    wmap(sesskey, "PortForwardings", cfg->portfwd, lenof(cfg->portfwd));
     write_setting_i(sesskey, "BugIgnore1", 2-cfg->sshbug_ignore1);
     write_setting_i(sesskey, "BugPlainPW1", 2-cfg->sshbug_plainpw1);
     write_setting_i(sesskey, "BugRSA1", 2-cfg->sshbug_rsa1);
@@ -381,6 +430,7 @@ void save_open_settings(void *sesskey, int do_host, Config *cfg)
     write_setting_i(sesskey, "BugDeriveKey2", 2-cfg->sshbug_derivekey2);
     write_setting_i(sesskey, "BugRSAPad2", 2-cfg->sshbug_rsapad2);
     write_setting_i(sesskey, "BugPKSessID2", 2-cfg->sshbug_pksessid2);
+    write_setting_i(sesskey, "BugRekey2", 2-cfg->sshbug_rekey2);
     write_setting_i(sesskey, "StampUtmp", cfg->stamp_utmp);
     write_setting_i(sesskey, "LoginShell", cfg->login_shell);
     write_setting_i(sesskey, "ScrollbarOnLeft", cfg->scrollbar_on_left);
@@ -406,7 +456,7 @@ void load_open_settings(void *sesskey, int do_host, Config *cfg)
     char prot[10];
 
     cfg->ssh_subsys = 0;              /* FIXME: load this properly */
-    cfg->remote_cmd_ptr = cfg->remote_cmd;
+    cfg->remote_cmd_ptr = NULL;
     cfg->remote_cmd_ptr2 = NULL;
 
     if (do_host) {
@@ -451,6 +501,21 @@ void load_open_settings(void *sesskey, int do_host, Config *cfg)
         sizeof(cfg->termtype));
     gpps(sesskey, "TerminalSpeed", "38400,38400", cfg->termspeed,
         sizeof(cfg->termspeed));
+    {
+       /* This hardcodes a big set of defaults in any new saved
+        * sessions. Let's hope we don't change our mind. */
+       int i;
+       char *def = dupstr("");
+       /* Default: all set to "auto" */
+       for (i = 0; ttymodes[i]; i++) {
+           char *def2 = dupprintf("%s%s=A,", def, ttymodes[i]);
+           sfree(def);
+           def = def2;
+       }
+       gppmap(sesskey, "TerminalModes", def,
+              cfg->ttymodes, lenof(cfg->ttymodes));
+       sfree(def);
+    }
 
     /* proxy settings */
     gpps(sesskey, "ProxyExcludeList", "", cfg->proxy_exclude_list,
@@ -486,32 +551,13 @@ void load_open_settings(void *sesskey, int do_host, Config *cfg)
         sizeof(cfg->proxy_password));
     gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
         cfg->proxy_telnet_command, sizeof(cfg->proxy_telnet_command));
-
-    {
-       char buf[2 * sizeof(cfg->environmt)], *p, *q;
-       gpps(sesskey, "Environment", "", buf, sizeof(buf));
-       p = buf;
-       q = cfg->environmt;
-       while (*p) {
-           while (*p && *p != ',') {
-               int c = *p++;
-               if (c == '=')
-                   c = '\t';
-               if (c == '\\')
-                   c = *p++;
-               *q++ = c;
-           }
-           if (*p == ',')
-               p++;
-           *q++ = '\0';
-       }
-       *q = '\0';
-    }
+    gppmap(sesskey, "Environment", "", cfg->environmt, lenof(cfg->environmt));
     gpps(sesskey, "UserName", "", cfg->username, sizeof(cfg->username));
     gpps(sesskey, "LocalUserName", "", cfg->localusername,
         sizeof(cfg->localusername));
     gppi(sesskey, "NoPTY", 0, &cfg->nopty);
     gppi(sesskey, "Compression", 0, &cfg->compression);
+    gppi(sesskey, "TryAgent", 1, &cfg->tryagent);
     gppi(sesskey, "AgentFwd", 0, &cfg->agentfwd);
     gppi(sesskey, "ChangeUsername", 0, &cfg->change_username);
     gprefs(sesskey, "Cipher", "\0",
@@ -535,6 +581,7 @@ void load_open_settings(void *sesskey, int do_host, Config *cfg)
         sizeof(cfg->ssh_rekey_data));
     gppi(sesskey, "SshProt", 2, &cfg->sshprot);
     gppi(sesskey, "SSH2DES", 0, &cfg->ssh2_des_cbc);
+    gppi(sesskey, "SshNoAuth", 0, &cfg->ssh_no_userauth);
     gppi(sesskey, "AuthTIS", 0, &cfg->try_tis_auth);
     gppi(sesskey, "AuthKI", 1, &cfg->try_ki_auth);
     gppi(sesskey, "SshNoShell", 0, &cfg->ssh_no_shell);
@@ -605,6 +652,7 @@ void load_open_settings(void *sesskey, int do_host, Config *cfg)
     gppi(sesskey, "TermWidth", 80, &cfg->width);
     gppi(sesskey, "TermHeight", 24, &cfg->height);
     gppfont(sesskey, "Font", &cfg->font);
+    gppi(sesskey, "FontQuality", FQ_DEFAULT, &cfg->font_quality);
     gppi(sesskey, "FontVTMode", VT_UNICODE, (int *) &cfg->vtmode);
     gppi(sesskey, "UseSystemColours", 0, &cfg->system_colour);
     gppi(sesskey, "TryPalette", 0, &cfg->try_palette);
@@ -666,6 +714,7 @@ void load_open_settings(void *sesskey, int do_host, Config *cfg)
      */
     gpps(sesskey, "LineCodePage", "", cfg->line_codepage,
         sizeof(cfg->line_codepage));
+    gppi(sesskey, "CJKAmbigWide", 0, &cfg->cjk_ambig_wide);
     gppi(sesskey, "UTF8Override", 1, &cfg->utf8_override);
     gpps(sesskey, "Printer", "", cfg->printer, sizeof(cfg->printer));
     gppi (sesskey, "CapsLockCyr", 0, &cfg->xlat_capslockcyr);
@@ -684,26 +733,7 @@ void load_open_settings(void *sesskey, int do_host, Config *cfg)
 
     gppi(sesskey, "LocalPortAcceptAll", 0, &cfg->lport_acceptall);
     gppi(sesskey, "RemotePortAcceptAll", 0, &cfg->rport_acceptall);
-    {
-       char buf[2 * sizeof(cfg->portfwd)], *p, *q;
-       gpps(sesskey, "PortForwardings", "", buf, sizeof(buf));
-       p = buf;
-       q = cfg->portfwd;
-       while (*p) {
-           while (*p && *p != ',') {
-               int c = *p++;
-               if (c == '=')
-                   c = '\t';
-               if (c == '\\')
-                   c = *p++;
-               *q++ = c;
-           }
-           if (*p == ',')
-               p++;
-           *q++ = '\0';
-       }
-       *q = '\0';
-    }
+    gppmap(sesskey, "PortForwardings", "", cfg->portfwd, lenof(cfg->portfwd));
     gppi(sesskey, "BugIgnore1", 0, &i); cfg->sshbug_ignore1 = 2-i;
     gppi(sesskey, "BugPlainPW1", 0, &i); cfg->sshbug_plainpw1 = 2-i;
     gppi(sesskey, "BugRSA1", 0, &i); cfg->sshbug_rsa1 = 2-i;
@@ -816,5 +846,7 @@ void get_sesslist(struct sesslist *list, int allocate)
     } else {
        sfree(list->buffer);
        sfree(list->sessions);
+       list->buffer = NULL;
+       list->sessions = NULL;
     }
 }