2021-03-17 13:38:05 +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 "reg.h"
|
|
|
|
|
2021-05-12 14:24:23 +02:00
|
|
|
static BOOL op_delete_key = TRUE;
|
|
|
|
|
|
|
|
static void output_error(LONG rc)
|
|
|
|
{
|
|
|
|
if (rc == ERROR_FILE_NOT_FOUND)
|
|
|
|
{
|
|
|
|
if (op_delete_key)
|
|
|
|
output_message(STRING_KEY_NONEXIST);
|
|
|
|
else
|
|
|
|
output_message(STRING_VALUE_NONEXIST);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output_message(STRING_ACCESS_DENIED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:57:46 +02:00
|
|
|
static int run_delete(HKEY root, WCHAR *path, REGSAM sam, WCHAR *key_name, WCHAR *value_name,
|
2021-03-30 14:00:12 +02:00
|
|
|
BOOL value_empty, BOOL value_all, BOOL force)
|
2021-03-17 13:38:05 +01:00
|
|
|
{
|
2021-05-12 14:24:23 +02:00
|
|
|
LONG rc;
|
2021-04-28 11:19:28 +02:00
|
|
|
HKEY hkey;
|
2021-03-17 13:38:05 +01:00
|
|
|
|
|
|
|
if (!force)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if (value_name || value_empty)
|
|
|
|
ret = ask_confirm(STRING_DELETE_VALUE, value_name);
|
|
|
|
else if (value_all)
|
|
|
|
ret = ask_confirm(STRING_DELETE_VALUEALL, key_name);
|
|
|
|
else
|
|
|
|
ret = ask_confirm(STRING_DELETE_SUBKEY, key_name);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
output_message(STRING_CANCELLED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 14:57:47 +02:00
|
|
|
if ((rc = RegOpenKeyExW(root, path, 0, KEY_READ|KEY_SET_VALUE|sam, &hkey)))
|
|
|
|
{
|
|
|
|
output_error(rc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:24:32 +02:00
|
|
|
/* Delete registry key if no /v* option is given */
|
2021-03-17 13:38:05 +01:00
|
|
|
if (!value_name && !value_empty && !value_all)
|
|
|
|
{
|
2021-06-28 14:57:47 +02:00
|
|
|
if ((rc = RegDeleteTreeW(hkey, NULL)))
|
2021-03-17 13:38:05 +01:00
|
|
|
{
|
2021-06-28 14:57:47 +02:00
|
|
|
RegCloseKey(hkey);
|
2021-05-12 14:24:23 +02:00
|
|
|
output_error(rc);
|
2021-03-17 13:38:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2021-05-12 14:24:23 +02:00
|
|
|
|
2021-06-28 14:57:47 +02:00
|
|
|
RegDeleteKeyW(hkey, L"");
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
|
2021-03-17 13:38:05 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:24:23 +02:00
|
|
|
op_delete_key = FALSE;
|
|
|
|
|
2021-03-17 13:38:05 +01:00
|
|
|
if (value_all)
|
|
|
|
{
|
|
|
|
DWORD max_value_len = 256, value_len;
|
|
|
|
WCHAR *value_name;
|
|
|
|
|
2021-03-24 09:08:58 +01:00
|
|
|
value_name = malloc(max_value_len * sizeof(WCHAR));
|
2021-03-17 13:38:05 +01:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
value_len = max_value_len;
|
2021-04-28 11:19:28 +02:00
|
|
|
rc = RegEnumValueW(hkey, 0, value_name, &value_len, NULL, NULL, NULL, NULL);
|
2021-03-17 13:38:05 +01:00
|
|
|
if (rc == ERROR_SUCCESS)
|
|
|
|
{
|
2021-04-28 11:19:28 +02:00
|
|
|
rc = RegDeleteValueW(hkey, value_name);
|
2021-03-17 13:38:05 +01:00
|
|
|
if (rc != ERROR_SUCCESS)
|
|
|
|
{
|
2021-03-24 09:08:58 +01:00
|
|
|
free(value_name);
|
2021-04-28 11:19:28 +02:00
|
|
|
RegCloseKey(hkey);
|
2021-03-17 13:38:05 +01:00
|
|
|
output_message(STRING_VALUEALL_FAILED, key_name);
|
2021-05-12 14:24:23 +02:00
|
|
|
output_error(rc);
|
2021-03-17 13:38:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rc == ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
max_value_len *= 2;
|
2021-03-24 09:08:58 +01:00
|
|
|
value_name = realloc(value_name, max_value_len * sizeof(WCHAR));
|
2021-03-17 13:38:05 +01:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
2021-03-24 09:08:58 +01:00
|
|
|
free(value_name);
|
2021-03-17 13:38:05 +01:00
|
|
|
}
|
|
|
|
else if (value_name || value_empty)
|
|
|
|
{
|
2021-05-12 14:24:23 +02:00
|
|
|
if ((rc = RegDeleteValueW(hkey, value_name)))
|
2021-03-17 13:38:05 +01:00
|
|
|
{
|
2021-04-28 11:19:28 +02:00
|
|
|
RegCloseKey(hkey);
|
2021-05-12 14:24:23 +02:00
|
|
|
output_error(rc);
|
2021-03-17 13:38:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 11:19:28 +02:00
|
|
|
RegCloseKey(hkey);
|
2021-03-17 13:38:05 +01:00
|
|
|
output_message(STRING_SUCCESS);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-30 14:00:12 +02:00
|
|
|
|
|
|
|
int reg_delete(int argc, WCHAR *argvW[])
|
|
|
|
{
|
|
|
|
HKEY root;
|
|
|
|
WCHAR *path, *key_name, *value_name = NULL;
|
|
|
|
BOOL value_all = FALSE, value_empty = FALSE, force = FALSE;
|
2021-06-28 14:57:46 +02:00
|
|
|
REGSAM sam = 0;
|
2021-03-30 14:00:12 +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:12 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (i = 3; i < argc; i++)
|
|
|
|
{
|
2021-04-02 13:26:54 +02:00
|
|
|
WCHAR *str;
|
2021-03-30 14:00:12 +02:00
|
|
|
|
2021-04-02 13:26:54 +02:00
|
|
|
if (argvW[i][0] != '/' && argvW[i][0] != '-')
|
|
|
|
goto invalid;
|
2021-03-30 14:00:12 +02:00
|
|
|
|
2021-04-02 13:26:54 +02:00
|
|
|
str = &argvW[i][1];
|
|
|
|
|
|
|
|
if (!lstrcmpiW(str, L"va"))
|
|
|
|
{
|
|
|
|
if (value_all) goto invalid;
|
|
|
|
value_all = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!lstrcmpiW(str, L"ve"))
|
|
|
|
{
|
|
|
|
if (value_empty) goto invalid;
|
|
|
|
value_empty = TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-28 14:57:46 +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-28 14:57:46 +02:00
|
|
|
}
|
2021-04-02 13:26:54 +02:00
|
|
|
else if (!str[0] || str[1])
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
switch (towlower(*str))
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
if (value_name || !(value_name = argvW[++i]))
|
2021-03-30 14:00:12 +02:00
|
|
|
goto invalid;
|
2021-04-02 13:26:54 +02:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (force) goto invalid;
|
|
|
|
force = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto invalid;
|
2021-03-30 14:00:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((value_name && value_empty) || (value_name && value_all) || (value_empty && value_all))
|
|
|
|
goto invalid;
|
|
|
|
|
2021-06-28 14:57:46 +02:00
|
|
|
if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
|
|
|
|
goto invalid;
|
|
|
|
|
2021-04-05 15:24:11 +02:00
|
|
|
key_name = get_long_key(root, path);
|
|
|
|
|
2021-06-28 14:57:46 +02:00
|
|
|
return run_delete(root, path, sam, key_name, value_name, value_empty, value_all, force);
|
2021-03-30 14:00:12 +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:12 +02:00
|
|
|
return 1;
|
|
|
|
}
|