2013-11-17 14:05:29 +00:00
|
|
|
/*
|
|
|
|
* winsecur.c: implementation of winsecur.h.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
|
|
|
|
#if !defined NO_SECURITY
|
|
|
|
|
|
|
|
#include "winsecur.h"
|
|
|
|
|
2015-11-22 12:04:04 +00:00
|
|
|
/* Initialised once, then kept around to reuse forever */
|
|
|
|
static PSID worldsid, networksid, usersid;
|
|
|
|
|
2020-02-02 10:00:43 +00:00
|
|
|
DEF_WINDOWS_FUNCTION(OpenProcessToken);
|
|
|
|
DEF_WINDOWS_FUNCTION(GetTokenInformation);
|
|
|
|
DEF_WINDOWS_FUNCTION(InitializeSecurityDescriptor);
|
|
|
|
DEF_WINDOWS_FUNCTION(SetSecurityDescriptorOwner);
|
|
|
|
DEF_WINDOWS_FUNCTION(GetSecurityInfo);
|
|
|
|
DEF_WINDOWS_FUNCTION(SetSecurityInfo);
|
|
|
|
DEF_WINDOWS_FUNCTION(SetEntriesInAclA);
|
2015-11-22 12:04:04 +00:00
|
|
|
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
bool got_advapi(void)
|
2013-11-17 14:05:29 +00:00
|
|
|
{
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
static bool attempted = false;
|
|
|
|
static bool successful;
|
2013-11-17 14:05:29 +00:00
|
|
|
static HMODULE advapi;
|
|
|
|
|
|
|
|
if (!attempted) {
|
2018-10-29 19:50:29 +00:00
|
|
|
attempted = true;
|
2013-11-17 14:05:29 +00:00
|
|
|
advapi = load_system32_dll("advapi32.dll");
|
|
|
|
successful = advapi &&
|
|
|
|
GET_WINDOWS_FUNCTION(advapi, GetSecurityInfo) &&
|
2015-11-28 18:31:10 +00:00
|
|
|
GET_WINDOWS_FUNCTION(advapi, SetSecurityInfo) &&
|
2013-11-17 14:05:29 +00:00
|
|
|
GET_WINDOWS_FUNCTION(advapi, OpenProcessToken) &&
|
|
|
|
GET_WINDOWS_FUNCTION(advapi, GetTokenInformation) &&
|
|
|
|
GET_WINDOWS_FUNCTION(advapi, InitializeSecurityDescriptor) &&
|
2013-11-17 14:05:41 +00:00
|
|
|
GET_WINDOWS_FUNCTION(advapi, SetSecurityDescriptorOwner) &&
|
|
|
|
GET_WINDOWS_FUNCTION(advapi, SetEntriesInAclA);
|
|
|
|
}
|
|
|
|
return successful;
|
|
|
|
}
|
|
|
|
|
2013-11-17 14:05:29 +00:00
|
|
|
PSID get_user_sid(void)
|
|
|
|
{
|
|
|
|
HANDLE proc = NULL, tok = NULL;
|
|
|
|
TOKEN_USER *user = NULL;
|
|
|
|
DWORD toklen, sidlen;
|
|
|
|
PSID sid = NULL, ret = NULL;
|
|
|
|
|
2016-02-27 09:25:23 +00:00
|
|
|
if (usersid)
|
|
|
|
return usersid;
|
|
|
|
|
2013-11-17 14:05:29 +00:00
|
|
|
if (!got_advapi())
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
if ((proc = OpenProcess(MAXIMUM_ALLOWED, false,
|
2013-11-17 14:05:29 +00:00
|
|
|
GetCurrentProcessId())) == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
|
|
|
|
GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
sidlen = GetLengthSid(user->User.Sid);
|
|
|
|
|
|
|
|
sid = (PSID)smalloc(sidlen);
|
|
|
|
|
|
|
|
if (!CopySid(sidlen, sid, user->User.Sid))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Success. Move sid into the return value slot, and null it out
|
|
|
|
* to stop the cleanup code freeing it. */
|
2016-02-27 09:25:23 +00:00
|
|
|
ret = usersid = sid;
|
2013-11-17 14:05:29 +00:00
|
|
|
sid = NULL;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (proc != NULL)
|
|
|
|
CloseHandle(proc);
|
|
|
|
if (tok != NULL)
|
|
|
|
CloseHandle(tok);
|
|
|
|
if (user != NULL)
|
|
|
|
LocalFree(user);
|
|
|
|
if (sid != NULL)
|
|
|
|
sfree(sid);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-29 06:22:01 +00:00
|
|
|
static bool getsids(char **error)
|
2013-11-17 14:05:41 +00:00
|
|
|
{
|
2017-02-05 11:19:22 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wmissing-braces"
|
|
|
|
#endif
|
|
|
|
SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
|
|
|
|
SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
bool ret = false;
|
2013-11-17 14:05:41 +00:00
|
|
|
|
2017-02-01 20:42:21 +00:00
|
|
|
*error = NULL;
|
2013-11-17 14:05:41 +00:00
|
|
|
|
2013-11-25 18:35:14 +00:00
|
|
|
if (!usersid) {
|
|
|
|
if ((usersid = get_user_sid()) == NULL) {
|
2017-02-01 20:42:21 +00:00
|
|
|
*error = dupprintf("unable to construct SID for current user: %s",
|
2013-11-25 18:35:14 +00:00
|
|
|
win_strerror(GetLastError()));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!worldsid) {
|
|
|
|
if (!AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, &worldsid)) {
|
2017-02-01 20:42:21 +00:00
|
|
|
*error = dupprintf("unable to construct SID for world: %s",
|
2013-11-25 18:35:14 +00:00
|
|
|
win_strerror(GetLastError()));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!networksid) {
|
|
|
|
if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, &networksid)) {
|
2017-02-01 20:42:21 +00:00
|
|
|
*error = dupprintf("unable to construct SID for "
|
2013-11-25 18:35:14 +00:00
|
|
|
"local same-user access only: %s",
|
|
|
|
win_strerror(GetLastError()));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-11-17 14:05:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
ret = true;
|
2015-11-22 12:04:04 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
2019-09-08 19:29:00 +00:00
|
|
|
|
2015-11-22 12:04:04 +00:00
|
|
|
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
bool make_private_security_descriptor(DWORD permissions,
|
|
|
|
PSECURITY_DESCRIPTOR *psd,
|
|
|
|
PACL *acl,
|
|
|
|
char **error)
|
2015-11-22 12:04:04 +00:00
|
|
|
{
|
|
|
|
EXPLICIT_ACCESS ea[3];
|
|
|
|
int acl_err;
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
bool ret = false;
|
2015-11-22 12:04:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
*psd = NULL;
|
|
|
|
*acl = NULL;
|
|
|
|
*error = NULL;
|
|
|
|
|
2017-02-01 20:42:21 +00:00
|
|
|
if (!getsids(error))
|
2015-11-22 12:04:04 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-11-17 14:05:41 +00:00
|
|
|
memset(ea, 0, sizeof(ea));
|
|
|
|
ea[0].grfAccessPermissions = permissions;
|
|
|
|
ea[0].grfAccessMode = REVOKE_ACCESS;
|
|
|
|
ea[0].grfInheritance = NO_INHERITANCE;
|
2013-11-25 18:35:14 +00:00
|
|
|
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
|
|
|
ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
|
2013-11-17 14:05:41 +00:00
|
|
|
ea[1].grfAccessPermissions = permissions;
|
|
|
|
ea[1].grfAccessMode = GRANT_ACCESS;
|
|
|
|
ea[1].grfInheritance = NO_INHERITANCE;
|
2013-11-25 18:35:14 +00:00
|
|
|
ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
|
|
|
ea[1].Trustee.ptstrName = (LPTSTR)usersid;
|
2013-11-17 14:05:41 +00:00
|
|
|
ea[2].grfAccessPermissions = permissions;
|
|
|
|
ea[2].grfAccessMode = REVOKE_ACCESS;
|
|
|
|
ea[2].grfInheritance = NO_INHERITANCE;
|
|
|
|
ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
2013-11-25 18:35:14 +00:00
|
|
|
ea[2].Trustee.ptstrName = (LPTSTR)networksid;
|
2013-11-17 14:05:41 +00:00
|
|
|
|
2013-11-22 19:41:49 +00:00
|
|
|
acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
|
|
|
|
if (acl_err != ERROR_SUCCESS || *acl == NULL) {
|
2013-11-17 14:05:41 +00:00
|
|
|
*error = dupprintf("unable to construct ACL: %s",
|
2013-11-22 19:41:49 +00:00
|
|
|
win_strerror(acl_err));
|
2013-11-17 14:05:41 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
*psd = (PSECURITY_DESCRIPTOR)
|
|
|
|
LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
|
|
|
|
if (!*psd) {
|
|
|
|
*error = dupprintf("unable to allocate security descriptor: %s",
|
|
|
|
win_strerror(GetLastError()));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
|
|
|
|
*error = dupprintf("unable to initialise security descriptor: %s",
|
|
|
|
win_strerror(GetLastError()));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
if (!SetSecurityDescriptorOwner(*psd, usersid, false)) {
|
2014-05-13 19:19:28 +00:00
|
|
|
*error = dupprintf("unable to set owner in security descriptor: %s",
|
|
|
|
win_strerror(GetLastError()));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
if (!SetSecurityDescriptorDacl(*psd, true, *acl, false)) {
|
2013-11-17 14:05:41 +00:00
|
|
|
*error = dupprintf("unable to set DACL in security descriptor: %s",
|
|
|
|
win_strerror(GetLastError()));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:50:29 +00:00
|
|
|
ret = true;
|
2013-11-17 14:05:41 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (!ret) {
|
|
|
|
if (*psd) {
|
|
|
|
LocalFree(*psd);
|
|
|
|
*psd = NULL;
|
|
|
|
}
|
|
|
|
if (*acl) {
|
|
|
|
LocalFree(*acl);
|
|
|
|
*acl = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sfree(*error);
|
|
|
|
*error = NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-02 10:00:42 +00:00
|
|
|
static bool acl_restricted = false;
|
|
|
|
bool restricted_acl(void) { return acl_restricted; }
|
|
|
|
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
static bool really_restrict_process_acl(char **error)
|
2015-11-22 12:04:04 +00:00
|
|
|
{
|
|
|
|
EXPLICIT_ACCESS ea[2];
|
|
|
|
int acl_err;
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
bool ret = false;
|
2015-11-22 12:04:04 +00:00
|
|
|
PACL acl = NULL;
|
|
|
|
|
2016-04-10 13:24:39 +00:00
|
|
|
static const DWORD nastyace=WRITE_DAC | WRITE_OWNER |
|
2019-09-08 19:29:00 +00:00
|
|
|
PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
|
|
|
|
PROCESS_DUP_HANDLE |
|
|
|
|
PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
|
|
|
|
PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
|
|
|
|
PROCESS_SUSPEND_RESUME;
|
2015-11-22 12:04:04 +00:00
|
|
|
|
|
|
|
if (!getsids(error))
|
2019-09-08 19:29:00 +00:00
|
|
|
goto cleanup;
|
2015-11-22 12:04:04 +00:00
|
|
|
|
|
|
|
memset(ea, 0, sizeof(ea));
|
|
|
|
|
|
|
|
/* Everyone: deny */
|
|
|
|
ea[0].grfAccessPermissions = nastyace;
|
|
|
|
ea[0].grfAccessMode = DENY_ACCESS;
|
|
|
|
ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
|
|
|
|
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
|
|
|
ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
|
|
|
|
|
|
|
|
/* User: user ace */
|
|
|
|
ea[1].grfAccessPermissions = ~nastyace & 0x1fff;
|
|
|
|
ea[1].grfAccessMode = GRANT_ACCESS;
|
|
|
|
ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
|
|
|
|
ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
|
|
|
|
ea[1].Trustee.ptstrName = (LPTSTR)usersid;
|
|
|
|
|
|
|
|
acl_err = p_SetEntriesInAclA(2, ea, NULL, &acl);
|
|
|
|
|
|
|
|
if (acl_err != ERROR_SUCCESS || acl == NULL) {
|
2019-09-08 19:29:00 +00:00
|
|
|
*error = dupprintf("unable to construct ACL: %s",
|
2017-02-01 20:42:21 +00:00
|
|
|
win_strerror(acl_err));
|
2015-11-22 12:04:04 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-11-28 18:31:10 +00:00
|
|
|
if (ERROR_SUCCESS != p_SetSecurityInfo
|
|
|
|
(GetCurrentProcess(), SE_KERNEL_OBJECT,
|
|
|
|
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
|
|
|
|
usersid, NULL, acl, NULL)) {
|
2019-09-08 19:29:00 +00:00
|
|
|
*error = dupprintf("Unable to set process ACL: %s",
|
2017-02-01 20:42:21 +00:00
|
|
|
win_strerror(GetLastError()));
|
2019-09-08 19:29:00 +00:00
|
|
|
goto cleanup;
|
2015-11-22 12:04:04 +00:00
|
|
|
}
|
2019-09-08 19:29:00 +00:00
|
|
|
|
2020-02-02 10:00:42 +00:00
|
|
|
acl_restricted = true;
|
2018-10-29 19:50:29 +00:00
|
|
|
ret=true;
|
2019-09-08 19:29:00 +00:00
|
|
|
|
2015-11-22 12:04:04 +00:00
|
|
|
cleanup:
|
|
|
|
if (!ret) {
|
|
|
|
if (acl) {
|
|
|
|
LocalFree(acl);
|
|
|
|
acl = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2017-02-01 20:42:21 +00:00
|
|
|
}
|
2013-11-17 14:05:29 +00:00
|
|
|
#endif /* !defined NO_SECURITY */
|
2017-01-28 21:56:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock down our process's ACL, to present an obstacle to malware
|
|
|
|
* trying to write into its memory. This can't be a full defence,
|
|
|
|
* because well timed malware could attack us before this code runs -
|
|
|
|
* even if it was unconditionally run at the very start of main(),
|
|
|
|
* which we wouldn't want to do anyway because it turns out in practie
|
|
|
|
* that interfering with other processes in this way has significant
|
|
|
|
* non-infringing uses on Windows (e.g. screen reader software).
|
|
|
|
*
|
|
|
|
* If we've been requested to do this and are unsuccessful, bomb out
|
|
|
|
* via modalfatalbox rather than continue in a less protected mode.
|
|
|
|
*
|
|
|
|
* This function is intentionally outside the #ifndef NO_SECURITY that
|
|
|
|
* covers the rest of this file, because when PuTTY is compiled
|
|
|
|
* without the ability to restrict its ACL, we don't want it to
|
|
|
|
* silently pretend to honour the instruction to do so.
|
|
|
|
*/
|
|
|
|
void restrict_process_acl(void)
|
|
|
|
{
|
|
|
|
char *error = NULL;
|
Convert a lot of 'int' variables to 'bool'.
My normal habit these days, in new code, is to treat int and bool as
_almost_ completely separate types. I'm still willing to use C's
implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine,
no need to spell it out as blob.len != 0), but generally, if a
variable is going to be conceptually a boolean, I like to declare it
bool and assign to it using 'true' or 'false' rather than 0 or 1.
PuTTY is an exception, because it predates the C99 bool, and I've
stuck to its existing coding style even when adding new code to it.
But it's been annoying me more and more, so now that I've decided C99
bool is an acceptable thing to require from our toolchain in the first
place, here's a quite thorough trawl through the source doing
'boolification'. Many variables and function parameters are now typed
as bool rather than int; many assignments of 0 or 1 to those variables
are now spelled 'true' or 'false'.
I managed this thorough conversion with the help of a custom clang
plugin that I wrote to trawl the AST and apply heuristics to point out
where things might want changing. So I've even managed to do a decent
job on parts of the code I haven't looked at in years!
To make the plugin's work easier, I pushed platform front ends
generally in the direction of using standard 'bool' in preference to
platform-specific boolean types like Windows BOOL or GTK's gboolean;
I've left the platform booleans in places they _have_ to be for the
platform APIs to work right, but variables only used by my own code
have been converted wherever I found them.
In a few places there are int values that look very like booleans in
_most_ of the places they're used, but have a rarely-used third value,
or a distinction between different nonzero values that most users
don't care about. In these cases, I've _removed_ uses of 'true' and
'false' for the return values, to emphasise that there's something
more subtle going on than a simple boolean answer:
- the 'multisel' field in dialog.h's list box structure, for which
the GTK front end in particular recognises a difference between 1
and 2 but nearly everything else treats as boolean
- the 'urgent' parameter to plug_receive, where 1 vs 2 tells you
something about the specific location of the urgent pointer, but
most clients only care about 0 vs 'something nonzero'
- the return value of wc_match, where -1 indicates a syntax error in
the wildcard.
- the return values from SSH-1 RSA-key loading functions, which use
-1 for 'wrong passphrase' and 0 for all other failures (so any
caller which already knows it's not loading an _encrypted private_
key can treat them as boolean)
- term->esc_query, and the 'query' parameter in toggle_mode in
terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h,
but can also hold -1 for some other intervening character that we
don't support.
In a few places there's an integer that I haven't turned into a bool
even though it really _can_ only take values 0 or 1 (and, as above,
tried to make the call sites consistent in not calling those values
true and false), on the grounds that I thought it would make it more
confusing to imply that the 0 value was in some sense 'negative' or
bad and the 1 positive or good:
- the return value of plug_accepting uses the POSIXish convention of
0=success and nonzero=error; I think if I made it bool then I'd
also want to reverse its sense, and that's a job for a separate
piece of work.
- the 'screen' parameter to lineptr() in terminal.c, where 0 and 1
represent the default and alternate screens. There's no obvious
reason why one of those should be considered 'true' or 'positive'
or 'success' - they're just indices - so I've left it as int.
ssh_scp_recv had particularly confusing semantics for its previous int
return value: its call sites used '<= 0' to check for error, but it
never actually returned a negative number, just 0 or 1. Now the
function and its call sites agree that it's a bool.
In a couple of places I've renamed variables called 'ret', because I
don't like that name any more - it's unclear whether it means the
return value (in preparation) for the _containing_ function or the
return value received from a subroutine call, and occasionally I've
accidentally used the same variable for both and introduced a bug. So
where one of those got in my way, I've renamed it to 'toret' or 'retd'
(the latter short for 'returned') in line with my usual modern
practice, but I haven't done a thorough job of finding all of them.
Finally, one amusing side effect of doing this is that I've had to
separate quite a few chained assignments. It used to be perfectly fine
to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a
the 'true' defined by stdbool.h, that idiom provokes a warning from
gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
|
|
|
bool ret;
|
2017-01-28 21:56:28 +00:00
|
|
|
|
|
|
|
#if !defined NO_SECURITY
|
2017-02-01 20:42:21 +00:00
|
|
|
ret = really_restrict_process_acl(&error);
|
2017-01-28 21:56:28 +00:00
|
|
|
#else
|
2018-10-29 19:50:29 +00:00
|
|
|
ret = false;
|
2017-01-28 21:56:28 +00:00
|
|
|
error = dupstr("ACL restrictions not compiled into this binary");
|
|
|
|
#endif
|
|
|
|
if (!ret)
|
|
|
|
modalfatalbox("Could not restrict process ACL: %s", error);
|
|
|
|
}
|