2003-09-02 09:00:35 +00:00
|
|
|
/*
|
|
|
|
* uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2003-09-02 09:52:13 +00:00
|
|
|
#include <stdlib.h>
|
2003-09-02 09:00:35 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
2008-11-17 18:38:09 +00:00
|
|
|
#include "ssh.h"
|
2003-09-02 09:00:35 +00:00
|
|
|
#include "psftp.h"
|
|
|
|
|
2019-03-26 19:09:35 +00:00
|
|
|
#if HAVE_GLOB_H
|
|
|
|
#include <glob.h>
|
|
|
|
#endif
|
|
|
|
|
2003-09-02 09:00:35 +00:00
|
|
|
char *x_get_default(const char *key)
|
|
|
|
{
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL; /* this is a stub */
|
2003-09-02 09:00:35 +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
|
|
|
void platform_get_x11_auth(struct X11Display *display, Conf *conf)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
/* Do nothing, therefore no auth. */
|
|
|
|
}
|
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
|
|
|
const bool platform_uses_x11_unix_by_default = true;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Default settings that are specific to PSFTP.
|
|
|
|
*/
|
|
|
|
char *platform_default_s(const char *name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:57:31 +00:00
|
|
|
bool platform_default_b(const char *name, bool def)
|
|
|
|
{
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2003-09-02 09:00:35 +00:00
|
|
|
int platform_default_i(const char *name, int def)
|
|
|
|
{
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2011-10-01 17:38:59 +00:00
|
|
|
FontSpec *platform_default_fontspec(const char *name)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
2011-10-01 17:38:59 +00:00
|
|
|
return fontspec_new("");
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
2011-10-02 11:01:57 +00:00
|
|
|
Filename *platform_default_filename(const char *name)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
if (!strcmp(name, "LogFileName"))
|
2019-09-08 19:29:00 +00:00
|
|
|
return filename_from_str("putty.log");
|
2003-09-02 09:00:35 +00:00
|
|
|
else
|
2019-09-08 19:29:00 +00:00
|
|
|
return filename_from_str("");
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
New abstraction 'Seat', to pass to backends.
This is a new vtable-based abstraction which is passed to a backend in
place of Frontend, and it implements only the subset of the Frontend
functions needed by a backend. (Many other Frontend functions still
exist, notably the wide range of things called by terminal.c providing
platform-independent operations on the GUI terminal window.)
The purpose of making it a vtable is that this opens up the
possibility of creating a backend as an internal implementation detail
of some other activity, by providing just that one backend with a
custom Seat that implements the methods differently.
For example, this refactoring should make it feasible to directly
implement an SSH proxy type, aka the 'jump host' feature supported by
OpenSSH, aka 'open a secondary SSH session in MAINCHAN_DIRECT_TCP
mode, and then expose the main channel of that as the Socket for the
primary connection'. (Which of course you can already do by spawning
'plink -nc' as a separate proxy process, but this would permit it in
the _same_ process without anything getting confused.)
I've centralised a full set of stub methods in misc.c for the new
abstraction, which allows me to get rid of several annoying stubs in
the previous code. Also, while I'm here, I've moved a lot of
duplicated modalfatalbox() type functions from application main
program files into wincons.c / uxcons.c, which I think saves
duplication overall. (A minor visible effect is that the prefixes on
those console-based fatal error messages will now be more consistent
between applications.)
2018-10-11 18:58:42 +00:00
|
|
|
int filexfer_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input)
|
2005-10-30 20:24:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2018-05-18 06:22:56 +00:00
|
|
|
ret = cmdline_get_passwd_input(p);
|
2005-10-30 20:24:09 +00:00
|
|
|
if (ret == -1)
|
2019-09-08 19:29:00 +00:00
|
|
|
ret = console_get_userpass_input(p);
|
2005-10-30 20:24:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-09-02 09:00:35 +00:00
|
|
|
/*
|
|
|
|
* Set local current directory. Returns NULL on success, or else an
|
|
|
|
* error message which must be freed after printing.
|
|
|
|
*/
|
|
|
|
char *psftp_lcd(char *dir)
|
|
|
|
{
|
|
|
|
if (chdir(dir) < 0)
|
2019-09-08 19:29:00 +00:00
|
|
|
return dupprintf("%s: chdir: %s", dir, strerror(errno));
|
2003-09-02 09:00:35 +00:00
|
|
|
else
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get local current directory. Returns a string which must be
|
|
|
|
* freed.
|
|
|
|
*/
|
|
|
|
char *psftp_getcwd(void)
|
|
|
|
{
|
|
|
|
char *buffer, *ret;
|
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the
lines of
if (logical_array_len >= physical_array_size) {
physical_array_size = logical_array_len * 5 / 4 + 256;
array = sresize(array, physical_array_size, ElementType);
}
which happens at a zillion call sites throughout this code base, with
different random choices of the geometric factor and additive
constant, sometimes forgetting them completely, and generally doing a
lot of repeated work.
The new macro sgrowarray(array,size,n) has the semantics: here are the
array pointer and its physical size for you to modify, now please
ensure that the nth element exists, so I can write into it. And
sgrowarrayn(array,size,n,m) is the same except that it ensures that
the array has size at least n+m (so sgrowarray is just the special
case where m=1).
Now that this is a single centralised implementation that will be used
everywhere, I've also gone to more effort in the implementation, with
careful overflow checks that would have been painful to put at all the
previous call sites.
This commit also switches over every use of sresize(), apart from a
few where I really didn't think it would gain anything. A consequence
of that is that a lot of array-size variables have to have their types
changed to size_t, because the macros require that (they address-take
the size to pass to the underlying function).
2019-02-28 20:07:30 +00:00
|
|
|
size_t size = 256;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
buffer = snewn(size, char);
|
|
|
|
while (1) {
|
2019-09-08 19:29:00 +00:00
|
|
|
ret = getcwd(buffer, size);
|
|
|
|
if (ret != NULL)
|
|
|
|
return ret;
|
|
|
|
if (errno != ERANGE) {
|
|
|
|
sfree(buffer);
|
|
|
|
return dupprintf("[cwd unavailable: %s]", strerror(errno));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Otherwise, ERANGE was returned, meaning the buffer
|
|
|
|
* wasn't big enough.
|
|
|
|
*/
|
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the
lines of
if (logical_array_len >= physical_array_size) {
physical_array_size = logical_array_len * 5 / 4 + 256;
array = sresize(array, physical_array_size, ElementType);
}
which happens at a zillion call sites throughout this code base, with
different random choices of the geometric factor and additive
constant, sometimes forgetting them completely, and generally doing a
lot of repeated work.
The new macro sgrowarray(array,size,n) has the semantics: here are the
array pointer and its physical size for you to modify, now please
ensure that the nth element exists, so I can write into it. And
sgrowarrayn(array,size,n,m) is the same except that it ensures that
the array has size at least n+m (so sgrowarray is just the special
case where m=1).
Now that this is a single centralised implementation that will be used
everywhere, I've also gone to more effort in the implementation, with
careful overflow checks that would have been painful to put at all the
previous call sites.
This commit also switches over every use of sresize(), apart from a
few where I really didn't think it would gain anything. A consequence
of that is that a lot of array-size variables have to have their types
changed to size_t, because the macros require that (they address-take
the size to pass to the underlying function).
2019-02-28 20:07:30 +00:00
|
|
|
sgrowarray(buffer, size, size);
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RFile {
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2018-10-26 22:08:58 +00:00
|
|
|
RFile *open_existing_file(const char *name, uint64_t *size,
|
2019-09-08 19:29:00 +00:00
|
|
|
unsigned long *mtime, unsigned long *atime,
|
2011-08-11 17:59:30 +00:00
|
|
|
long *perms)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
RFile *ret;
|
|
|
|
|
|
|
|
fd = open(name, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
ret = snew(RFile);
|
|
|
|
ret->fd = fd;
|
|
|
|
|
2011-08-11 17:59:30 +00:00
|
|
|
if (size || mtime || atime || perms) {
|
2019-09-08 19:29:00 +00:00
|
|
|
struct stat statbuf;
|
|
|
|
if (fstat(fd, &statbuf) < 0) {
|
|
|
|
fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
|
|
|
|
memset(&statbuf, 0, sizeof(statbuf));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
*size = statbuf.st_size;
|
|
|
|
|
|
|
|
if (mtime)
|
|
|
|
*mtime = statbuf.st_mtime;
|
|
|
|
|
|
|
|
if (atime)
|
|
|
|
*atime = statbuf.st_atime;
|
|
|
|
|
|
|
|
if (perms)
|
|
|
|
*perms = statbuf.st_mode;
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_from_file(RFile *f, void *buffer, int length)
|
|
|
|
{
|
|
|
|
return read(f->fd, buffer, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_rfile(RFile *f)
|
|
|
|
{
|
|
|
|
close(f->fd);
|
|
|
|
sfree(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WFile {
|
|
|
|
int fd;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
2015-05-15 10:15:42 +00:00
|
|
|
WFile *open_new_file(const char *name, long perms)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
WFile *ret;
|
|
|
|
|
2011-08-11 17:59:30 +00:00
|
|
|
fd = open(name, O_CREAT | O_TRUNC | O_WRONLY,
|
|
|
|
(mode_t)(perms ? perms : 0666));
|
2003-09-02 09:00:35 +00:00
|
|
|
if (fd < 0)
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
ret = snew(WFile);
|
|
|
|
ret->fd = fd;
|
|
|
|
ret->name = dupstr(name);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-08-12 15:20:19 +00:00
|
|
|
|
2018-10-26 22:08:58 +00:00
|
|
|
WFile *open_existing_wfile(const char *name, uint64_t *size)
|
2006-08-12 15:20:19 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
WFile *ret;
|
|
|
|
|
|
|
|
fd = open(name, O_APPEND | O_WRONLY);
|
|
|
|
if (fd < 0)
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
2006-08-12 15:20:19 +00:00
|
|
|
|
|
|
|
ret = snew(WFile);
|
|
|
|
ret->fd = fd;
|
2009-04-26 22:19:30 +00:00
|
|
|
ret->name = dupstr(name);
|
2006-08-12 15:20:19 +00:00
|
|
|
|
|
|
|
if (size) {
|
2019-09-08 19:29:00 +00:00
|
|
|
struct stat statbuf;
|
|
|
|
if (fstat(fd, &statbuf) < 0) {
|
|
|
|
fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
|
|
|
|
memset(&statbuf, 0, sizeof(statbuf));
|
|
|
|
}
|
2006-08-12 15:20:19 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
*size = statbuf.st_size;
|
2006-08-12 15:20:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-09-02 09:00:35 +00:00
|
|
|
int write_to_file(WFile *f, void *buffer, int length)
|
|
|
|
{
|
|
|
|
char *p = (char *)buffer;
|
|
|
|
int so_far = 0;
|
|
|
|
|
|
|
|
/* Keep trying until we've really written as much as we can. */
|
|
|
|
while (length > 0) {
|
2019-09-08 19:29:00 +00:00
|
|
|
int ret = write(f->fd, p, length);
|
2003-09-02 09:00:35 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
if (ret == 0)
|
|
|
|
break;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
p += ret;
|
|
|
|
length -= ret;
|
|
|
|
so_far += ret;
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return so_far;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
|
|
|
|
{
|
|
|
|
struct utimbuf ut;
|
|
|
|
|
|
|
|
ut.actime = atime;
|
|
|
|
ut.modtime = mtime;
|
|
|
|
|
|
|
|
utime(f->name, &ut);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Closes and frees the WFile */
|
|
|
|
void close_wfile(WFile *f)
|
|
|
|
{
|
|
|
|
close(f->fd);
|
|
|
|
sfree(f->name);
|
|
|
|
sfree(f);
|
|
|
|
}
|
|
|
|
|
2006-08-12 15:20:19 +00:00
|
|
|
/* Seek offset bytes through file, from whence, where whence is
|
|
|
|
FROM_START, FROM_CURRENT, or FROM_END */
|
2018-10-26 22:08:58 +00:00
|
|
|
int seek_file(WFile *f, uint64_t offset, int whence)
|
2006-08-12 15:20:19 +00:00
|
|
|
{
|
|
|
|
int lseek_whence;
|
2019-09-08 19:29:00 +00:00
|
|
|
|
2006-08-12 15:20:19 +00:00
|
|
|
switch (whence) {
|
|
|
|
case FROM_START:
|
2019-09-08 19:29:00 +00:00
|
|
|
lseek_whence = SEEK_SET;
|
|
|
|
break;
|
2006-08-12 15:20:19 +00:00
|
|
|
case FROM_CURRENT:
|
2019-09-08 19:29:00 +00:00
|
|
|
lseek_whence = SEEK_CUR;
|
|
|
|
break;
|
2006-08-12 15:20:19 +00:00
|
|
|
case FROM_END:
|
2019-09-08 19:29:00 +00:00
|
|
|
lseek_whence = SEEK_END;
|
|
|
|
break;
|
2006-08-12 15:20:19 +00:00
|
|
|
default:
|
2019-09-08 19:29:00 +00:00
|
|
|
return -1;
|
2006-08-12 15:20:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 22:08:58 +00:00
|
|
|
return lseek(f->fd, offset, lseek_whence) >= 0 ? 0 : -1;
|
2006-08-12 15:20:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 22:08:58 +00:00
|
|
|
uint64_t get_file_posn(WFile *f)
|
2006-08-12 15:20:19 +00:00
|
|
|
{
|
2018-10-26 22:08:58 +00:00
|
|
|
return lseek(f->fd, (off_t) 0, SEEK_CUR);
|
2006-08-12 15:20:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 10:15:42 +00:00
|
|
|
int file_type(const char *name)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
if (stat(name, &statbuf) < 0) {
|
2019-09-08 19:29:00 +00:00
|
|
|
if (errno != ENOENT)
|
|
|
|
fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
|
|
|
|
return FILE_TYPE_NONEXISTENT;
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISREG(statbuf.st_mode))
|
2019-09-08 19:29:00 +00:00
|
|
|
return FILE_TYPE_FILE;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
if (S_ISDIR(statbuf.st_mode))
|
2019-09-08 19:29:00 +00:00
|
|
|
return FILE_TYPE_DIRECTORY;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
return FILE_TYPE_WEIRD;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DirHandle {
|
|
|
|
DIR *dir;
|
|
|
|
};
|
|
|
|
|
2018-12-27 16:52:23 +00:00
|
|
|
DirHandle *open_directory(const char *name, const char **errmsg)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
DirHandle *ret;
|
|
|
|
|
|
|
|
dir = opendir(name);
|
2018-12-27 16:52:23 +00:00
|
|
|
if (!dir) {
|
|
|
|
*errmsg = strerror(errno);
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
2018-12-27 16:52:23 +00:00
|
|
|
}
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
ret = snew(DirHandle);
|
|
|
|
ret->dir = dir;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *read_filename(DirHandle *dir)
|
|
|
|
{
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
do {
|
2019-09-08 19:29:00 +00:00
|
|
|
de = readdir(dir->dir);
|
|
|
|
if (de == NULL)
|
|
|
|
return NULL;
|
2003-09-02 09:00:35 +00:00
|
|
|
} while ((de->d_name[0] == '.' &&
|
2019-09-08 19:29:00 +00:00
|
|
|
(de->d_name[1] == '\0' ||
|
|
|
|
(de->d_name[1] == '.' && de->d_name[2] == '\0'))));
|
2003-09-02 09:00:35 +00:00
|
|
|
|
|
|
|
return dupstr(de->d_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_directory(DirHandle *dir)
|
|
|
|
{
|
|
|
|
closedir(dir->dir);
|
|
|
|
sfree(dir);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
int test_wildcard(const char *name, bool cmdline)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
|
2004-12-16 17:35:20 +00:00
|
|
|
if (stat(name, &statbuf) == 0) {
|
2019-09-08 19:29:00 +00:00
|
|
|
return WCTYPE_FILENAME;
|
2004-12-16 17:35:20 +00:00
|
|
|
} else if (cmdline) {
|
2019-09-08 19:29:00 +00:00
|
|
|
/*
|
|
|
|
* On Unix, we never need to parse wildcards coming from
|
|
|
|
* the command line, because the shell will have expanded
|
|
|
|
* them into a filename list already.
|
|
|
|
*/
|
|
|
|
return WCTYPE_NONEXISTENT;
|
2004-12-16 17:35:20 +00:00
|
|
|
} else {
|
2019-03-26 19:09:35 +00:00
|
|
|
#if HAVE_GLOB_H
|
2019-09-08 19:29:00 +00:00
|
|
|
glob_t globbed;
|
|
|
|
int ret = WCTYPE_NONEXISTENT;
|
2004-12-16 17:35:20 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
|
|
|
|
if (globbed.gl_pathc > 0)
|
|
|
|
ret = WCTYPE_WILDCARD;
|
|
|
|
globfree(&globbed);
|
|
|
|
}
|
2004-12-16 17:35:20 +00:00
|
|
|
|
2019-09-08 19:29:00 +00:00
|
|
|
return ret;
|
2019-03-26 19:09:35 +00:00
|
|
|
#else
|
|
|
|
/* On a system without glob.h, we just have to return a
|
|
|
|
* failure code */
|
|
|
|
return WCTYPE_NONEXISTENT;
|
|
|
|
#endif
|
2004-12-16 17:35:20 +00:00
|
|
|
}
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-12-16 17:35:20 +00:00
|
|
|
* Actually return matching file names for a local wildcard.
|
2003-09-02 09:00:35 +00:00
|
|
|
*/
|
2019-03-26 19:09:35 +00:00
|
|
|
#if HAVE_GLOB_H
|
2003-09-02 09:00:35 +00:00
|
|
|
struct WildcardMatcher {
|
2004-12-16 17:35:20 +00:00
|
|
|
glob_t globbed;
|
|
|
|
int i;
|
2003-09-02 09:00:35 +00:00
|
|
|
};
|
2015-05-15 10:15:42 +00:00
|
|
|
WildcardMatcher *begin_wildcard_matching(const char *name) {
|
2004-12-16 17:35:20 +00:00
|
|
|
WildcardMatcher *ret = snew(WildcardMatcher);
|
|
|
|
|
|
|
|
if (glob(name, 0, NULL, &ret->globbed) < 0) {
|
2019-09-08 19:29:00 +00:00
|
|
|
sfree(ret);
|
|
|
|
return NULL;
|
2004-12-16 17:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret->i = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
char *wildcard_get_filename(WildcardMatcher *dir) {
|
|
|
|
if (dir->i < dir->globbed.gl_pathc) {
|
2019-09-08 19:29:00 +00:00
|
|
|
return dupstr(dir->globbed.gl_pathv[dir->i++]);
|
2004-12-16 17:35:20 +00:00
|
|
|
} else
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
2004-12-16 17:35:20 +00:00
|
|
|
}
|
|
|
|
void finish_wildcard_matching(WildcardMatcher *dir) {
|
|
|
|
globfree(&dir->globbed);
|
|
|
|
sfree(dir);
|
|
|
|
}
|
2019-03-26 19:09:35 +00:00
|
|
|
#else
|
|
|
|
WildcardMatcher *begin_wildcard_matching(const char *name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *wildcard_get_filename(WildcardMatcher *dir)
|
|
|
|
{
|
|
|
|
unreachable("Can't construct a valid WildcardMatcher without <glob.h>");
|
|
|
|
}
|
|
|
|
void finish_wildcard_matching(WildcardMatcher *dir)
|
|
|
|
{
|
|
|
|
unreachable("Can't construct a valid WildcardMatcher without <glob.h>");
|
|
|
|
}
|
|
|
|
#endif
|
2003-09-02 09:00:35 +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
|
|
|
char *stripslashes(const char *str, bool local)
|
2015-09-24 16:47:10 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On Unix, we do the same thing regardless of the 'local'
|
|
|
|
* parameter.
|
|
|
|
*/
|
|
|
|
p = strrchr(str, '/');
|
|
|
|
if (p) str = p+1;
|
|
|
|
|
|
|
|
return (char *)str;
|
|
|
|
}
|
|
|
|
|
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 vet_filename(const char *name)
|
2004-12-16 19:36:47 +00:00
|
|
|
{
|
|
|
|
if (strchr(name, '/'))
|
2019-09-08 19:29:00 +00:00
|
|
|
return false;
|
2004-12-16 19:36:47 +00:00
|
|
|
|
|
|
|
if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
|
2019-09-08 19:29:00 +00:00
|
|
|
return false;
|
2004-12-16 19:36:47 +00:00
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
return true;
|
2004-12-16 19:36:47 +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 create_directory(const char *name)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
|
|
|
return mkdir(name, 0777) == 0;
|
|
|
|
}
|
|
|
|
|
2015-05-15 10:15:42 +00:00
|
|
|
char *dir_file_cat(const char *dir, const char *file)
|
2003-09-02 09:00:35 +00:00
|
|
|
{
|
2019-03-09 16:15:51 +00:00
|
|
|
ptrlen dir_pl = ptrlen_from_asciz(dir);
|
|
|
|
return dupcat(
|
|
|
|
dir, ptrlen_endswith(dir_pl, PTRLEN_LITERAL("/"), NULL) ? "" : "/",
|
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
|
|
|
file);
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-11-27 13:20:21 +00:00
|
|
|
* Do a select() between all currently active network fds and
|
2020-02-07 19:14:32 +00:00
|
|
|
* optionally stdin, using cli_main_loop.
|
2003-09-02 09:00:35 +00:00
|
|
|
*/
|
2004-11-27 13:20:21 +00:00
|
|
|
|
2020-02-07 19:14:32 +00:00
|
|
|
struct ssh_sftp_mainloop_ctx {
|
|
|
|
bool include_stdin, no_fds_ok;
|
|
|
|
int toret;
|
|
|
|
};
|
|
|
|
static bool ssh_sftp_pw_setup(void *vctx, pollwrapper *pw)
|
|
|
|
{
|
|
|
|
struct ssh_sftp_mainloop_ctx *ctx = (struct ssh_sftp_mainloop_ctx *)vctx;
|
|
|
|
int fdstate, rwx;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
2020-02-07 19:14:32 +00:00
|
|
|
if (!ctx->no_fds_ok && !toplevel_callback_pending() &&
|
|
|
|
first_fd(&fdstate, &rwx) < 0) {
|
|
|
|
ctx->toret = -1;
|
|
|
|
return false; /* terminate cli_main_loop */
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 19:14:32 +00:00
|
|
|
if (ctx->include_stdin)
|
|
|
|
pollwrap_add_fd_rwx(pw, 0, SELECT_R);
|
2003-09-02 09:00:35 +00:00
|
|
|
|
2020-02-07 19:14:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static void ssh_sftp_pw_check(void *vctx, pollwrapper *pw)
|
|
|
|
{
|
|
|
|
struct ssh_sftp_mainloop_ctx *ctx = (struct ssh_sftp_mainloop_ctx *)vctx;
|
|
|
|
|
|
|
|
if (ctx->include_stdin && pollwrap_check_fd_rwx(pw, 0, SELECT_R))
|
|
|
|
ctx->toret = 1;
|
|
|
|
}
|
|
|
|
static bool ssh_sftp_mainloop_continue(void *vctx, bool found_any_fd,
|
|
|
|
bool ran_any_callback)
|
|
|
|
{
|
|
|
|
struct ssh_sftp_mainloop_ctx *ctx = (struct ssh_sftp_mainloop_ctx *)vctx;
|
|
|
|
if (ctx->toret != 0 || found_any_fd || ran_any_callback)
|
|
|
|
return false; /* finish the loop */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static int ssh_sftp_do_select(bool include_stdin, bool no_fds_ok)
|
|
|
|
{
|
|
|
|
struct ssh_sftp_mainloop_ctx ctx[1];
|
|
|
|
ctx->include_stdin = include_stdin;
|
|
|
|
ctx->no_fds_ok = no_fds_ok;
|
|
|
|
ctx->toret = 0;
|
2003-09-02 09:00:35 +00:00
|
|
|
|
2020-02-07 19:14:32 +00:00
|
|
|
cli_main_loop(ssh_sftp_pw_setup, ssh_sftp_pw_check,
|
|
|
|
ssh_sftp_mainloop_continue, ctx);
|
2013-08-17 16:06:08 +00:00
|
|
|
|
2020-02-07 19:14:32 +00:00
|
|
|
return ctx->toret;
|
2004-11-27 13:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for some network data and process it.
|
|
|
|
*/
|
|
|
|
int ssh_sftp_loop_iteration(void)
|
|
|
|
{
|
2018-10-29 19:50:29 +00:00
|
|
|
return ssh_sftp_do_select(false, false);
|
2004-11-27 13:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a PSFTP command line from stdin.
|
|
|
|
*/
|
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
|
|
|
char *ssh_sftp_get_cmdline(const char *prompt, bool no_fds_ok)
|
2004-11-27 13:20:21 +00:00
|
|
|
{
|
|
|
|
char *buf;
|
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the
lines of
if (logical_array_len >= physical_array_size) {
physical_array_size = logical_array_len * 5 / 4 + 256;
array = sresize(array, physical_array_size, ElementType);
}
which happens at a zillion call sites throughout this code base, with
different random choices of the geometric factor and additive
constant, sometimes forgetting them completely, and generally doing a
lot of repeated work.
The new macro sgrowarray(array,size,n) has the semantics: here are the
array pointer and its physical size for you to modify, now please
ensure that the nth element exists, so I can write into it. And
sgrowarrayn(array,size,n,m) is the same except that it ensures that
the array has size at least n+m (so sgrowarray is just the special
case where m=1).
Now that this is a single centralised implementation that will be used
everywhere, I've also gone to more effort in the implementation, with
careful overflow checks that would have been painful to put at all the
previous call sites.
This commit also switches over every use of sresize(), apart from a
few where I really didn't think it would gain anything. A consequence
of that is that a lot of array-size variables have to have their types
changed to size_t, because the macros require that (they address-take
the size to pass to the underlying function).
2019-02-28 20:07:30 +00:00
|
|
|
size_t buflen, bufsize;
|
|
|
|
int ret;
|
2004-11-27 13:20:21 +00:00
|
|
|
|
|
|
|
fputs(prompt, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
buf = NULL;
|
|
|
|
buflen = bufsize = 0;
|
|
|
|
|
|
|
|
while (1) {
|
2019-09-08 19:29:00 +00:00
|
|
|
ret = ssh_sftp_do_select(true, no_fds_ok);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("connection died\n");
|
2013-07-14 10:46:07 +00:00
|
|
|
sfree(buf);
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL; /* woop woop */
|
|
|
|
}
|
|
|
|
if (ret > 0) {
|
New array-growing macros: sgrowarray and sgrowarrayn.
The idea of these is that they centralise the common idiom along the
lines of
if (logical_array_len >= physical_array_size) {
physical_array_size = logical_array_len * 5 / 4 + 256;
array = sresize(array, physical_array_size, ElementType);
}
which happens at a zillion call sites throughout this code base, with
different random choices of the geometric factor and additive
constant, sometimes forgetting them completely, and generally doing a
lot of repeated work.
The new macro sgrowarray(array,size,n) has the semantics: here are the
array pointer and its physical size for you to modify, now please
ensure that the nth element exists, so I can write into it. And
sgrowarrayn(array,size,n,m) is the same except that it ensures that
the array has size at least n+m (so sgrowarray is just the special
case where m=1).
Now that this is a single centralised implementation that will be used
everywhere, I've also gone to more effort in the implementation, with
careful overflow checks that would have been painful to put at all the
previous call sites.
This commit also switches over every use of sresize(), apart from a
few where I really didn't think it would gain anything. A consequence
of that is that a lot of array-size variables have to have their types
changed to size_t, because the macros require that (they address-take
the size to pass to the underlying function).
2019-02-28 20:07:30 +00:00
|
|
|
sgrowarray(buf, bufsize, buflen);
|
2019-09-08 19:29:00 +00:00
|
|
|
ret = read(0, buf+buflen, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("read");
|
2013-07-14 10:46:07 +00:00
|
|
|
sfree(buf);
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
/* eof on stdin; no error, but no answer either */
|
2013-07-14 10:46:07 +00:00
|
|
|
sfree(buf);
|
2019-09-08 19:29:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[buflen++] == '\n') {
|
|
|
|
/* we have a full line */
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
2004-11-27 13:20:21 +00:00
|
|
|
}
|
2003-09-02 09:00:35 +00:00
|
|
|
}
|
|
|
|
|
2011-12-08 19:15:57 +00:00
|
|
|
void frontend_net_error_pending(void) {}
|
|
|
|
|
Remove FLAG_VERBOSE.
The global 'int flags' has always been an ugly feature of this code
base, and I suddenly thought that perhaps it's time to start throwing
it out, one flag at a time, until it's totally unused.
My first target is FLAG_VERBOSE. This was usually set by cmdline.c
when it saw a -v option on the program's command line, except that GUI
PuTTY itself sets it unconditionally on startup. And then various bits
of the code would check it in order to decide whether to print a given
message.
In the current system of front-end abstraction traits, there's no
_one_ place that I can move it to. But there are two: every place that
checked FLAG_VERBOSE has access to either a Seat or a LogPolicy. So
now each of those traits has a query method for 'do I want verbose
messages?'.
A good effect of this is that subsidiary Seats, like the ones used in
Uppity for the main SSH server module itself and the server end of
shell channels, now get to have their own verbosity setting instead of
inheriting the one global one. In fact I don't expect any code using
those Seats to be generating any messages at all, but if that changes
later, we'll have a way to control it. (Who knows, perhaps logging in
Uppity might become a thing.)
As part of this cleanup, I've added a new flag to cmdline_tooltype,
called TOOLTYPE_NO_VERBOSE_OPTION. The unconditionally-verbose tools
now set that, and it has the effect of making cmdline.c disallow -v
completely. So where 'putty -v' would previously have been silently
ignored ("I was already verbose"), it's now an error, reminding you
that that option doesn't actually do anything.
Finally, the 'default_logpolicy' provided by uxcons.c and wincons.c
(with identical definitions) has had to move into a new file of its
own, because now it has to ask cmdline.c for the verbosity setting as
well as asking console.c for the rest of its methods. So there's a new
file clicons.c which can only be included by programs that link
against both cmdline.c _and_ one of the *cons.c, and I've renamed the
logpolicy to reflect that.
2020-01-30 06:40:21 +00:00
|
|
|
void platform_psftp_pre_conn_setup(LogPolicy *lp) {}
|
2016-04-02 07:00:07 +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
|
|
|
const bool buildinfo_gtk_relevant = false;
|
2017-02-22 22:10:05 +00:00
|
|
|
|
2003-09-02 09:00:35 +00:00
|
|
|
/*
|
|
|
|
* Main program: do platform-specific initialisation and then call
|
|
|
|
* psftp_main().
|
|
|
|
*/
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
uxsel_init();
|
|
|
|
return psftp_main(argc, argv);
|
|
|
|
}
|