2021-03-17 13:38:06 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2016-2017, 2021 Hugh McMaster
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include "reg.h"
|
|
|
|
|
|
|
|
static DWORD wchar_get_type(const WCHAR *type_name)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
if (!type_name)
|
|
|
|
return REG_SZ;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(type_rels); i++)
|
|
|
|
{
|
|
|
|
if (!wcsicmp(type_rels[i].name, type_name))
|
|
|
|
return type_rels[i].type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ~0u;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hexchar_to_byte from programs/regedit/hexedit.c */
|
|
|
|
static inline BYTE hexchar_to_byte(WCHAR ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9')
|
|
|
|
return ch - '0';
|
|
|
|
else if (ch >= 'a' && ch <= 'f')
|
|
|
|
return ch - 'a' + 10;
|
|
|
|
else if (ch >= 'A' && ch <= 'F')
|
|
|
|
return ch - 'A' + 10;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:47:53 +02:00
|
|
|
static BOOL get_regdata(const WCHAR *data, DWORD reg_type, WCHAR separator,
|
|
|
|
BYTE **data_bytes, DWORD *size_bytes)
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
|
|
|
static const WCHAR empty;
|
2021-04-30 14:39:49 +02:00
|
|
|
|
|
|
|
*size_bytes = 0;
|
2021-03-17 13:38:06 +01:00
|
|
|
|
|
|
|
if (!data) data = ∅
|
|
|
|
|
|
|
|
switch (reg_type)
|
|
|
|
{
|
|
|
|
case REG_NONE:
|
|
|
|
case REG_SZ:
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
{
|
2021-04-30 14:39:49 +02:00
|
|
|
*size_bytes = (lstrlenW(data) + 1) * sizeof(WCHAR);
|
2021-05-06 14:47:53 +02:00
|
|
|
*data_bytes = malloc(*size_bytes);
|
|
|
|
lstrcpyW((WCHAR *)*data_bytes, data);
|
2021-03-17 13:38:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REG_DWORD:
|
|
|
|
/* case REG_DWORD_LITTLE_ENDIAN: */
|
|
|
|
case REG_DWORD_BIG_ENDIAN: /* Yes, this is correct! */
|
|
|
|
{
|
|
|
|
LPWSTR rest;
|
|
|
|
unsigned long val;
|
|
|
|
val = wcstoul(data, &rest, (towlower(data[1]) == 'x') ? 16 : 10);
|
|
|
|
if (*rest || data[0] == '-' || (val == ~0u && errno == ERANGE)) {
|
2021-05-12 14:24:25 +02:00
|
|
|
output_message(STRING_MISSING_NUMBER);
|
2021-05-06 14:47:53 +02:00
|
|
|
return FALSE;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
2021-04-30 14:39:49 +02:00
|
|
|
*size_bytes = sizeof(DWORD);
|
2021-05-06 14:47:53 +02:00
|
|
|
*data_bytes = malloc(*size_bytes);
|
|
|
|
*(DWORD *)*data_bytes = val;
|
2021-03-17 13:38:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REG_BINARY:
|
|
|
|
{
|
2021-05-06 14:47:53 +02:00
|
|
|
BYTE hex0, hex1, *ptr;
|
2021-03-17 13:38:06 +01:00
|
|
|
int i = 0, destByteIndex = 0, datalen = lstrlenW(data);
|
2021-05-06 14:47:53 +02:00
|
|
|
|
2021-05-06 14:47:54 +02:00
|
|
|
if (!datalen) return TRUE;
|
|
|
|
|
2021-04-30 14:39:49 +02:00
|
|
|
*size_bytes = ((datalen + datalen % 2) / 2) * sizeof(BYTE);
|
2021-05-06 14:47:53 +02:00
|
|
|
*data_bytes = malloc(*size_bytes);
|
|
|
|
|
|
|
|
if (datalen % 2)
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
|
|
|
hex1 = hexchar_to_byte(data[i++]);
|
2021-05-06 14:47:53 +02:00
|
|
|
if (hex1 == 0xFF)
|
2021-03-17 13:38:06 +01:00
|
|
|
goto no_hex_data;
|
2021-05-06 14:47:53 +02:00
|
|
|
*data_bytes[destByteIndex++] = hex1;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
2021-05-06 14:47:53 +02:00
|
|
|
|
|
|
|
ptr = *data_bytes;
|
|
|
|
|
|
|
|
for (; i + 1 < datalen; i += 2)
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
|
|
|
hex0 = hexchar_to_byte(data[i]);
|
|
|
|
hex1 = hexchar_to_byte(data[i + 1]);
|
2021-05-06 14:47:53 +02:00
|
|
|
if (hex0 == 0xFF || hex1 == 0xFF)
|
2021-03-17 13:38:06 +01:00
|
|
|
goto no_hex_data;
|
2021-05-06 14:47:53 +02:00
|
|
|
ptr[destByteIndex++] = (hex0 << 4) | hex1;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-05-06 14:47:53 +02:00
|
|
|
|
2021-03-17 13:38:06 +01:00
|
|
|
no_hex_data:
|
2021-05-06 14:47:53 +02:00
|
|
|
free(*data_bytes);
|
|
|
|
*data_bytes = NULL;
|
2021-03-17 13:38:06 +01:00
|
|
|
output_message(STRING_MISSING_HEXDATA);
|
2021-05-06 14:47:53 +02:00
|
|
|
return FALSE;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
{
|
|
|
|
int i, destindex, len = lstrlenW(data);
|
2021-03-24 09:08:58 +01:00
|
|
|
WCHAR *buffer = malloc((len + 2) * sizeof(WCHAR));
|
2021-03-17 13:38:06 +01:00
|
|
|
|
|
|
|
for (i = 0, destindex = 0; i < len; i++, destindex++)
|
|
|
|
{
|
|
|
|
if (!separator && data[i] == '\\' && data[i + 1] == '0')
|
|
|
|
{
|
|
|
|
buffer[destindex] = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (data[i] == separator)
|
|
|
|
buffer[destindex] = 0;
|
|
|
|
else
|
|
|
|
buffer[destindex] = data[i];
|
|
|
|
|
|
|
|
if (destindex && !buffer[destindex - 1] && (!buffer[destindex] || destindex == 1))
|
|
|
|
{
|
2021-03-24 09:08:58 +01:00
|
|
|
free(buffer);
|
2021-03-17 13:38:06 +01:00
|
|
|
output_message(STRING_INVALID_STRING);
|
2021-05-06 14:47:53 +02:00
|
|
|
return FALSE;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer[destindex] = 0;
|
|
|
|
if (destindex && buffer[destindex - 1])
|
|
|
|
buffer[++destindex] = 0;
|
2021-04-30 14:39:49 +02:00
|
|
|
*size_bytes = (destindex + 1) * sizeof(WCHAR);
|
2021-05-06 14:47:53 +02:00
|
|
|
*data_bytes = (BYTE *)buffer;
|
|
|
|
break;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
output_message(STRING_UNHANDLED_TYPE, reg_type, data);
|
|
|
|
}
|
|
|
|
|
2021-05-06 14:47:53 +02:00
|
|
|
return TRUE;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
|
|
|
|
2021-06-18 14:31:57 +02:00
|
|
|
static int run_add(HKEY root, WCHAR *path, REGSAM sam, WCHAR *value_name, BOOL value_empty,
|
2021-03-30 14:00:13 +02:00
|
|
|
WCHAR *type, WCHAR separator, WCHAR *data, BOOL force)
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
2021-04-28 11:19:28 +02:00
|
|
|
HKEY hkey;
|
2021-05-03 15:20:37 +02:00
|
|
|
DWORD dispos, data_type, data_size;
|
2021-05-03 15:20:35 +02:00
|
|
|
BYTE *reg_data = NULL;
|
2021-05-03 15:20:36 +02:00
|
|
|
LONG rc;
|
2021-03-17 13:38:06 +01:00
|
|
|
|
2021-03-30 14:00:15 +02:00
|
|
|
if (RegCreateKeyExW(root, path, 0, NULL, REG_OPTION_NON_VOLATILE,
|
2021-06-18 14:31:57 +02:00
|
|
|
KEY_READ|KEY_WRITE|sam, NULL, &hkey, &dispos))
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
2021-04-29 15:31:47 +02:00
|
|
|
output_message(STRING_ACCESS_DENIED);
|
2021-03-17 13:38:06 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-03 15:20:37 +02:00
|
|
|
if (!force && dispos == REG_OPENED_EXISTING_KEY)
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
2021-05-03 15:20:35 +02:00
|
|
|
if (RegQueryValueExW(hkey, value_name, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
2021-05-03 15:20:35 +02:00
|
|
|
if (!ask_confirm(STRING_OVERWRITE_VALUE, value_name))
|
2021-03-17 13:38:06 +01:00
|
|
|
{
|
2021-05-03 15:20:35 +02:00
|
|
|
RegCloseKey(hkey);
|
|
|
|
output_message(STRING_CANCELLED);
|
|
|
|
return 0;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-03 15:20:35 +02:00
|
|
|
}
|
2021-03-17 13:38:06 +01:00
|
|
|
|
2021-05-03 15:20:35 +02:00
|
|
|
data_type = wchar_get_type(type);
|
2021-03-17 13:38:06 +01:00
|
|
|
|
2021-05-03 15:20:35 +02:00
|
|
|
if (data_type == ~0u)
|
|
|
|
{
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
output_message(STRING_UNSUPPORTED_TYPE, type);
|
|
|
|
return 1;
|
|
|
|
}
|
2021-03-17 13:38:06 +01:00
|
|
|
|
2021-05-03 15:20:35 +02:00
|
|
|
if ((data_type == REG_DWORD || data_type == REG_DWORD_BIG_ENDIAN) && !data)
|
|
|
|
{
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
output_message(STRING_INVALID_CMDLINE);
|
|
|
|
return 1;
|
2021-03-17 13:38:06 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 14:47:53 +02:00
|
|
|
if (!get_regdata(data, data_type, separator, ®_data, &data_size))
|
2021-05-03 15:20:35 +02:00
|
|
|
{
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-03 15:20:36 +02:00
|
|
|
rc = RegSetValueExW(hkey, value_name, 0, data_type, reg_data, data_size);
|
2021-05-03 15:20:35 +02:00
|
|
|
|
2021-05-03 15:20:36 +02:00
|
|
|
free(reg_data);
|
2021-04-28 11:19:28 +02:00
|
|
|
RegCloseKey(hkey);
|
2021-05-03 15:20:36 +02:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
output_message(STRING_ACCESS_DENIED);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-17 13:38:06 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-30 14:00:13 +02:00
|
|
|
|
|
|
|
int reg_add(int argc, WCHAR *argvW[])
|
|
|
|
{
|
|
|
|
HKEY root;
|
2021-04-05 15:24:11 +02:00
|
|
|
WCHAR *path, *value_name = NULL, *type = NULL, *data = NULL, separator = '\0';
|
2021-03-30 14:00:13 +02:00
|
|
|
BOOL value_empty = FALSE, force = FALSE;
|
2021-06-18 14:31:57 +02:00
|
|
|
REGSAM sam = 0;
|
2021-03-30 14:00:13 +02:00
|
|
|
int i;
|
|
|
|
|
2021-04-05 15:24:11 +02:00
|
|
|
if (!parse_registry_key(argvW[2], &root, &path))
|
2021-03-30 14:00:13 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (i = 3; i < argc; i++)
|
|
|
|
{
|
2021-04-02 13:26:53 +02:00
|
|
|
WCHAR *str;
|
|
|
|
|
|
|
|
if (argvW[i][0] != '/' && argvW[i][0] != '-')
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
str = &argvW[i][1];
|
|
|
|
|
|
|
|
if (!lstrcmpiW(str, L"ve"))
|
2021-03-30 14:00:13 +02:00
|
|
|
{
|
2021-04-02 13:26:53 +02:00
|
|
|
if (value_empty) goto invalid;
|
|
|
|
value_empty = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-18 14:31:57 +02:00
|
|
|
else if (!lstrcmpiW(str, L"reg:32"))
|
|
|
|
{
|
|
|
|
if (sam & KEY_WOW64_32KEY) goto invalid;
|
|
|
|
sam |= KEY_WOW64_32KEY;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!lstrcmpiW(str, L"reg:64"))
|
|
|
|
{
|
|
|
|
if (sam & KEY_WOW64_64KEY) goto invalid;
|
|
|
|
sam |= KEY_WOW64_64KEY;
|
2021-04-08 14:10:54 +02:00
|
|
|
continue;
|
2021-06-18 14:31:57 +02:00
|
|
|
}
|
2021-04-02 13:26:53 +02:00
|
|
|
else if (!str[0] || str[1])
|
|
|
|
goto invalid;
|
2021-03-30 14:00:13 +02:00
|
|
|
|
2021-04-02 13:26:53 +02:00
|
|
|
switch (towlower(*str))
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
if (value_name || !(value_name = argvW[++i]))
|
2021-03-30 14:00:13 +02:00
|
|
|
goto invalid;
|
2021-04-02 13:26:53 +02:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (type || !(type = argvW[++i]))
|
2021-03-30 14:00:13 +02:00
|
|
|
goto invalid;
|
2021-04-02 13:26:53 +02:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (data || !(data = argvW[++i]))
|
|
|
|
goto invalid;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
str = argvW[++i];
|
|
|
|
if (separator || !str || lstrlenW(str) != 1)
|
|
|
|
goto invalid;
|
|
|
|
separator = str[0];
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (force) goto invalid;
|
|
|
|
force = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto invalid;
|
2021-03-30 14:00:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value_name && value_empty)
|
|
|
|
goto invalid;
|
|
|
|
|
2021-06-18 14:31:57 +02:00
|
|
|
if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
return run_add(root, path, sam, value_name, value_empty, type, separator, data, force);
|
2021-03-30 14:00:13 +02:00
|
|
|
|
|
|
|
invalid:
|
2021-04-08 14:10:53 +02:00
|
|
|
output_message(STRING_INVALID_SYNTAX);
|
|
|
|
output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
|
2021-03-30 14:00:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|