2018-09-30 09:16:38 +03:00
|
|
|
/*
|
|
|
|
* SSH main session channel handling.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
#include "sshppl.h"
|
|
|
|
#include "sshchan.h"
|
|
|
|
|
|
|
|
static void mainchan_free(Channel *chan);
|
|
|
|
static void mainchan_open_confirmation(Channel *chan);
|
|
|
|
static void mainchan_open_failure(Channel *chan, const char *errtext);
|
2019-02-06 23:42:44 +03:00
|
|
|
static size_t mainchan_send(
|
|
|
|
Channel *chan, bool is_stderr, const void *, size_t);
|
2018-09-30 09:16:38 +03:00
|
|
|
static void mainchan_send_eof(Channel *chan);
|
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 22:23:19 +03:00
|
|
|
static void mainchan_set_input_wanted(Channel *chan, bool wanted);
|
2018-09-30 09:16:38 +03:00
|
|
|
static char *mainchan_log_close_msg(Channel *chan);
|
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 22:23:19 +03:00
|
|
|
static bool mainchan_rcvd_exit_status(Channel *chan, int status);
|
|
|
|
static bool mainchan_rcvd_exit_signal(
|
|
|
|
Channel *chan, ptrlen signame, bool core_dumped, ptrlen msg);
|
|
|
|
static bool mainchan_rcvd_exit_signal_numeric(
|
|
|
|
Channel *chan, int signum, bool core_dumped, ptrlen msg);
|
|
|
|
static void mainchan_request_response(Channel *chan, bool success);
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
static const struct ChannelVtable mainchan_channelvt = {
|
|
|
|
mainchan_free,
|
|
|
|
mainchan_open_confirmation,
|
|
|
|
mainchan_open_failure,
|
|
|
|
mainchan_send,
|
|
|
|
mainchan_send_eof,
|
|
|
|
mainchan_set_input_wanted,
|
|
|
|
mainchan_log_close_msg,
|
Allow channels not to close immediately after two EOFs.
Some kinds of channel, even after they've sent EOF in both directions,
still have something to do before they initiate the CLOSE mechanism
and wind up the channel completely. For example, a session channel
with a subprocess running inside it will want to be sure to send the
"exit-status" or "exit-signal" notification, even if that happens
after bidirectional EOF of the data channels.
Previously, the SSH-2 connection layer had the standard policy that
once EOF had been both sent and received, it would start the final
close procedure. There's a method chan_want_close() by which a Channel
could vary this policy in one direction, by indicating that it wanted
the close procedure to commence after EOF was sent in only one
direction. Its parameters are a pair of booleans saying whether EOF
has been sent, and whether it's been received.
Now chan_want_close can vary the policy in the other direction as
well: if it returns FALSE even when _both_ parameters are true, the
connection layer will honour that, and not send CHANNEL_CLOSE. If it
does that, the Channel is responsible for indicating when it _does_
want close later, by calling sshfwd_initiate_close.
2018-10-18 20:02:59 +03:00
|
|
|
chan_default_want_close,
|
2018-09-30 09:16:38 +03:00
|
|
|
mainchan_rcvd_exit_status,
|
|
|
|
mainchan_rcvd_exit_signal,
|
|
|
|
mainchan_rcvd_exit_signal_numeric,
|
2018-10-20 23:48:49 +03:00
|
|
|
chan_no_run_shell,
|
|
|
|
chan_no_run_command,
|
|
|
|
chan_no_run_subsystem,
|
|
|
|
chan_no_enable_x11_forwarding,
|
|
|
|
chan_no_enable_agent_forwarding,
|
|
|
|
chan_no_allocate_pty,
|
|
|
|
chan_no_set_env,
|
|
|
|
chan_no_send_break,
|
|
|
|
chan_no_send_signal,
|
|
|
|
chan_no_change_window_size,
|
2018-09-30 09:16:38 +03:00
|
|
|
mainchan_request_response,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum MainChanType {
|
|
|
|
MAINCHAN_SESSION, MAINCHAN_DIRECT_TCPIP
|
|
|
|
} MainChanType;
|
|
|
|
|
2018-11-01 21:17:41 +03:00
|
|
|
struct mainchan {
|
2018-09-30 09:16:38 +03:00
|
|
|
SshChannel *sc;
|
|
|
|
Conf *conf;
|
|
|
|
PacketProtocolLayer *ppl;
|
|
|
|
ConnectionLayer *cl;
|
|
|
|
|
|
|
|
MainChanType type;
|
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 22:23:19 +03:00
|
|
|
bool is_simple;
|
2018-09-30 09:16:38 +03: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 22:23:19 +03:00
|
|
|
bool req_x11, req_agent, req_pty, req_cmd_primary, req_cmd_fallback;
|
2018-09-30 09:16:38 +03:00
|
|
|
int n_req_env, n_env_replies, n_env_fails;
|
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 22:23:19 +03:00
|
|
|
bool eof_pending, eof_sent, got_pty, ready;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
int term_width, term_height;
|
|
|
|
|
|
|
|
Channel chan;
|
2018-11-01 21:17:41 +03:00
|
|
|
};
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
mainchan *mainchan_new(
|
|
|
|
PacketProtocolLayer *ppl, ConnectionLayer *cl, Conf *conf,
|
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 22:23:19 +03:00
|
|
|
int term_width, int term_height, bool is_simple, SshChannel **sc_out)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
mainchan *mc;
|
|
|
|
|
2018-10-29 22:57:31 +03:00
|
|
|
if (conf_get_bool(conf, CONF_ssh_no_shell))
|
2018-09-30 09:16:38 +03:00
|
|
|
return NULL; /* no main channel at all */
|
|
|
|
|
|
|
|
mc = snew(mainchan);
|
|
|
|
memset(mc, 0, sizeof(mainchan));
|
|
|
|
mc->ppl = ppl;
|
|
|
|
mc->cl = cl;
|
|
|
|
mc->conf = conf_copy(conf);
|
|
|
|
mc->term_width = term_width;
|
|
|
|
mc->term_height = term_height;
|
|
|
|
mc->is_simple = is_simple;
|
|
|
|
|
|
|
|
mc->sc = NULL;
|
|
|
|
mc->chan.vt = &mainchan_channelvt;
|
|
|
|
mc->chan.initial_fixed_window_size = 0;
|
|
|
|
|
|
|
|
if (*conf_get_str(mc->conf, CONF_ssh_nc_host)) {
|
|
|
|
const char *host = conf_get_str(mc->conf, CONF_ssh_nc_host);
|
|
|
|
int port = conf_get_int(mc->conf, CONF_ssh_nc_port);
|
|
|
|
|
2018-10-18 22:06:42 +03:00
|
|
|
mc->sc = ssh_lportfwd_open(cl, host, port, "main channel",
|
|
|
|
NULL, &mc->chan);
|
2018-09-30 09:16:38 +03:00
|
|
|
mc->type = MAINCHAN_DIRECT_TCPIP;
|
|
|
|
} else {
|
|
|
|
mc->sc = ssh_session_open(cl, &mc->chan);
|
|
|
|
mc->type = MAINCHAN_SESSION;
|
|
|
|
}
|
|
|
|
|
2018-09-30 13:22:01 +03:00
|
|
|
if (sc_out) *sc_out = mc->sc;
|
2018-09-30 09:16:38 +03:00
|
|
|
return mc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_free(Channel *chan)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
conf_free(mc->conf);
|
|
|
|
sfree(mc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_try_fallback_command(mainchan *mc);
|
|
|
|
static void mainchan_ready(mainchan *mc);
|
|
|
|
|
|
|
|
static void mainchan_open_confirmation(Channel *chan)
|
|
|
|
{
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
seat_update_specials_menu(mc->ppl->seat);
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Opened main channel");
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
if (mc->is_simple)
|
|
|
|
sshfwd_hint_channel_is_simple(mc->sc);
|
|
|
|
|
|
|
|
if (mc->type == MAINCHAN_SESSION) {
|
|
|
|
/*
|
|
|
|
* Send the CHANNEL_REQUESTS for the main session channel.
|
|
|
|
*/
|
|
|
|
char *key, *val, *cmd;
|
|
|
|
struct X11Display *x11disp;
|
|
|
|
struct X11FakeAuth *x11auth;
|
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 22:23:19 +03:00
|
|
|
bool retry_cmd_now = false;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
2018-10-29 22:57:31 +03:00
|
|
|
if (conf_get_bool(mc->conf, CONF_x11_forward)) {;
|
2018-09-30 09:16:38 +03:00
|
|
|
char *x11_setup_err;
|
|
|
|
if ((x11disp = x11_setup_display(
|
|
|
|
conf_get_str(mc->conf, CONF_x11_display),
|
|
|
|
mc->conf, &x11_setup_err)) == NULL) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("X11 forwarding not enabled: unable to"
|
|
|
|
" initialise X display: %s", x11_setup_err);
|
2018-09-30 09:16:38 +03:00
|
|
|
sfree(x11_setup_err);
|
|
|
|
} else {
|
|
|
|
x11auth = ssh_add_x11_display(
|
|
|
|
mc->cl, conf_get_int(mc->conf, CONF_x11_auth), x11disp);
|
|
|
|
|
|
|
|
sshfwd_request_x11_forwarding(
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->sc, true, x11auth->protoname, x11auth->datastring,
|
|
|
|
x11disp->screennum, false);
|
|
|
|
mc->req_x11 = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssh_agent_forwarding_permitted(mc->cl)) {
|
2018-10-29 22:50:29 +03:00
|
|
|
sshfwd_request_agent_forwarding(mc->sc, true);
|
|
|
|
mc->req_agent = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 22:57:31 +03:00
|
|
|
if (!conf_get_bool(mc->conf, CONF_nopty)) {
|
2018-09-30 09:16:38 +03:00
|
|
|
sshfwd_request_pty(
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->sc, true, mc->conf, mc->term_width, mc->term_height);
|
|
|
|
mc->req_pty = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (val = conf_get_str_strs(mc->conf, CONF_environmt, NULL, &key);
|
|
|
|
val != NULL;
|
|
|
|
val = conf_get_str_strs(mc->conf, CONF_environmt, key, &key)) {
|
2018-10-29 22:50:29 +03:00
|
|
|
sshfwd_send_env_var(mc->sc, true, key, val);
|
2018-09-30 09:16:38 +03:00
|
|
|
mc->n_req_env++;
|
|
|
|
}
|
|
|
|
if (mc->n_req_env)
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Sent %d environment variables", mc->n_req_env);
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
cmd = conf_get_str(mc->conf, CONF_remote_cmd);
|
2018-10-29 22:57:31 +03:00
|
|
|
if (conf_get_bool(mc->conf, CONF_ssh_subsys)) {
|
2018-10-29 22:50:29 +03:00
|
|
|
retry_cmd_now = !sshfwd_start_subsystem(mc->sc, true, cmd);
|
2018-09-30 09:16:38 +03:00
|
|
|
} else if (*cmd) {
|
2018-10-29 22:50:29 +03:00
|
|
|
sshfwd_start_command(mc->sc, true, cmd);
|
2018-09-30 09:16:38 +03:00
|
|
|
} else {
|
2018-10-29 22:50:29 +03:00
|
|
|
sshfwd_start_shell(mc->sc, true);
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retry_cmd_now)
|
|
|
|
mainchan_try_fallback_command(mc);
|
|
|
|
else
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->req_cmd_primary = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
} else {
|
2018-10-29 22:50:29 +03:00
|
|
|
ssh_set_ldisc_option(mc->cl, LD_ECHO, true);
|
|
|
|
ssh_set_ldisc_option(mc->cl, LD_EDIT, true);
|
2018-09-30 09:16:38 +03:00
|
|
|
mainchan_ready(mc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_try_fallback_command(mainchan *mc)
|
|
|
|
{
|
|
|
|
const char *cmd = conf_get_str(mc->conf, CONF_remote_cmd2);
|
2018-10-29 22:57:31 +03:00
|
|
|
if (conf_get_bool(mc->conf, CONF_ssh_subsys2)) {
|
2018-10-29 22:50:29 +03:00
|
|
|
sshfwd_start_subsystem(mc->sc, true, cmd);
|
2018-09-30 09:16:38 +03:00
|
|
|
} else {
|
2018-10-29 22:50:29 +03:00
|
|
|
sshfwd_start_command(mc->sc, true, cmd);
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->req_cmd_fallback = true;
|
2018-09-30 09:16:38 +03: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 22:23:19 +03:00
|
|
|
static void mainchan_request_response(Channel *chan, bool success)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
if (mc->req_x11) {
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->req_x11 = false;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
if (success) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("X11 forwarding enabled");
|
2018-09-30 09:16:38 +03:00
|
|
|
ssh_enable_x_fwd(mc->cl);
|
|
|
|
} else {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("X11 forwarding refused");
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_agent) {
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->req_agent = false;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
if (success) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Agent forwarding enabled");
|
2018-09-30 09:16:38 +03:00
|
|
|
ssh_enable_agent_fwd(mc->cl);
|
|
|
|
} else {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Agent forwarding refused");
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_pty) {
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->req_pty = false;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
if (success) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Allocated pty");
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->got_pty = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
} else {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Server refused to allocate pty");
|
|
|
|
ppl_printf("Server refused to allocate pty\r\n");
|
2018-10-29 22:50:29 +03:00
|
|
|
ssh_set_ldisc_option(mc->cl, LD_ECHO, true);
|
|
|
|
ssh_set_ldisc_option(mc->cl, LD_EDIT, true);
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->n_env_replies < mc->n_req_env) {
|
|
|
|
int j = mc->n_env_replies++;
|
|
|
|
if (!success) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Server refused to set environment variable %s",
|
|
|
|
conf_get_str_nthstrkey(mc->conf,
|
|
|
|
CONF_environmt, j));
|
2018-09-30 09:16:38 +03:00
|
|
|
mc->n_env_fails++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->n_env_replies == mc->n_req_env) {
|
|
|
|
if (mc->n_env_fails == 0) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("All environment variables successfully set");
|
2018-09-30 09:16:38 +03:00
|
|
|
} else if (mc->n_env_fails == mc->n_req_env) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("All environment variables refused");
|
|
|
|
ppl_printf("Server refused to set environment "
|
|
|
|
"variables\r\n");
|
2018-09-30 09:16:38 +03:00
|
|
|
} else {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_printf("Server refused to set all environment "
|
|
|
|
"variables\r\n");
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_cmd_primary) {
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->req_cmd_primary = false;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
if (success) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Started a shell/command");
|
2018-09-30 09:16:38 +03:00
|
|
|
mainchan_ready(mc);
|
|
|
|
} else if (*conf_get_str(mc->conf, CONF_remote_cmd2)) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Primary command failed; attempting fallback");
|
2018-09-30 09:16:38 +03:00
|
|
|
mainchan_try_fallback_command(mc);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If there's no remote_cmd2 configured, then we have no
|
|
|
|
* fallback command, so we've run out of options.
|
|
|
|
*/
|
|
|
|
ssh_sw_abort(mc->ppl->ssh,
|
|
|
|
"Server refused to start a shell/command");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc->req_cmd_fallback) {
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->req_cmd_fallback = false;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
if (success) {
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Started a shell/command");
|
2018-09-30 09:16:38 +03:00
|
|
|
ssh_got_fallback_cmd(mc->ppl->ssh);
|
|
|
|
mainchan_ready(mc);
|
|
|
|
} else {
|
|
|
|
ssh_sw_abort(mc->ppl->ssh,
|
|
|
|
"Server refused to start a shell/command");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_ready(mainchan *mc)
|
|
|
|
{
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->ready = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
|
2018-10-29 22:50:29 +03:00
|
|
|
ssh_set_wants_user_input(mc->cl, true);
|
2018-09-30 09:16:38 +03:00
|
|
|
ssh_ppl_got_user_input(mc->ppl); /* in case any is already queued */
|
|
|
|
|
|
|
|
/* If an EOF arrived before we were ready, handle it now. */
|
|
|
|
if (mc->eof_pending) {
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->eof_pending = false;
|
2018-09-30 09:16:38 +03:00
|
|
|
mainchan_special_cmd(mc, SS_EOF, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_ldisc_update(mc->ppl->ssh);
|
|
|
|
queue_idempotent_callback(&mc->ppl->ic_process_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mainchan_open_failure_abort_ctx {
|
|
|
|
Ssh *ssh;
|
|
|
|
char *abort_message;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mainchan_open_failure_abort(void *vctx)
|
|
|
|
{
|
|
|
|
struct mainchan_open_failure_abort_ctx *ctx =
|
|
|
|
(struct mainchan_open_failure_abort_ctx *)vctx;
|
|
|
|
ssh_sw_abort(
|
|
|
|
ctx->ssh, "Server refused to open main channel: %s",
|
|
|
|
ctx->abort_message);
|
|
|
|
sfree(ctx->abort_message);
|
|
|
|
sfree(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_open_failure(Channel *chan, const char *errtext)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
|
|
|
|
struct mainchan_open_failure_abort_ctx *ctx =
|
|
|
|
snew(struct mainchan_open_failure_abort_ctx);
|
|
|
|
|
|
|
|
ctx->ssh = mc->ppl->ssh;
|
|
|
|
ctx->abort_message = dupstr(errtext);
|
|
|
|
queue_toplevel_callback(mainchan_open_failure_abort, ctx);
|
|
|
|
}
|
|
|
|
|
2019-02-06 23:42:44 +03:00
|
|
|
static size_t mainchan_send(Channel *chan, bool is_stderr,
|
|
|
|
const void *data, size_t length)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
return seat_output(mc->ppl->seat, is_stderr, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_send_eof(Channel *chan)
|
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
2018-10-30 21:09:12 +03:00
|
|
|
if (!mc->eof_sent && (seat_eof(mc->ppl->seat) || mc->got_pty)) {
|
2018-09-30 09:16:38 +03:00
|
|
|
/*
|
|
|
|
* Either seat_eof told us that the front end wants us to
|
|
|
|
* close the outgoing side of the connection as soon as we see
|
|
|
|
* EOF from the far end, or else we've unilaterally decided to
|
|
|
|
* do that because we've allocated a remote pty and hence EOF
|
|
|
|
* isn't a particularly meaningful concept.
|
|
|
|
*/
|
|
|
|
sshfwd_write_eof(mc->sc);
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Sent EOF message");
|
2018-11-12 23:27:25 +03:00
|
|
|
mc->eof_sent = true;
|
|
|
|
ssh_set_wants_user_input(mc->cl, false); /* stop reading from stdin */
|
2018-09-30 09:16:38 +03: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 22:23:19 +03:00
|
|
|
static void mainchan_set_input_wanted(Channel *chan, bool wanted)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the main channel of the SSH session, i.e. the one tied
|
|
|
|
* to the standard input (or GUI) of the primary SSH client user
|
|
|
|
* interface. So ssh->send_ok is how we control whether we're
|
|
|
|
* reading from that input.
|
|
|
|
*/
|
|
|
|
ssh_set_wants_user_input(mc->cl, wanted);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *mainchan_log_close_msg(Channel *chan)
|
|
|
|
{
|
|
|
|
return dupstr("Main session channel closed");
|
|
|
|
}
|
|
|
|
|
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 22:23:19 +03:00
|
|
|
static bool mainchan_rcvd_exit_status(Channel *chan, int status)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
ssh_got_exitcode(mc->ppl->ssh, status);
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Session sent command exit status %d", status);
|
2018-10-29 22:50:29 +03:00
|
|
|
return true;
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mainchan_log_exit_signal_common(
|
|
|
|
mainchan *mc, const char *sigdesc,
|
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 22:23:19 +03:00
|
|
|
bool core_dumped, ptrlen msg)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
|
|
|
|
const char *core_msg = core_dumped ? " (core dumped)" : "";
|
|
|
|
const char *msg_pre = (msg.len ? " (" : "");
|
|
|
|
const char *msg_post = (msg.len ? ")" : "");
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Session exited on %s%s%s%.*s%s",
|
|
|
|
sigdesc, core_msg, msg_pre, PTRLEN_PRINTF(msg), msg_post);
|
2018-09-30 09:16:38 +03: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 22:23:19 +03:00
|
|
|
static bool mainchan_rcvd_exit_signal(
|
|
|
|
Channel *chan, ptrlen signame, bool core_dumped, ptrlen msg)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
int exitcode;
|
|
|
|
char *signame_str;
|
|
|
|
|
|
|
|
/*
|
2018-10-14 12:05:23 +03:00
|
|
|
* Translate the signal description back into a locally meaningful
|
|
|
|
* number, or 128 if the string didn't match any we recognise.
|
2018-09-30 09:16:38 +03:00
|
|
|
*/
|
2018-10-14 12:05:23 +03:00
|
|
|
exitcode = 128;
|
|
|
|
|
|
|
|
#define SIGNAL_SUB(s) \
|
|
|
|
if (ptrlen_eq_string(signame, #s)) \
|
|
|
|
exitcode = 128 + SIG ## s;
|
|
|
|
#define SIGNAL_MAIN(s, text) SIGNAL_SUB(s)
|
|
|
|
#define SIGNALS_LOCAL_ONLY
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_SUB
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNALS_LOCAL_ONLY
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
ssh_got_exitcode(mc->ppl->ssh, exitcode);
|
|
|
|
if (exitcode == 128)
|
|
|
|
signame_str = dupprintf("unrecognised signal \"%.*s\"",
|
|
|
|
PTRLEN_PRINTF(signame));
|
|
|
|
else
|
|
|
|
signame_str = dupprintf("signal SIG%.*s", PTRLEN_PRINTF(signame));
|
|
|
|
mainchan_log_exit_signal_common(mc, signame_str, core_dumped, msg);
|
|
|
|
sfree(signame_str);
|
2018-10-29 22:50:29 +03:00
|
|
|
return true;
|
2018-09-30 09:16:38 +03: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 22:23:19 +03:00
|
|
|
static bool mainchan_rcvd_exit_signal_numeric(
|
|
|
|
Channel *chan, int signum, bool core_dumped, ptrlen msg)
|
2018-09-30 09:16:38 +03:00
|
|
|
{
|
|
|
|
assert(chan->vt == &mainchan_channelvt);
|
|
|
|
mainchan *mc = container_of(chan, mainchan, chan);
|
|
|
|
char *signum_str;
|
|
|
|
|
|
|
|
ssh_got_exitcode(mc->ppl->ssh, 128 + signum);
|
|
|
|
signum_str = dupprintf("signal %d", signum);
|
|
|
|
mainchan_log_exit_signal_common(mc, signum_str, core_dumped, msg);
|
|
|
|
sfree(signum_str);
|
2018-10-29 22:50:29 +03:00
|
|
|
return true;
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void mainchan_get_specials(
|
|
|
|
mainchan *mc, add_special_fn_t add_special, void *ctx)
|
|
|
|
{
|
|
|
|
/* FIXME: this _does_ depend on whether these services are supported */
|
|
|
|
|
|
|
|
add_special(ctx, "Break", SS_BRK, 0);
|
|
|
|
|
2018-10-14 12:05:23 +03:00
|
|
|
#define SIGNAL_MAIN(name, desc) \
|
2018-09-30 09:16:38 +03:00
|
|
|
add_special(ctx, "SIG" #name " (" desc ")", SS_SIG ## name, 0);
|
2018-10-14 12:05:23 +03:00
|
|
|
#define SIGNAL_SUB(name)
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNAL_SUB
|
2018-09-30 09:16:38 +03:00
|
|
|
|
|
|
|
add_special(ctx, "More signals", SS_SUBMENU, 0);
|
|
|
|
|
2018-10-14 12:05:23 +03:00
|
|
|
#define SIGNAL_MAIN(name, desc)
|
|
|
|
#define SIGNAL_SUB(name) \
|
|
|
|
add_special(ctx, "SIG" #name, SS_SIG ## name, 0);
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNAL_SUB
|
|
|
|
|
|
|
|
add_special(ctx, NULL, SS_EXITMENU, 0);
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *ssh_signal_lookup(SessionSpecialCode code)
|
|
|
|
{
|
2018-10-14 12:05:23 +03:00
|
|
|
#define SIGNAL_SUB(name) \
|
2018-09-30 09:16:38 +03:00
|
|
|
if (code == SS_SIG ## name) return #name;
|
2018-10-14 12:05:23 +03:00
|
|
|
#define SIGNAL_MAIN(name, desc) SIGNAL_SUB(name)
|
|
|
|
#include "sshsignals.h"
|
|
|
|
#undef SIGNAL_MAIN
|
|
|
|
#undef SIGNAL_SUB
|
2018-09-30 09:16:38 +03:00
|
|
|
|
2018-10-14 12:05:23 +03:00
|
|
|
/* If none of those clauses matched, fail lookup. */
|
2018-09-30 09:16:38 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mainchan_special_cmd(mainchan *mc, SessionSpecialCode code, int arg)
|
|
|
|
{
|
|
|
|
PacketProtocolLayer *ppl = mc->ppl; /* for ppl_logevent */
|
|
|
|
const char *signame;
|
|
|
|
|
|
|
|
if (code == SS_EOF) {
|
|
|
|
if (!mc->ready) {
|
|
|
|
/*
|
|
|
|
* Buffer the EOF to send as soon as the main channel is
|
|
|
|
* fully set up.
|
|
|
|
*/
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->eof_pending = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
} else if (!mc->eof_sent) {
|
|
|
|
sshfwd_write_eof(mc->sc);
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->eof_sent = true;
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
} else if (code == SS_BRK) {
|
|
|
|
sshfwd_send_serial_break(
|
2018-10-29 22:50:29 +03:00
|
|
|
mc->sc, false, 0 /* default break length */);
|
2018-09-30 09:16:38 +03:00
|
|
|
} else if ((signame = ssh_signal_lookup(code)) != NULL) {
|
|
|
|
/* It's a signal. */
|
2018-10-29 22:50:29 +03:00
|
|
|
sshfwd_send_signal(mc->sc, false, signame);
|
Start using C99 variadic macros.
In the past, I've had a lot of macros which you call with double
parentheses, along the lines of debug(("format string", params)), so
that the inner parens protect the commas and permit the macro to treat
the whole printf-style argument list as one macro argument.
That's all very well, but it's a bit inconvenient (it doesn't leave
you any way to implement such a macro by prepending another argument
to the list), and now this code base's rules allow C99isms, I can
switch all those macros to using a single pair of parens, using the
C99 ability to say '...' in the parameter list of the #define and get
at the corresponding suffix of the arguments as __VA_ARGS__.
So I'm doing it. I've made the following printf-style macros variadic:
bpp_logevent, ppl_logevent, ppl_printf and debug.
While I'm here, I've also fixed up a collection of conditioned-out
calls to debug() in the Windows front end which were clearly expecting
a macro with a different calling syntax, because they had an integer
parameter first. If I ever have a need to condition those back in,
they should actually work now.
2018-12-08 23:32:31 +03:00
|
|
|
ppl_logevent("Sent signal SIG%s", signame);
|
2018-09-30 09:16:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mainchan_terminal_size(mainchan *mc, int width, int height)
|
|
|
|
{
|
|
|
|
mc->term_width = width;
|
|
|
|
mc->term_height = height;
|
|
|
|
|
|
|
|
if (mc->req_pty || mc->got_pty)
|
|
|
|
sshfwd_send_terminal_size_change(mc->sc, width, height);
|
|
|
|
}
|