1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

windows/utils/registry.c: allow opening reg keys RO.

These handy wrappers on the verbose underlying Win32 registry API have
to lose some expressiveness, and one thing they lost was the ability
to open a registry key without asking for both read and write access.
This meant they couldn't be used for accessing keys not owned by the
calling user.

So far, I've only used them for accessing PuTTY's own saved data,
which means that hasn't been a problem. But I want to use them
elsewhere in an upcoming commit, so I need to fix that.

The obvious thing would be to change the meaning of the existing
'create' boolean flag so that if it's false, we also don't request
write access. The rationale would be that you're either reading or
writing, and if you're writing you want both RW access and to create
keys that don't already exist. But in fact that's not true: you do
want to set create==false and have write access in the case where
you're _deleting_ things from the key (or the whole key). So we really
do need three ways to call the wrapper function.

Rather than add another boolean field to every call site or mess about
with an 'access type' enum, I've taken an in-between route: the
underlying open_regkey_fn *function* takes a 'create' and a 'write'
flag, but at call sites, it's wrapped with a macro anyway (to append
NULL to the variadic argument list), so I've just made three macros
whose names request different access. That makes call sites marginally
_less_ verbose, while still
This commit is contained in:
Simon Tatham
2022-09-14 16:04:14 +01:00
parent 6a1eba054f
commit 7339e00f4a
3 changed files with 31 additions and 28 deletions

View File

@ -5,7 +5,7 @@
#include "putty.h"
HKEY open_regkey_fn(bool create, HKEY hk, const char *path, ...)
HKEY open_regkey_fn(bool create, bool write, HKEY hk, const char *path, ...)
{
HKEY toret = NULL;
bool hk_needs_close = false;
@ -15,14 +15,14 @@ HKEY open_regkey_fn(bool create, HKEY hk, const char *path, ...)
for (; path; path = va_arg(ap, const char *)) {
HKEY hk_sub = NULL;
DWORD access = KEY_READ | (write ? KEY_WRITE : 0);
LONG status;
if (create)
status = RegCreateKeyEx(
hk, path, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_READ | KEY_WRITE, NULL, &hk_sub, NULL);
access, NULL, &hk_sub, NULL);
else
status = RegOpenKeyEx(
hk, path, 0, KEY_READ | KEY_WRITE, &hk_sub);
status = RegOpenKeyEx(hk, path, 0, access, &hk_sub);
if (status != ERROR_SUCCESS)
goto out;
@ -175,7 +175,7 @@ bool put_reg_multi_sz(HKEY key, const char *name, strbuf *str)
char *get_reg_sz_simple(HKEY key, const char *name, const char *leaf)
{
HKEY subkey = open_regkey(false, key, name);
HKEY subkey = open_regkey_ro(key, name);
if (!subkey)
return NULL;
char *toret = get_reg_sz(subkey, leaf);