2002-10-09 18:09:42 +00:00
|
|
|
/*
|
|
|
|
* uxstore.c: Unix-specific implementation of the interface defined
|
|
|
|
* in storage.h.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-04-01 18:10:25 +00:00
|
|
|
#include <string.h>
|
2003-03-31 11:36:14 +00:00
|
|
|
#include <assert.h>
|
2003-04-01 18:10:25 +00:00
|
|
|
#include <errno.h>
|
2002-10-16 22:54:58 +00:00
|
|
|
#include <ctype.h>
|
2005-09-13 20:08:25 +00:00
|
|
|
#include <limits.h>
|
2002-10-31 19:49:52 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2003-03-31 11:36:14 +00:00
|
|
|
#include <dirent.h>
|
2002-10-31 19:49:52 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2008-03-22 12:01:16 +00:00
|
|
|
#include <pwd.h>
|
2002-10-09 18:09:42 +00:00
|
|
|
#include "putty.h"
|
|
|
|
#include "storage.h"
|
2002-10-16 22:54:58 +00:00
|
|
|
#include "tree234.h"
|
2002-10-09 18:09:42 +00:00
|
|
|
|
2005-09-13 20:08:25 +00:00
|
|
|
#ifdef PATH_MAX
|
|
|
|
#define FNLEN PATH_MAX
|
|
|
|
#else
|
|
|
|
#define FNLEN 1024 /* XXX */
|
|
|
|
#endif
|
|
|
|
|
2003-03-31 11:36:14 +00:00
|
|
|
enum {
|
2004-01-17 13:00:18 +00:00
|
|
|
INDEX_DIR, INDEX_HOSTKEYS, INDEX_HOSTKEYS_TMP, INDEX_RANDSEED,
|
2003-03-31 11:36:14 +00:00
|
|
|
INDEX_SESSIONDIR, INDEX_SESSION,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char hex[16] = "0123456789ABCDEF";
|
|
|
|
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
static void make_session_filename(const char *in, strbuf *out)
|
2003-03-31 11:36:14 +00:00
|
|
|
{
|
2003-04-11 17:42:52 +00:00
|
|
|
if (!in || !*in)
|
2003-03-31 11:36:14 +00:00
|
|
|
in = "Default Settings";
|
|
|
|
|
|
|
|
while (*in) {
|
|
|
|
/*
|
|
|
|
* There are remarkably few punctuation characters that
|
|
|
|
* aren't shell-special in some way or likely to be used as
|
|
|
|
* separators in some file format or another! Hence we use
|
|
|
|
* opt-in for safe characters rather than opt-out for
|
|
|
|
* specific unsafe ones...
|
|
|
|
*/
|
2019-09-08 19:29:00 +00:00
|
|
|
if (*in!='+' && *in!='-' && *in!='.' && *in!='@' && *in!='_' &&
|
2003-03-31 11:36:14 +00:00
|
|
|
!(*in >= '0' && *in <= '9') &&
|
|
|
|
!(*in >= 'A' && *in <= 'Z') &&
|
|
|
|
!(*in >= 'a' && *in <= 'z')) {
|
2019-09-08 19:29:00 +00:00
|
|
|
put_byte(out, '%');
|
|
|
|
put_byte(out, hex[((unsigned char) *in) >> 4]);
|
|
|
|
put_byte(out, hex[((unsigned char) *in) & 15]);
|
|
|
|
} else
|
|
|
|
put_byte(out, *in);
|
|
|
|
in++;
|
2003-03-31 11:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
static void decode_session_filename(const char *in, strbuf *out)
|
2003-03-31 11:36:14 +00:00
|
|
|
{
|
|
|
|
while (*in) {
|
2019-09-08 19:29:00 +00:00
|
|
|
if (*in == '%' && in[1] && in[2]) {
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
i = in[1] - '0';
|
|
|
|
i -= (i > 9 ? 7 : 0);
|
|
|
|
j = in[2] - '0';
|
|
|
|
j -= (j > 9 ? 7 : 0);
|
|
|
|
|
|
|
|
put_byte(out, (i << 4) + j);
|
|
|
|
in += 3;
|
|
|
|
} else {
|
|
|
|
put_byte(out, *in++);
|
|
|
|
}
|
2003-03-31 11:36:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
static char *make_filename(int index, const char *subname)
|
2003-03-31 11:36:14 +00:00
|
|
|
{
|
2008-03-22 12:01:16 +00:00
|
|
|
char *env, *tmp, *ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow override of the PuTTY configuration location, and of
|
|
|
|
* specific subparts of it, by means of environment variables.
|
|
|
|
*/
|
|
|
|
if (index == INDEX_DIR) {
|
2019-09-08 19:29:00 +00:00
|
|
|
struct passwd *pwd;
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
char *xdg_dir, *old_dir, *old_dir2, *old_dir3, *home, *pwd_home;
|
2008-03-22 12:01:16 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
env = getenv("PUTTYDIR");
|
|
|
|
if (env)
|
|
|
|
return dupstr(env);
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
|
|
|
|
home = getenv("HOME");
|
2019-09-08 19:29:00 +00:00
|
|
|
pwd = getpwuid(getuid());
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
if (pwd && pwd->pw_dir) {
|
|
|
|
pwd_home = pwd->pw_dir;
|
|
|
|
} else {
|
|
|
|
pwd_home = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
xdg_dir = NULL;
|
|
|
|
env = getenv("XDG_CONFIG_HOME");
|
|
|
|
if (env && *env) {
|
|
|
|
xdg_dir = dupprintf("%s/putty", env);
|
|
|
|
}
|
|
|
|
if (!xdg_dir) {
|
|
|
|
if (home) {
|
|
|
|
tmp = home;
|
|
|
|
} else if (pwd_home) {
|
|
|
|
tmp = pwd_home;
|
|
|
|
} else {
|
|
|
|
tmp = "";
|
|
|
|
}
|
|
|
|
xdg_dir = dupprintf("%s/.config/putty", tmp);
|
|
|
|
}
|
|
|
|
if (xdg_dir && access(xdg_dir, F_OK) == 0) {
|
|
|
|
return xdg_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_dir = old_dir2 = old_dir3 = NULL;
|
|
|
|
if (home) {
|
|
|
|
old_dir = dupprintf("%s/.putty", home);
|
|
|
|
}
|
|
|
|
if (pwd_home) {
|
|
|
|
old_dir2 = dupprintf("%s/.putty", pwd_home);
|
|
|
|
}
|
|
|
|
old_dir3 = dupstr("/.putty");
|
|
|
|
|
2017-02-14 20:47:16 +00:00
|
|
|
if (old_dir && access(old_dir, F_OK) == 0) {
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
ret = old_dir;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-02-14 20:47:16 +00:00
|
|
|
if (old_dir2 && access(old_dir2, F_OK) == 0) {
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
ret = old_dir2;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (access(old_dir3, F_OK) == 0) {
|
|
|
|
ret = old_dir3;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#ifdef XDG_DEFAULT
|
|
|
|
if (xdg_dir) {
|
|
|
|
ret = xdg_dir;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ret = old_dir ? old_dir : (old_dir2 ? old_dir2 : old_dir3);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (ret != old_dir)
|
|
|
|
sfree(old_dir);
|
|
|
|
if (ret != old_dir2)
|
|
|
|
sfree(old_dir2);
|
|
|
|
if (ret != old_dir3)
|
|
|
|
sfree(old_dir3);
|
|
|
|
if (ret != xdg_dir)
|
|
|
|
sfree(xdg_dir);
|
|
|
|
return ret;
|
2008-03-22 12:01:16 +00:00
|
|
|
}
|
|
|
|
if (index == INDEX_SESSIONDIR) {
|
2019-09-08 19:29:00 +00:00
|
|
|
env = getenv("PUTTYSESSIONS");
|
|
|
|
if (env)
|
|
|
|
return dupstr(env);
|
|
|
|
tmp = make_filename(INDEX_DIR, NULL);
|
|
|
|
ret = dupprintf("%s/sessions", tmp);
|
|
|
|
sfree(tmp);
|
|
|
|
return ret;
|
2008-03-22 12:01:16 +00:00
|
|
|
}
|
2003-03-31 11:36:14 +00:00
|
|
|
if (index == INDEX_SESSION) {
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
strbuf *sb = strbuf_new();
|
2019-09-08 19:29:00 +00:00
|
|
|
tmp = make_filename(INDEX_SESSIONDIR, NULL);
|
|
|
|
strbuf_catf(sb, "%s/", tmp);
|
|
|
|
sfree(tmp);
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
make_session_filename(subname, sb);
|
|
|
|
return strbuf_to_str(sb);
|
2008-03-22 12:01:16 +00:00
|
|
|
}
|
|
|
|
if (index == INDEX_HOSTKEYS) {
|
2019-09-08 19:29:00 +00:00
|
|
|
env = getenv("PUTTYSSHHOSTKEYS");
|
|
|
|
if (env)
|
|
|
|
return dupstr(env);
|
|
|
|
tmp = make_filename(INDEX_DIR, NULL);
|
|
|
|
ret = dupprintf("%s/sshhostkeys", tmp);
|
|
|
|
sfree(tmp);
|
|
|
|
return ret;
|
2008-03-22 12:01:16 +00:00
|
|
|
}
|
|
|
|
if (index == INDEX_HOSTKEYS_TMP) {
|
2019-09-08 19:29:00 +00:00
|
|
|
tmp = make_filename(INDEX_HOSTKEYS, NULL);
|
|
|
|
ret = dupprintf("%s.tmp", tmp);
|
|
|
|
sfree(tmp);
|
|
|
|
return ret;
|
2008-03-22 12:01:16 +00:00
|
|
|
}
|
|
|
|
if (index == INDEX_RANDSEED) {
|
2019-09-08 19:29:00 +00:00
|
|
|
env = getenv("PUTTYRANDOMSEED");
|
|
|
|
if (env)
|
|
|
|
return dupstr(env);
|
|
|
|
tmp = make_filename(INDEX_DIR, NULL);
|
|
|
|
ret = dupprintf("%s/randomseed", tmp);
|
|
|
|
sfree(tmp);
|
|
|
|
return ret;
|
2003-03-31 11:36:14 +00:00
|
|
|
}
|
2008-03-22 12:01:16 +00:00
|
|
|
tmp = make_filename(INDEX_DIR, NULL);
|
|
|
|
ret = dupprintf("%s/ERROR", tmp);
|
|
|
|
sfree(tmp);
|
|
|
|
return ret;
|
2003-03-31 11:36:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
struct settings_w {
|
|
|
|
FILE *fp;
|
|
|
|
};
|
|
|
|
|
|
|
|
settings_w *open_settings_w(const char *sessionname, char **errmsg)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
char *filename, *err;
|
2003-03-31 11:36:14 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
2003-04-01 18:10:25 +00:00
|
|
|
*errmsg = NULL;
|
|
|
|
|
2003-03-31 11:36:14 +00:00
|
|
|
/*
|
2003-04-01 18:10:25 +00:00
|
|
|
* Start by making sure the .putty directory and its sessions
|
2013-07-19 17:44:53 +00:00
|
|
|
* subdir actually exist.
|
2003-03-31 11:36:14 +00:00
|
|
|
*/
|
2013-07-19 17:44:53 +00:00
|
|
|
filename = make_filename(INDEX_DIR, NULL);
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
if ((err = make_dir_path(filename, 0700)) != NULL) {
|
|
|
|
*errmsg = dupprintf("Unable to save session: %s", err);
|
|
|
|
sfree(err);
|
2013-07-19 17:44:53 +00:00
|
|
|
sfree(filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sfree(filename);
|
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
filename = make_filename(INDEX_SESSIONDIR, NULL);
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
if ((err = make_dir_path(filename, 0700)) != NULL) {
|
|
|
|
*errmsg = dupprintf("Unable to save session: %s", err);
|
|
|
|
sfree(err);
|
2013-07-19 17:44:53 +00:00
|
|
|
sfree(filename);
|
|
|
|
return NULL;
|
2008-03-22 12:01:16 +00:00
|
|
|
}
|
|
|
|
sfree(filename);
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
filename = make_filename(INDEX_SESSION, sessionname);
|
2003-03-31 11:36:14 +00:00
|
|
|
fp = fopen(filename, "w");
|
2003-04-01 18:10:25 +00:00
|
|
|
if (!fp) {
|
2013-07-19 17:44:53 +00:00
|
|
|
*errmsg = dupprintf("Unable to save session: open(\"%s\") "
|
|
|
|
"returned '%s'", filename, strerror(errno));
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(filename);
|
|
|
|
return NULL; /* can't open */
|
2003-04-01 18:10:25 +00:00
|
|
|
}
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(filename);
|
2018-09-14 07:45:42 +00:00
|
|
|
|
|
|
|
settings_w *toret = snew(settings_w);
|
|
|
|
toret->fp = fp;
|
|
|
|
return toret;
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
void write_setting_s(settings_w *handle, const char *key, const char *value)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2018-09-14 07:45:42 +00:00
|
|
|
fprintf(handle->fp, "%s=%s\n", key, value);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
void write_setting_i(settings_w *handle, const char *key, int value)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2018-09-14 07:45:42 +00:00
|
|
|
fprintf(handle->fp, "%s=%d\n", key, value);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
void close_settings_w(settings_w *handle)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2018-09-14 07:45:42 +00:00
|
|
|
fclose(handle->fp);
|
|
|
|
sfree(handle);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 10:46:47 +00:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* System for treating X resources as a fallback source of defaults,
|
|
|
|
* after data read from a saved-session disk file.
|
|
|
|
*
|
|
|
|
* The read_setting_* functions will call get_setting(key) as a
|
|
|
|
* fallback if the setting isn't in the file they loaded. That in turn
|
|
|
|
* will hand on to x_get_default, which the front end application
|
|
|
|
* provides, and which actually reads resources from the X server (if
|
|
|
|
* appropriate). In between, there's a tree234 of X-resource shaped
|
|
|
|
* settings living locally in this file: the front end can call
|
|
|
|
* provide_xrm_string() to insert a setting into this tree (typically
|
|
|
|
* in response to an -xrm command line option or similar), and those
|
|
|
|
* will override the actual X resources.
|
2002-10-16 14:32:06 +00:00
|
|
|
*/
|
|
|
|
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval {
|
2003-01-14 18:43:45 +00:00
|
|
|
const char *key;
|
|
|
|
const char *value;
|
2002-10-16 22:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static tree234 *xrmtree = NULL;
|
|
|
|
|
2018-10-06 10:46:37 +00:00
|
|
|
static int keycmp(void *av, void *bv)
|
2002-10-16 22:54:58 +00:00
|
|
|
{
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval *a = (struct skeyval *)av;
|
|
|
|
struct skeyval *b = (struct skeyval *)bv;
|
2002-10-16 22:54:58 +00:00
|
|
|
return strcmp(a->key, b->key);
|
|
|
|
}
|
|
|
|
|
2019-04-13 18:12:53 +00:00
|
|
|
void provide_xrm_string(const char *string, const char *progname)
|
2002-10-16 22:54:58 +00:00
|
|
|
{
|
2019-04-13 18:09:56 +00:00
|
|
|
const char *p, *q;
|
|
|
|
char *key;
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval *xrms, *ret;
|
2002-10-16 22:54:58 +00:00
|
|
|
|
|
|
|
p = q = strchr(string, ':');
|
|
|
|
if (!q) {
|
2019-09-08 19:29:00 +00:00
|
|
|
fprintf(stderr, "%s: expected a colon in resource string"
|
|
|
|
" \"%s\"\n", progname, string);
|
|
|
|
return;
|
2002-10-16 22:54:58 +00:00
|
|
|
}
|
|
|
|
q++;
|
|
|
|
while (p > string && p[-1] != '.' && p[-1] != '*')
|
2019-09-08 19:29:00 +00:00
|
|
|
p--;
|
2010-05-19 18:22:17 +00:00
|
|
|
xrms = snew(struct skeyval);
|
2003-03-29 16:14:26 +00:00
|
|
|
key = snewn(q-p, char);
|
2003-01-14 18:43:45 +00:00
|
|
|
memcpy(key, p, q-p);
|
|
|
|
key[q-p-1] = '\0';
|
|
|
|
xrms->key = key;
|
2003-03-11 09:30:31 +00:00
|
|
|
while (*q && isspace((unsigned char)*q))
|
2019-09-08 19:29:00 +00:00
|
|
|
q++;
|
2002-10-16 22:54:58 +00:00
|
|
|
xrms->value = dupstr(q);
|
|
|
|
|
|
|
|
if (!xrmtree)
|
2019-09-08 19:29:00 +00:00
|
|
|
xrmtree = newtree234(keycmp);
|
2002-10-16 22:54:58 +00:00
|
|
|
|
|
|
|
ret = add234(xrmtree, xrms);
|
|
|
|
if (ret) {
|
2019-09-08 19:29:00 +00:00
|
|
|
/* Override an existing string. */
|
|
|
|
del234(xrmtree, ret);
|
|
|
|
add234(xrmtree, xrms);
|
2002-10-16 22:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-06 10:46:37 +00:00
|
|
|
static const char *get_setting(const char *key)
|
2002-10-16 22:54:58 +00:00
|
|
|
{
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval tmp, *ret;
|
2002-10-16 22:54:58 +00:00
|
|
|
tmp.key = key;
|
|
|
|
if (xrmtree) {
|
2019-09-08 19:29:00 +00:00
|
|
|
ret = find234(xrmtree, &tmp, NULL);
|
|
|
|
if (ret)
|
|
|
|
return ret->value;
|
2002-10-16 22:54:58 +00:00
|
|
|
}
|
2002-10-31 19:49:52 +00:00
|
|
|
return x_get_default(key);
|
2002-10-16 22:54:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 10:46:47 +00:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
* Main code for reading settings from a disk file, calling the above
|
|
|
|
* get_setting() as a fallback if necessary.
|
|
|
|
*/
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
struct settings_r {
|
|
|
|
tree234 *t;
|
|
|
|
};
|
|
|
|
|
|
|
|
settings_r *open_settings_r(const char *sessionname)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2008-03-22 12:01:16 +00:00
|
|
|
char *filename;
|
2003-03-31 11:36:14 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *line;
|
2018-09-14 07:45:42 +00:00
|
|
|
settings_r *toret;
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
filename = make_filename(INDEX_SESSION, sessionname);
|
2003-03-31 11:36:14 +00:00
|
|
|
fp = fopen(filename, "r");
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(filename);
|
2003-03-31 11:36:14 +00:00
|
|
|
if (!fp)
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL; /* can't open */
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
toret = snew(settings_r);
|
|
|
|
toret->t = newtree234(keycmp);
|
2003-03-31 11:36:14 +00:00
|
|
|
|
|
|
|
while ( (line = fgetline(fp)) ) {
|
|
|
|
char *value = strchr(line, '=');
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval *kv;
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2013-07-14 10:46:07 +00:00
|
|
|
if (!value) {
|
|
|
|
sfree(line);
|
2003-03-31 11:36:14 +00:00
|
|
|
continue;
|
2013-07-14 10:46:07 +00:00
|
|
|
}
|
2003-03-31 11:36:14 +00:00
|
|
|
*value++ = '\0';
|
|
|
|
value[strcspn(value, "\r\n")] = '\0'; /* trim trailing NL */
|
|
|
|
|
2010-05-19 18:22:17 +00:00
|
|
|
kv = snew(struct skeyval);
|
2003-03-31 11:36:14 +00:00
|
|
|
kv->key = dupstr(line);
|
|
|
|
kv->value = dupstr(value);
|
2018-09-14 07:45:42 +00:00
|
|
|
add234(toret->t, kv);
|
2003-03-31 11:36:14 +00:00
|
|
|
|
|
|
|
sfree(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
return toret;
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
char *read_setting_s(settings_r *handle, const char *key)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2003-03-31 11:36:14 +00:00
|
|
|
const char *val;
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval tmp, *kv;
|
2003-03-31 11:36:14 +00:00
|
|
|
|
|
|
|
tmp.key = key;
|
2018-09-14 07:45:42 +00:00
|
|
|
if (handle != NULL &&
|
|
|
|
(kv = find234(handle->t, &tmp, NULL)) != NULL) {
|
2003-03-31 11:36:14 +00:00
|
|
|
val = kv->value;
|
|
|
|
assert(val != NULL);
|
|
|
|
} else
|
|
|
|
val = get_setting(key);
|
|
|
|
|
2002-10-16 14:32:06 +00:00
|
|
|
if (!val)
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
else
|
2019-09-08 19:29:00 +00:00
|
|
|
return dupstr(val);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
int read_setting_i(settings_r *handle, const char *key, int defvalue)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2003-03-31 11:36:14 +00:00
|
|
|
const char *val;
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval tmp, *kv;
|
2003-03-31 11:36:14 +00:00
|
|
|
|
|
|
|
tmp.key = key;
|
2018-09-14 07:45:42 +00:00
|
|
|
if (handle != NULL &&
|
|
|
|
(kv = find234(handle->t, &tmp, NULL)) != NULL) {
|
2003-03-31 11:36:14 +00:00
|
|
|
val = kv->value;
|
|
|
|
assert(val != NULL);
|
|
|
|
} else
|
|
|
|
val = get_setting(key);
|
|
|
|
|
2002-10-16 14:32:06 +00:00
|
|
|
if (!val)
|
2019-09-08 19:29:00 +00:00
|
|
|
return defvalue;
|
2002-10-16 14:32:06 +00:00
|
|
|
else
|
2019-09-08 19:29:00 +00:00
|
|
|
return atoi(val);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
FontSpec *read_setting_fontspec(settings_r *handle, const char *name)
|
2003-02-01 12:54:40 +00:00
|
|
|
{
|
In the new unified font handling, my strategy so far for combining
client- and server-side fonts into a single namespace was mainly to
hope there would naturally be no collisions, and to provide
disambiguating "client:" and "server:" prefixes for manual use in
emergencies.
Jacob points out, however, that his system not only has a namespace
clash but worse still the clash is at the name "fixed", which is our
default font! So, modify my namespace policy to use the
disambiguating prefixes everywhere by default, and use _unprefixed_
names only if the user types one in by hand.
In particular, I've changed the keys used to store font names in
Unix saved session files. Font names read from the new keys will be
passed straight to the new unifont framework; font names read from
the old keys will have "server:" prepended. So any existing
configuration file for GTK1 PuTTY should now work reliably in GTK2
PuTTY and select the same font, even if that font is one on which
your system (rather, your client+server combination) has a font
namespace clash.
[originally from svn r7973]
2008-04-05 13:37:20 +00:00
|
|
|
/*
|
|
|
|
* In GTK1-only PuTTY, we used to store font names simply as a
|
|
|
|
* valid X font description string (logical or alias), under a
|
|
|
|
* bare key such as "Font".
|
2019-09-08 19:29:00 +00:00
|
|
|
*
|
In the new unified font handling, my strategy so far for combining
client- and server-side fonts into a single namespace was mainly to
hope there would naturally be no collisions, and to provide
disambiguating "client:" and "server:" prefixes for manual use in
emergencies.
Jacob points out, however, that his system not only has a namespace
clash but worse still the clash is at the name "fixed", which is our
default font! So, modify my namespace policy to use the
disambiguating prefixes everywhere by default, and use _unprefixed_
names only if the user types one in by hand.
In particular, I've changed the keys used to store font names in
Unix saved session files. Font names read from the new keys will be
passed straight to the new unifont framework; font names read from
the old keys will have "server:" prepended. So any existing
configuration file for GTK1 PuTTY should now work reliably in GTK2
PuTTY and select the same font, even if that font is one on which
your system (rather, your client+server combination) has a font
namespace clash.
[originally from svn r7973]
2008-04-05 13:37:20 +00:00
|
|
|
* In GTK2 PuTTY, we have a prefix system where "client:"
|
|
|
|
* indicates a Pango font and "server:" an X one; existing
|
|
|
|
* configuration needs to be reinterpreted as having the
|
|
|
|
* "server:" prefix, so we change the storage key from the
|
|
|
|
* provided name string (e.g. "Font") to a suffixed one
|
|
|
|
* ("FontName").
|
|
|
|
*/
|
Make dupcat() into a variadic macro.
Up until now, it's been a variadic _function_, whose argument list
consists of 'const char *' ASCIZ strings to concatenate, terminated by
one containing a null pointer. Now, that function is dupcat_fn(), and
it's wrapped by a C99 variadic _macro_ called dupcat(), which
automatically suffixes the null-pointer terminating argument.
This has three benefits. Firstly, it's just less effort at every call
site. Secondly, it protects against the risk of accidentally leaving
off the NULL, causing arbitrary words of stack memory to be
dereferenced as char pointers. And thirdly, it protects against the
more subtle risk of writing a bare 'NULL' as the terminating argument,
instead of casting it explicitly to a pointer. That last one is
necessary because C permits the macro NULL to expand to an integer
constant such as 0, so NULL by itself may not have pointer type, and
worse, it may not be marshalled in a variadic argument list in the
same way as a pointer. (For example, on a 64-bit machine it might only
occupy 32 bits. And yet, on another 64-bit platform, it might work
just fine, so that you don't notice the mistake!)
I was inspired to do this by happening to notice one of those bare
NULL terminators, and thinking I'd better check if there were any
more. Turned out there were quite a few. Now there are none.
2019-10-14 18:42:37 +00:00
|
|
|
char *suffname = dupcat(name, "Name");
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if ((tmp = read_setting_s(handle, suffname)) != NULL) {
|
2011-10-01 17:38:59 +00:00
|
|
|
FontSpec *fs = fontspec_new(tmp);
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(suffname);
|
|
|
|
sfree(tmp);
|
|
|
|
return fs; /* got new-style name */
|
In the new unified font handling, my strategy so far for combining
client- and server-side fonts into a single namespace was mainly to
hope there would naturally be no collisions, and to provide
disambiguating "client:" and "server:" prefixes for manual use in
emergencies.
Jacob points out, however, that his system not only has a namespace
clash but worse still the clash is at the name "fixed", which is our
default font! So, modify my namespace policy to use the
disambiguating prefixes everywhere by default, and use _unprefixed_
names only if the user types one in by hand.
In particular, I've changed the keys used to store font names in
Unix saved session files. Font names read from the new keys will be
passed straight to the new unifont framework; font names read from
the old keys will have "server:" prepended. So any existing
configuration file for GTK1 PuTTY should now work reliably in GTK2
PuTTY and select the same font, even if that font is one on which
your system (rather, your client+server combination) has a font
namespace clash.
[originally from svn r7973]
2008-04-05 13:37:20 +00:00
|
|
|
}
|
|
|
|
sfree(suffname);
|
|
|
|
|
|
|
|
/* Fall back to old-style name. */
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
tmp = read_setting_s(handle, name);
|
|
|
|
if (tmp && *tmp) {
|
Make dupcat() into a variadic macro.
Up until now, it's been a variadic _function_, whose argument list
consists of 'const char *' ASCIZ strings to concatenate, terminated by
one containing a null pointer. Now, that function is dupcat_fn(), and
it's wrapped by a C99 variadic _macro_ called dupcat(), which
automatically suffixes the null-pointer terminating argument.
This has three benefits. Firstly, it's just less effort at every call
site. Secondly, it protects against the risk of accidentally leaving
off the NULL, causing arbitrary words of stack memory to be
dereferenced as char pointers. And thirdly, it protects against the
more subtle risk of writing a bare 'NULL' as the terminating argument,
instead of casting it explicitly to a pointer. That last one is
necessary because C permits the macro NULL to expand to an integer
constant such as 0, so NULL by itself may not have pointer type, and
worse, it may not be marshalled in a variadic argument list in the
same way as a pointer. (For example, on a 64-bit machine it might only
occupy 32 bits. And yet, on another 64-bit platform, it might work
just fine, so that you don't notice the mistake!)
I was inspired to do this by happening to notice one of those bare
NULL terminators, and thinking I'd better check if there were any
more. Turned out there were quite a few. Now there are none.
2019-10-14 18:42:37 +00:00
|
|
|
char *tmp2 = dupcat("server:", tmp);
|
2011-10-01 17:38:59 +00:00
|
|
|
FontSpec *fs = fontspec_new(tmp2);
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(tmp2);
|
|
|
|
sfree(tmp);
|
|
|
|
return fs;
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
} else {
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(tmp);
|
|
|
|
return NULL;
|
In the new unified font handling, my strategy so far for combining
client- and server-side fonts into a single namespace was mainly to
hope there would naturally be no collisions, and to provide
disambiguating "client:" and "server:" prefixes for manual use in
emergencies.
Jacob points out, however, that his system not only has a namespace
clash but worse still the clash is at the name "fixed", which is our
default font! So, modify my namespace policy to use the
disambiguating prefixes everywhere by default, and use _unprefixed_
names only if the user types one in by hand.
In particular, I've changed the keys used to store font names in
Unix saved session files. Font names read from the new keys will be
passed straight to the new unifont framework; font names read from
the old keys will have "server:" prepended. So any existing
configuration file for GTK1 PuTTY should now work reliably in GTK2
PuTTY and select the same font, even if that font is one on which
your system (rather, your client+server combination) has a font
namespace clash.
[originally from svn r7973]
2008-04-05 13:37:20 +00:00
|
|
|
}
|
2003-02-01 12:54:40 +00:00
|
|
|
}
|
2018-09-14 07:45:42 +00:00
|
|
|
Filename *read_setting_filename(settings_r *handle, const char *name)
|
2003-02-01 12:54:40 +00:00
|
|
|
{
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
char *tmp = read_setting_s(handle, name);
|
|
|
|
if (tmp) {
|
2011-10-02 11:01:57 +00:00
|
|
|
Filename *ret = filename_from_str(tmp);
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(tmp);
|
|
|
|
return ret;
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
} else
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
2003-02-01 12:54:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
void write_setting_fontspec(settings_w *handle, const char *name, FontSpec *fs)
|
2003-02-01 12:54:40 +00:00
|
|
|
{
|
In the new unified font handling, my strategy so far for combining
client- and server-side fonts into a single namespace was mainly to
hope there would naturally be no collisions, and to provide
disambiguating "client:" and "server:" prefixes for manual use in
emergencies.
Jacob points out, however, that his system not only has a namespace
clash but worse still the clash is at the name "fixed", which is our
default font! So, modify my namespace policy to use the
disambiguating prefixes everywhere by default, and use _unprefixed_
names only if the user types one in by hand.
In particular, I've changed the keys used to store font names in
Unix saved session files. Font names read from the new keys will be
passed straight to the new unifont framework; font names read from
the old keys will have "server:" prepended. So any existing
configuration file for GTK1 PuTTY should now work reliably in GTK2
PuTTY and select the same font, even if that font is one on which
your system (rather, your client+server combination) has a font
namespace clash.
[originally from svn r7973]
2008-04-05 13:37:20 +00:00
|
|
|
/*
|
|
|
|
* read_setting_fontspec had to handle two cases, but when
|
|
|
|
* writing our settings back out we simply always generate the
|
|
|
|
* new-style name.
|
|
|
|
*/
|
Make dupcat() into a variadic macro.
Up until now, it's been a variadic _function_, whose argument list
consists of 'const char *' ASCIZ strings to concatenate, terminated by
one containing a null pointer. Now, that function is dupcat_fn(), and
it's wrapped by a C99 variadic _macro_ called dupcat(), which
automatically suffixes the null-pointer terminating argument.
This has three benefits. Firstly, it's just less effort at every call
site. Secondly, it protects against the risk of accidentally leaving
off the NULL, causing arbitrary words of stack memory to be
dereferenced as char pointers. And thirdly, it protects against the
more subtle risk of writing a bare 'NULL' as the terminating argument,
instead of casting it explicitly to a pointer. That last one is
necessary because C permits the macro NULL to expand to an integer
constant such as 0, so NULL by itself may not have pointer type, and
worse, it may not be marshalled in a variadic argument list in the
same way as a pointer. (For example, on a 64-bit machine it might only
occupy 32 bits. And yet, on another 64-bit platform, it might work
just fine, so that you don't notice the mistake!)
I was inspired to do this by happening to notice one of those bare
NULL terminators, and thinking I'd better check if there were any
more. Turned out there were quite a few. Now there are none.
2019-10-14 18:42:37 +00:00
|
|
|
char *suffname = dupcat(name, "Name");
|
2011-10-01 17:38:59 +00:00
|
|
|
write_setting_s(handle, suffname, fs->name);
|
In the new unified font handling, my strategy so far for combining
client- and server-side fonts into a single namespace was mainly to
hope there would naturally be no collisions, and to provide
disambiguating "client:" and "server:" prefixes for manual use in
emergencies.
Jacob points out, however, that his system not only has a namespace
clash but worse still the clash is at the name "fixed", which is our
default font! So, modify my namespace policy to use the
disambiguating prefixes everywhere by default, and use _unprefixed_
names only if the user types one in by hand.
In particular, I've changed the keys used to store font names in
Unix saved session files. Font names read from the new keys will be
passed straight to the new unifont framework; font names read from
the old keys will have "server:" prepended. So any existing
configuration file for GTK1 PuTTY should now work reliably in GTK2
PuTTY and select the same font, even if that font is one on which
your system (rather, your client+server combination) has a font
namespace clash.
[originally from svn r7973]
2008-04-05 13:37:20 +00:00
|
|
|
sfree(suffname);
|
2003-02-01 12:54:40 +00:00
|
|
|
}
|
2018-09-14 07:45:42 +00:00
|
|
|
void write_setting_filename(settings_w *handle,
|
|
|
|
const char *name, Filename *result)
|
2003-02-01 12:54:40 +00:00
|
|
|
{
|
2011-10-02 11:01:57 +00:00
|
|
|
write_setting_s(handle, name, result->path);
|
2003-02-01 12:54:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
void close_settings_r(settings_r *handle)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2010-05-19 18:22:17 +00:00
|
|
|
struct skeyval *kv;
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
if (!handle)
|
2003-03-31 11:36:14 +00:00
|
|
|
return;
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
while ( (kv = index234(handle->t, 0)) != NULL) {
|
|
|
|
del234(handle->t, kv);
|
2003-03-31 11:36:14 +00:00
|
|
|
sfree((char *)kv->key);
|
|
|
|
sfree((char *)kv->value);
|
|
|
|
sfree(kv);
|
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
freetree234(handle->t);
|
|
|
|
sfree(handle);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
void del_settings(const char *sessionname)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2008-03-22 12:01:16 +00:00
|
|
|
char *filename;
|
|
|
|
filename = make_filename(INDEX_SESSION, sessionname);
|
2003-03-31 11:36:14 +00:00
|
|
|
unlink(filename);
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(filename);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
struct settings_e {
|
|
|
|
DIR *dp;
|
|
|
|
};
|
|
|
|
|
|
|
|
settings_e *enum_settings_start(void)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2003-03-31 11:36:14 +00:00
|
|
|
DIR *dp;
|
2008-03-22 12:01:16 +00:00
|
|
|
char *filename;
|
2002-10-09 18:09:42 +00:00
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
filename = make_filename(INDEX_SESSIONDIR, NULL);
|
2003-03-31 11:36:14 +00:00
|
|
|
dp = opendir(filename);
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(filename);
|
2002-10-09 18:09:42 +00:00
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
settings_e *toret = snew(settings_e);
|
|
|
|
toret->dp = dp;
|
|
|
|
return toret;
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
bool enum_settings_next(settings_e *handle, strbuf *out)
|
2002-10-31 19:49:52 +00:00
|
|
|
{
|
2003-03-31 11:36:14 +00:00
|
|
|
struct dirent *de;
|
|
|
|
struct stat st;
|
2019-02-11 06:58:07 +00:00
|
|
|
strbuf *fullpath;
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2018-10-07 13:04:26 +00:00
|
|
|
if (!handle->dp)
|
|
|
|
return NULL;
|
|
|
|
|
2019-02-11 06:58:07 +00:00
|
|
|
fullpath = strbuf_new();
|
|
|
|
|
|
|
|
char *sessiondir = make_filename(INDEX_SESSIONDIR, NULL);
|
|
|
|
put_datapl(fullpath, ptrlen_from_asciz(sessiondir));
|
|
|
|
sfree(sessiondir);
|
|
|
|
put_byte(fullpath, '/');
|
|
|
|
|
|
|
|
size_t baselen = fullpath->len;
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
while ( (de = readdir(handle->dp)) != NULL ) {
|
2020-01-21 20:16:28 +00:00
|
|
|
strbuf_shrink_to(fullpath, baselen);
|
2019-09-08 19:29:00 +00:00
|
|
|
put_datapl(fullpath, ptrlen_from_asciz(de->d_name));
|
2003-03-31 11:36:14 +00:00
|
|
|
|
2019-02-11 06:58:07 +00:00
|
|
|
if (stat(fullpath->s, &st) < 0 || !S_ISREG(st.st_mode))
|
2003-03-31 11:36:14 +00:00
|
|
|
continue; /* try another one */
|
|
|
|
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
decode_session_filename(de->d_name, out);
|
2019-09-08 19:29:00 +00:00
|
|
|
strbuf_free(fullpath);
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
return true;
|
2003-03-31 11:36:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 06:58:07 +00:00
|
|
|
strbuf_free(fullpath);
|
Rework mungestr() and unmungestr().
For a start, they now have different names on Windows and Unix,
reflecting their different roles: on Windows they apply escaping to
any string that's going to be used as a registry key (be it a session
name, or a host name for host key storage), whereas on Unix they're
for constructing saved-session file names in particular (and also
handle the special case of filling in "Default Settings" for NULL).
Also, they now produce output by writing to a strbuf, which simplifies
a lot of the call sites. In particular, the strbuf output idiom is
passed on to enum_settings_next, which is especially nice because its
only actual caller was doing an ad-hoc realloc loop that I can now get
rid of completely.
Thirdly, on Windows they're centralised into winmisc.c instead of
living in winstore.c, because that way Pageant can use the unescape
function too. (It was spotting the duplication there that made me
think of doing this in the first place, but once I'd started, I had to
keep unravelling the thread...)
2018-11-03 08:58:41 +00:00
|
|
|
return false;
|
2002-10-31 19:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 07:45:42 +00:00
|
|
|
void enum_settings_finish(settings_e *handle)
|
2002-10-31 19:49:52 +00:00
|
|
|
{
|
2018-10-07 13:04:26 +00:00
|
|
|
if (handle->dp)
|
|
|
|
closedir(handle->dp);
|
2018-09-14 07:45:42 +00:00
|
|
|
sfree(handle);
|
2002-10-31 19:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lines in the host keys file are of the form
|
2019-09-08 19:29:00 +00:00
|
|
|
*
|
2002-10-31 19:49:52 +00:00
|
|
|
* type@port:hostname keydata
|
2019-09-08 19:29:00 +00:00
|
|
|
*
|
2002-10-31 19:49:52 +00:00
|
|
|
* e.g.
|
2019-09-08 19:29:00 +00:00
|
|
|
*
|
2002-10-31 19:49:52 +00:00
|
|
|
* rsa@22:foovax.example.org 0x23,0x293487364395345345....2343
|
|
|
|
*/
|
2003-01-14 18:43:45 +00:00
|
|
|
int verify_host_key(const char *hostname, int port,
|
2019-09-08 19:29:00 +00:00
|
|
|
const char *keytype, const char *key)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2002-10-31 19:49:52 +00:00
|
|
|
FILE *fp;
|
2008-03-22 12:01:16 +00:00
|
|
|
char *filename;
|
2002-10-31 19:49:52 +00:00
|
|
|
char *line;
|
|
|
|
int ret;
|
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
filename = make_filename(INDEX_HOSTKEYS, NULL);
|
2002-10-31 19:49:52 +00:00
|
|
|
fp = fopen(filename, "r");
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(filename);
|
2002-10-31 19:49:52 +00:00
|
|
|
if (!fp)
|
2019-09-08 19:29:00 +00:00
|
|
|
return 1; /* key does not exist */
|
2002-10-31 19:49:52 +00:00
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
while ( (line = fgetline(fp)) ) {
|
2019-09-08 19:29:00 +00:00
|
|
|
int i;
|
|
|
|
char *p = line;
|
|
|
|
char porttext[20];
|
|
|
|
|
|
|
|
line[strcspn(line, "\n")] = '\0'; /* strip trailing newline */
|
|
|
|
|
|
|
|
i = strlen(keytype);
|
|
|
|
if (strncmp(p, keytype, i))
|
|
|
|
goto done;
|
|
|
|
p += i;
|
|
|
|
|
|
|
|
if (*p != '@')
|
|
|
|
goto done;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
sprintf(porttext, "%d", port);
|
|
|
|
i = strlen(porttext);
|
|
|
|
if (strncmp(p, porttext, i))
|
|
|
|
goto done;
|
|
|
|
p += i;
|
|
|
|
|
|
|
|
if (*p != ':')
|
|
|
|
goto done;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
i = strlen(hostname);
|
|
|
|
if (strncmp(p, hostname, i))
|
|
|
|
goto done;
|
|
|
|
p += i;
|
|
|
|
|
|
|
|
if (*p != ' ')
|
|
|
|
goto done;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Found the key. Now just work out whether it's the right
|
|
|
|
* one or not.
|
|
|
|
*/
|
|
|
|
if (!strcmp(p, key))
|
|
|
|
ret = 0; /* key matched OK */
|
|
|
|
else
|
|
|
|
ret = 2; /* key mismatch */
|
|
|
|
|
|
|
|
done:
|
|
|
|
sfree(line);
|
|
|
|
if (ret != 1)
|
|
|
|
break;
|
2002-10-31 19:49:52 +00:00
|
|
|
}
|
|
|
|
|
2003-10-31 21:45:15 +00:00
|
|
|
fclose(fp);
|
2002-10-31 19:49:52 +00:00
|
|
|
return ret;
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
bool have_ssh_host_key(const char *hostname, int port,
|
|
|
|
const char *keytype)
|
2015-05-29 21:40:50 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we have a host key, verify_host_key will return 0 or 2.
|
|
|
|
* If we don't have one, it'll return 1.
|
|
|
|
*/
|
|
|
|
return verify_host_key(hostname, port, keytype, "") != 1;
|
|
|
|
}
|
|
|
|
|
2003-01-14 18:43:45 +00:00
|
|
|
void store_host_key(const char *hostname, int port,
|
2019-09-08 19:29:00 +00:00
|
|
|
const char *keytype, const char *key)
|
2002-10-09 18:09:42 +00:00
|
|
|
{
|
2004-01-17 13:00:18 +00:00
|
|
|
FILE *rfp, *wfp;
|
|
|
|
char *newtext, *line;
|
|
|
|
int headerlen;
|
2008-03-22 12:01:16 +00:00
|
|
|
char *filename, *tmpfilename;
|
2002-10-31 19:49:52 +00:00
|
|
|
|
2004-01-17 13:00:18 +00:00
|
|
|
/*
|
|
|
|
* Open both the old file and a new file.
|
|
|
|
*/
|
2008-03-22 12:01:16 +00:00
|
|
|
tmpfilename = make_filename(INDEX_HOSTKEYS_TMP, NULL);
|
2004-01-17 13:00:18 +00:00
|
|
|
wfp = fopen(tmpfilename, "w");
|
2013-07-19 17:44:38 +00:00
|
|
|
if (!wfp && errno == ENOENT) {
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
char *dir, *errmsg;
|
2004-01-19 09:37:17 +00:00
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
dir = make_filename(INDEX_DIR, NULL);
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
if ((errmsg = make_dir_path(dir, 0700)) != NULL) {
|
|
|
|
nonfatal("Unable to store host key: %s", errmsg);
|
|
|
|
sfree(errmsg);
|
2013-07-19 17:44:38 +00:00
|
|
|
sfree(dir);
|
|
|
|
sfree(tmpfilename);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(dir);
|
2008-03-22 12:01:16 +00:00
|
|
|
|
2004-01-19 09:37:17 +00:00
|
|
|
wfp = fopen(tmpfilename, "w");
|
2002-10-31 19:49:52 +00:00
|
|
|
}
|
2008-03-22 12:01:16 +00:00
|
|
|
if (!wfp) {
|
2015-05-01 13:54:51 +00:00
|
|
|
nonfatal("Unable to store host key: open(\"%s\") "
|
|
|
|
"returned '%s'", tmpfilename, strerror(errno));
|
2013-07-19 17:44:38 +00:00
|
|
|
sfree(tmpfilename);
|
|
|
|
return;
|
2008-03-22 12:01:16 +00:00
|
|
|
}
|
|
|
|
filename = make_filename(INDEX_HOSTKEYS, NULL);
|
2004-01-19 09:37:17 +00:00
|
|
|
rfp = fopen(filename, "r");
|
2004-01-17 13:00:18 +00:00
|
|
|
|
2013-07-14 10:46:07 +00:00
|
|
|
newtext = dupprintf("%s@%d:%s %s\n", keytype, port, hostname, key);
|
|
|
|
headerlen = 1 + strcspn(newtext, " "); /* count the space too */
|
|
|
|
|
2004-01-17 13:00:18 +00:00
|
|
|
/*
|
|
|
|
* Copy all lines from the old file to the new one that _don't_
|
|
|
|
* involve the same host key identifier as the one we're adding.
|
|
|
|
*/
|
2004-01-19 09:37:17 +00:00
|
|
|
if (rfp) {
|
|
|
|
while ( (line = fgetline(rfp)) ) {
|
|
|
|
if (strncmp(line, newtext, headerlen))
|
|
|
|
fputs(line, wfp);
|
2013-07-14 10:46:07 +00:00
|
|
|
sfree(line);
|
2004-01-19 09:37:17 +00:00
|
|
|
}
|
|
|
|
fclose(rfp);
|
2002-10-31 19:49:52 +00:00
|
|
|
}
|
2004-01-17 13:00:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now add the new line at the end.
|
|
|
|
*/
|
|
|
|
fputs(newtext, wfp);
|
|
|
|
|
|
|
|
fclose(wfp);
|
|
|
|
|
2013-07-21 07:40:30 +00:00
|
|
|
if (rename(tmpfilename, filename) < 0) {
|
2015-05-01 13:54:51 +00:00
|
|
|
nonfatal("Unable to store host key: rename(\"%s\",\"%s\")"
|
|
|
|
" returned '%s'", tmpfilename, filename,
|
|
|
|
strerror(errno));
|
2013-07-21 07:40:30 +00:00
|
|
|
}
|
2004-01-17 13:00:18 +00:00
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(tmpfilename);
|
|
|
|
sfree(filename);
|
2004-01-17 13:00:18 +00:00
|
|
|
sfree(newtext);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_random_seed(noise_consumer_t consumer)
|
|
|
|
{
|
2002-11-02 15:23:20 +00:00
|
|
|
int fd;
|
2008-03-22 12:01:16 +00:00
|
|
|
char *fname;
|
2002-11-02 15:23:20 +00:00
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
fname = make_filename(INDEX_RANDSEED, NULL);
|
2002-11-02 15:23:20 +00:00
|
|
|
fd = open(fname, O_RDONLY);
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(fname);
|
2009-04-26 22:32:41 +00:00
|
|
|
if (fd >= 0) {
|
2019-09-08 19:29:00 +00:00
|
|
|
char buf[512];
|
|
|
|
int ret;
|
|
|
|
while ( (ret = read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
consumer(buf, ret);
|
|
|
|
close(fd);
|
2002-11-02 15:23:20 +00:00
|
|
|
}
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void write_random_seed(void *data, int len)
|
|
|
|
{
|
2002-11-02 15:23:20 +00:00
|
|
|
int fd;
|
2008-03-22 12:01:16 +00:00
|
|
|
char *fname;
|
2002-11-02 15:23:20 +00:00
|
|
|
|
2008-03-22 12:01:16 +00:00
|
|
|
fname = make_filename(INDEX_RANDSEED, NULL);
|
2002-11-07 20:01:04 +00:00
|
|
|
/*
|
|
|
|
* Don't truncate the random seed file if it already exists; if
|
|
|
|
* something goes wrong half way through writing it, it would
|
|
|
|
* be better to leave the old data there than to leave it empty.
|
|
|
|
*/
|
|
|
|
fd = open(fname, O_CREAT | O_WRONLY, 0600);
|
2002-11-02 15:23:20 +00:00
|
|
|
if (fd < 0) {
|
2013-07-19 17:44:33 +00:00
|
|
|
if (errno != ENOENT) {
|
2015-05-01 13:54:51 +00:00
|
|
|
nonfatal("Unable to write random seed: open(\"%s\") "
|
|
|
|
"returned '%s'", fname, strerror(errno));
|
2013-07-20 13:15:16 +00:00
|
|
|
sfree(fname);
|
2013-07-19 17:44:33 +00:00
|
|
|
return;
|
|
|
|
}
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
char *dir, *errmsg;
|
2002-11-02 15:23:20 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
dir = make_filename(INDEX_DIR, NULL);
|
Added support for the XDG specification
The XDG configuration location ($XDG_CONFIG_HOME/putty, or
~/.config/putty) is now prefered over the old ~/.putty location, if the
XDG location already exists. If it doesn't exist, we try to use one of
the old locations ($HOME/.putty, [/etc/passwd home]/.putty, /.putty). If
none of the directories exist, we fall back to ~/.config/putty or
~/.putty, if the XDG_DEFAULT macro is defined or not, respectively. The
PUTTYDIR environment variable remains a definitive override of the
configuration location. This all ensures that the old location is still
used, unless the user explicitly requests otherwise.
The configuration directories are created using the make_dir_path()
function, to ensure that saving the configuration doesn't fail e.g.
because of a non-existent ~/.config directory.
2016-08-02 13:12:43 +00:00
|
|
|
if ((errmsg = make_dir_path(dir, 0700)) != NULL) {
|
|
|
|
nonfatal("Unable to write random seed: %s", errmsg);
|
|
|
|
sfree(errmsg);
|
2013-07-20 13:15:16 +00:00
|
|
|
sfree(fname);
|
2013-07-19 17:44:33 +00:00
|
|
|
sfree(dir);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(dir);
|
2008-03-22 12:01:16 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
fd = open(fname, O_CREAT | O_WRONLY, 0600);
|
2013-07-21 07:40:30 +00:00
|
|
|
if (fd < 0) {
|
2015-05-01 13:54:51 +00:00
|
|
|
nonfatal("Unable to write random seed: open(\"%s\") "
|
|
|
|
"returned '%s'", fname, strerror(errno));
|
2013-07-20 13:15:16 +00:00
|
|
|
sfree(fname);
|
2013-07-19 17:44:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-11-02 15:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (len > 0) {
|
2019-09-08 19:29:00 +00:00
|
|
|
int ret = write(fd, data, len);
|
|
|
|
if (ret < 0) {
|
2015-05-01 13:54:51 +00:00
|
|
|
nonfatal("Unable to write random seed: write "
|
|
|
|
"returned '%s'", strerror(errno));
|
2013-07-19 17:44:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-09-08 19:29:00 +00:00
|
|
|
len -= ret;
|
|
|
|
data = (char *)data + len;
|
2002-11-02 15:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2008-03-22 12:01:16 +00:00
|
|
|
sfree(fname);
|
2002-10-09 18:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_all(void)
|
|
|
|
{
|
|
|
|
}
|