2012-06-06 05:50:03 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Windows Registry (.reg file format)
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-08-15 01:20:53 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
#include <errno.h>
|
2012-06-06 05:50:03 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-08-29 10:09:38 +03:00
|
|
|
#include <winpr/wtypes.h>
|
2021-06-18 10:58:18 +03:00
|
|
|
#include <winpr/assert.h>
|
2012-06-06 05:50:03 +04:00
|
|
|
#include <winpr/crt.h>
|
2021-05-31 12:42:03 +03:00
|
|
|
#include <winpr/file.h>
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
#include "registry_reg.h"
|
|
|
|
|
2014-08-18 19:22:22 +04:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#define TAG WINPR_TAG("registry")
|
2014-08-18 19:22:22 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
#define WINPR_HKLM_HIVE "/etc/winpr/HKLM.reg"
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
struct reg_data_type
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* tag;
|
2022-01-10 17:33:39 +03:00
|
|
|
size_t length;
|
2012-06-06 05:50:03 +04:00
|
|
|
DWORD type;
|
|
|
|
};
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static struct reg_data_type REG_DATA_TYPE_TABLE[] = { { "\"", 1, REG_SZ },
|
|
|
|
{ "dword:", 6, REG_DWORD },
|
|
|
|
{ "str:\"", 5, REG_SZ },
|
|
|
|
{ "str(2):\"", 8, REG_EXPAND_SZ },
|
|
|
|
{ "str(7):\"", 8, REG_MULTI_SZ },
|
|
|
|
{ "hex:", 4, REG_BINARY },
|
|
|
|
{ "hex(2):\"", 8, REG_EXPAND_SZ },
|
|
|
|
{ "hex(7):\"", 8, REG_MULTI_SZ },
|
2021-12-22 11:32:11 +03:00
|
|
|
{ "hex(b):\"", 8, REG_QWORD } };
|
|
|
|
|
|
|
|
static char* reg_data_type_string(DWORD type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case REG_NONE:
|
|
|
|
return "REG_NONE";
|
|
|
|
case REG_SZ:
|
|
|
|
return "REG_SZ";
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
return "REG_EXPAND_SZ";
|
|
|
|
case REG_BINARY:
|
|
|
|
return "REG_BINARY";
|
|
|
|
case REG_DWORD:
|
|
|
|
return "REG_DWORD";
|
|
|
|
case REG_DWORD_BIG_ENDIAN:
|
|
|
|
return "REG_DWORD_BIG_ENDIAN";
|
|
|
|
case REG_LINK:
|
|
|
|
return "REG_LINK";
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
return "REG_MULTI_SZ";
|
|
|
|
case REG_RESOURCE_LIST:
|
|
|
|
return "REG_RESOURCE_LIST";
|
|
|
|
case REG_FULL_RESOURCE_DESCRIPTOR:
|
|
|
|
return "REG_FULL_RESOURCE_DESCRIPTOR";
|
|
|
|
case REG_RESOURCE_REQUIREMENTS_LIST:
|
|
|
|
return "REG_RESOURCE_REQUIREMENTS_LIST";
|
|
|
|
case REG_QWORD:
|
|
|
|
return "REG_QWORD";
|
|
|
|
default:
|
|
|
|
return "REG_UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 05:50:03 +04:00
|
|
|
|
2022-01-10 17:33:39 +03:00
|
|
|
static BOOL reg_load_start(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2022-01-10 17:33:39 +03:00
|
|
|
char* buffer;
|
2017-08-11 11:07:46 +03:00
|
|
|
INT64 file_size;
|
2022-01-10 17:33:39 +03:00
|
|
|
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
2022-01-10 17:33:39 +03:00
|
|
|
WINPR_ASSERT(reg->fp);
|
|
|
|
|
2017-08-11 11:07:46 +03:00
|
|
|
_fseeki64(reg->fp, 0, SEEK_END);
|
|
|
|
file_size = _ftelli64(reg->fp);
|
|
|
|
_fseeki64(reg->fp, 0, SEEK_SET);
|
2013-08-30 18:29:25 +04:00
|
|
|
reg->line = NULL;
|
|
|
|
reg->next_line = NULL;
|
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
if (file_size < 1)
|
2022-01-10 17:33:39 +03:00
|
|
|
return FALSE;
|
2012-06-06 05:50:03 +04:00
|
|
|
|
2022-01-10 17:33:39 +03:00
|
|
|
buffer = (char*)realloc(reg->buffer, (size_t)file_size + 2);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2022-01-10 17:33:39 +03:00
|
|
|
if (!buffer)
|
|
|
|
return FALSE;
|
|
|
|
reg->buffer = buffer;
|
2012-06-06 05:50:03 +04:00
|
|
|
|
2021-06-16 15:43:07 +03:00
|
|
|
if (fread(reg->buffer, (size_t)file_size, 1, reg->fp) != 1)
|
2022-01-10 17:33:39 +03:00
|
|
|
return FALSE;
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
reg->buffer[file_size] = '\n';
|
|
|
|
reg->buffer[file_size + 1] = '\0';
|
|
|
|
reg->next_line = strtok(reg->buffer, "\n");
|
2022-01-10 17:33:39 +03:00
|
|
|
return TRUE;
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void reg_load_finish(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2013-08-30 18:29:25 +04:00
|
|
|
if (!reg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (reg->buffer)
|
|
|
|
{
|
|
|
|
free(reg->buffer);
|
|
|
|
reg->buffer = NULL;
|
|
|
|
}
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
2022-02-15 15:01:52 +03:00
|
|
|
static RegVal* reg_load_value(const Reg* reg, RegKey* key)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2022-01-10 17:33:39 +03:00
|
|
|
size_t index;
|
2022-02-15 15:01:52 +03:00
|
|
|
const char* p[5] = { 0 };
|
2021-06-18 10:58:18 +03:00
|
|
|
size_t length;
|
2021-12-22 11:32:11 +03:00
|
|
|
char* name = NULL;
|
2022-02-15 15:01:52 +03:00
|
|
|
const char* type;
|
|
|
|
const char* data;
|
2021-12-22 11:32:11 +03:00
|
|
|
RegVal* value = NULL;
|
|
|
|
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
|
|
|
WINPR_ASSERT(key);
|
2022-01-10 17:33:39 +03:00
|
|
|
WINPR_ASSERT(reg->line);
|
2021-12-22 11:32:11 +03:00
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
p[0] = reg->line + 1;
|
|
|
|
p[1] = strstr(p[0], "\"=");
|
2022-01-10 17:33:39 +03:00
|
|
|
if (!p[1])
|
|
|
|
return NULL;
|
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
p[2] = p[1] + 2;
|
|
|
|
type = p[2];
|
|
|
|
|
|
|
|
if (p[2][0] == '"')
|
|
|
|
p[3] = p[2];
|
|
|
|
else
|
|
|
|
p[3] = strchr(p[2], ':');
|
|
|
|
|
2022-01-10 17:33:39 +03:00
|
|
|
if (!p[3])
|
|
|
|
return NULL;
|
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
data = p[3] + 1;
|
2022-01-10 17:33:39 +03:00
|
|
|
length = (size_t)(p[1] - p[0]);
|
2021-12-22 11:32:11 +03:00
|
|
|
if (length < 1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
name = (char*)calloc(length + 1, sizeof(char));
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!name)
|
2021-12-22 11:32:11 +03:00
|
|
|
goto fail;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
memcpy(name, p[0], length);
|
2021-12-22 11:32:11 +03:00
|
|
|
value = (RegVal*)calloc(1, sizeof(RegVal));
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!value)
|
2021-12-22 11:32:11 +03:00
|
|
|
goto fail;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
value->name = name;
|
|
|
|
value->type = REG_NONE;
|
|
|
|
|
2021-12-22 11:32:11 +03:00
|
|
|
for (index = 0; index < ARRAYSIZE(REG_DATA_TYPE_TABLE); index++)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2021-12-22 11:32:11 +03:00
|
|
|
const struct reg_data_type* current = ®_DATA_TYPE_TABLE[index];
|
|
|
|
WINPR_ASSERT(current->tag);
|
|
|
|
WINPR_ASSERT(current->length > 0);
|
|
|
|
WINPR_ASSERT(current->type != REG_NONE);
|
|
|
|
|
|
|
|
if (strncmp(type, current->tag, current->length) == 0)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2021-12-22 11:32:11 +03:00
|
|
|
value->type = current->type;
|
2012-06-06 05:50:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 11:32:11 +03:00
|
|
|
switch (value->type)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2021-12-22 11:32:11 +03:00
|
|
|
case REG_DWORD:
|
2017-11-14 18:10:52 +03:00
|
|
|
{
|
2021-12-22 11:32:11 +03:00
|
|
|
unsigned long val;
|
|
|
|
errno = 0;
|
2022-02-15 15:01:52 +03:00
|
|
|
val = strtoul(data, NULL, 0);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2021-12-22 11:32:11 +03:00
|
|
|
if ((errno != 0) || (val > UINT32_MAX))
|
2022-02-15 15:01:52 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "%s::%s value %s invalid", key->name, value->name, data);
|
2021-12-22 11:32:11 +03:00
|
|
|
goto fail;
|
2022-02-15 15:01:52 +03:00
|
|
|
}
|
2022-02-24 14:15:11 +03:00
|
|
|
value->data.dword = (DWORD)val;
|
2022-02-22 17:32:20 +03:00
|
|
|
}
|
2022-06-23 08:57:38 +03:00
|
|
|
break;
|
|
|
|
case REG_QWORD:
|
|
|
|
{
|
|
|
|
unsigned long long val;
|
|
|
|
errno = 0;
|
|
|
|
val = strtoull(data, NULL, 0);
|
2022-02-15 15:01:52 +03:00
|
|
|
|
2022-06-23 08:57:38 +03:00
|
|
|
if ((errno != 0) || (val > UINT64_MAX))
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "%s::%s value %s invalid", key->name, value->name, data);
|
|
|
|
goto fail;
|
2022-02-15 15:01:52 +03:00
|
|
|
}
|
2022-06-23 08:57:38 +03:00
|
|
|
|
|
|
|
value->data.qword = (UINT64)val;
|
|
|
|
}
|
2021-12-22 11:32:11 +03:00
|
|
|
break;
|
|
|
|
case REG_SZ:
|
2015-06-17 23:08:02 +03:00
|
|
|
{
|
2021-12-22 11:32:11 +03:00
|
|
|
size_t len, cmp;
|
|
|
|
char* end;
|
|
|
|
char* start = strchr(data, '"');
|
|
|
|
if (!start)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Check for terminating quote, check it is the last symbol */
|
|
|
|
len = strlen(start);
|
|
|
|
end = strchr(start + 1, '"');
|
|
|
|
if (!end)
|
|
|
|
goto fail;
|
|
|
|
cmp = end - start + 1;
|
|
|
|
if (len != cmp)
|
|
|
|
goto fail;
|
|
|
|
if (start[0] == '"')
|
|
|
|
start++;
|
|
|
|
if (end[0] == '"')
|
|
|
|
end[0] = '\0';
|
|
|
|
value->data.string = _strdup(start);
|
|
|
|
|
|
|
|
if (!value->data.string)
|
|
|
|
goto fail;
|
2015-06-17 23:08:02 +03:00
|
|
|
}
|
2021-12-22 11:32:11 +03:00
|
|
|
break;
|
|
|
|
default:
|
2022-02-15 15:01:52 +03:00
|
|
|
WLog_ERR(TAG, "[%s] %s unimplemented format: %s", key->name, value->name,
|
2022-06-23 08:57:38 +03:00
|
|
|
reg_data_type_string(value->type));
|
2021-12-22 11:32:11 +03:00
|
|
|
break;
|
2022-06-23 08:57:38 +03:00
|
|
|
}
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
if (!key->values)
|
|
|
|
{
|
|
|
|
key->values = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
RegVal* pValue = key->values;
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
while (pValue->next != NULL)
|
|
|
|
{
|
|
|
|
pValue = pValue->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pValue->next = value;
|
|
|
|
value->prev = pValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2021-12-22 11:32:11 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(value);
|
|
|
|
free(name);
|
|
|
|
return NULL;
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static BOOL reg_load_has_next_line(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2013-08-29 17:30:22 +04:00
|
|
|
if (!reg)
|
2013-08-30 18:29:25 +04:00
|
|
|
return 0;
|
2013-08-29 17:30:22 +04:00
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
return (reg->next_line != NULL) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static char* reg_load_get_next_line(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2013-08-30 18:29:25 +04:00
|
|
|
if (!reg)
|
|
|
|
return NULL;
|
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
reg->line = reg->next_line;
|
|
|
|
reg->next_line = strtok(NULL, "\n");
|
|
|
|
reg->line_length = strlen(reg->line);
|
|
|
|
return reg->line;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static char* reg_load_peek_next_line(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
2012-06-06 05:50:03 +04:00
|
|
|
return reg->next_line;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void reg_insert_key(Reg* reg, RegKey* key, RegKey* subkey)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2022-01-10 18:09:57 +03:00
|
|
|
char* name = NULL;
|
|
|
|
char* path = NULL;
|
|
|
|
char* save = NULL;
|
2022-01-10 17:33:39 +03:00
|
|
|
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
|
|
|
WINPR_ASSERT(key);
|
|
|
|
WINPR_ASSERT(subkey);
|
2022-01-10 17:33:39 +03:00
|
|
|
WINPR_ASSERT(subkey->name);
|
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
path = _strdup(subkey->name);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!path)
|
|
|
|
return;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-07-30 23:21:57 +04:00
|
|
|
name = strtok_s(path, "\\", &save);
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
while (name != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp(key->name, name) == 0)
|
|
|
|
{
|
2022-01-10 18:09:57 +03:00
|
|
|
if (save)
|
|
|
|
subkey->subname = _strdup(save);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
/* TODO: free allocated memory in error case */
|
|
|
|
if (!subkey->subname)
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
return;
|
|
|
|
}
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 23:21:57 +04:00
|
|
|
name = strtok_s(NULL, "\\", &save);
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static RegKey* reg_load_key(Reg* reg, RegKey* key)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* p[2];
|
2022-01-10 17:33:39 +03:00
|
|
|
size_t length;
|
2014-08-18 21:34:47 +04:00
|
|
|
RegKey* subkey;
|
2022-01-10 17:33:39 +03:00
|
|
|
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
|
|
|
WINPR_ASSERT(key);
|
2022-01-10 17:33:39 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(reg->line);
|
2012-06-06 05:50:03 +04:00
|
|
|
p[0] = reg->line + 1;
|
|
|
|
p[1] = strrchr(p[0], ']');
|
2022-01-10 17:33:39 +03:00
|
|
|
if (!p[1])
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
subkey = (RegKey*)calloc(1, sizeof(RegKey));
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!subkey)
|
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2022-01-10 17:33:39 +03:00
|
|
|
length = (size_t)(p[1] - p[0]);
|
2019-11-06 17:24:51 +03:00
|
|
|
subkey->name = (char*)malloc(length + 1);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!subkey->name)
|
|
|
|
{
|
|
|
|
free(subkey);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
memcpy(subkey->name, p[0], length);
|
|
|
|
subkey->name[length] = '\0';
|
|
|
|
|
|
|
|
while (reg_load_has_next_line(reg))
|
|
|
|
{
|
2022-01-10 17:33:39 +03:00
|
|
|
char* line = reg_load_peek_next_line(reg);
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
if (line[0] == '[')
|
|
|
|
break;
|
|
|
|
|
|
|
|
reg_load_get_next_line(reg);
|
|
|
|
|
|
|
|
if (reg->line[0] == '"')
|
|
|
|
{
|
|
|
|
reg_load_value(reg, subkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reg_insert_key(reg, key, subkey);
|
|
|
|
|
|
|
|
if (!key->subkeys)
|
|
|
|
{
|
|
|
|
key->subkeys = subkey;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
RegKey* pKey = key->subkeys;
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
while (pKey->next != NULL)
|
|
|
|
{
|
|
|
|
pKey = pKey->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pKey->next = subkey;
|
|
|
|
subkey->prev = pKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return subkey;
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static void reg_load(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
|
|
|
reg_load_start(reg);
|
|
|
|
|
|
|
|
while (reg_load_has_next_line(reg))
|
|
|
|
{
|
|
|
|
reg_load_get_next_line(reg);
|
|
|
|
|
|
|
|
if (reg->line[0] == '[')
|
|
|
|
{
|
|
|
|
reg_load_key(reg, reg->root_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reg_load_finish(reg);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void reg_unload_value(Reg* reg, RegVal* value)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
|
|
|
WINPR_ASSERT(value);
|
2022-01-10 17:33:39 +03:00
|
|
|
|
|
|
|
switch (value->type)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2022-01-10 17:33:39 +03:00
|
|
|
case REG_SZ:
|
|
|
|
free(value->data.string);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void reg_unload_key(Reg* reg, RegKey* key)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
RegVal* pValue;
|
2022-01-10 17:33:39 +03:00
|
|
|
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
|
|
|
WINPR_ASSERT(key);
|
2022-01-10 17:33:39 +03:00
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
pValue = key->values;
|
|
|
|
|
|
|
|
while (pValue != NULL)
|
|
|
|
{
|
2022-01-10 17:33:39 +03:00
|
|
|
RegVal* pValueNext = pValue->next;
|
2012-06-06 05:50:03 +04:00
|
|
|
reg_unload_value(reg, pValue);
|
|
|
|
pValue = pValueNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(key->name);
|
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static void reg_unload(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2021-06-18 10:58:18 +03:00
|
|
|
WINPR_ASSERT(reg);
|
2022-01-10 19:04:49 +03:00
|
|
|
if (reg->root_key)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2023-06-05 13:16:57 +03:00
|
|
|
RegKey* pKey = reg->root_key->subkeys;
|
2022-01-10 19:04:49 +03:00
|
|
|
|
|
|
|
while (pKey != NULL)
|
|
|
|
{
|
|
|
|
RegKey* pKeyNext = pKey->next;
|
|
|
|
reg_unload_key(reg, pKey);
|
|
|
|
pKey = pKeyNext;
|
|
|
|
}
|
2012-06-06 05:50:03 +04:00
|
|
|
|
2022-01-10 19:04:49 +03:00
|
|
|
free(reg->root_key);
|
|
|
|
}
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
Reg* reg_open(BOOL read_only)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2022-01-10 17:33:39 +03:00
|
|
|
Reg* reg = (Reg*)calloc(1, sizeof(Reg));
|
2012-06-06 05:50:03 +04:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!reg)
|
|
|
|
return NULL;
|
2012-06-16 01:06:26 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
reg->read_only = read_only;
|
|
|
|
reg->filename = WINPR_HKLM_HIVE;
|
2015-03-23 19:25:23 +03:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
if (reg->read_only)
|
2021-05-31 12:42:03 +03:00
|
|
|
reg->fp = winpr_fopen(reg->filename, "r");
|
2017-11-14 18:10:52 +03:00
|
|
|
else
|
|
|
|
{
|
2021-05-31 12:42:03 +03:00
|
|
|
reg->fp = winpr_fopen(reg->filename, "r+");
|
2017-11-14 18:10:52 +03:00
|
|
|
|
|
|
|
if (!reg->fp)
|
2021-05-31 12:42:03 +03:00
|
|
|
reg->fp = winpr_fopen(reg->filename, "w+");
|
2017-11-14 18:10:52 +03:00
|
|
|
}
|
2015-03-23 19:25:23 +03:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
if (!reg->fp)
|
2022-01-10 17:33:39 +03:00
|
|
|
goto fail;
|
2015-03-23 19:25:23 +03:00
|
|
|
|
2022-01-10 17:33:39 +03:00
|
|
|
reg->root_key = (RegKey*)calloc(1, sizeof(RegKey));
|
2015-03-23 19:25:23 +03:00
|
|
|
|
|
|
|
if (!reg->root_key)
|
2022-01-10 17:33:39 +03:00
|
|
|
goto fail;
|
2012-06-06 05:50:03 +04:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
reg->root_key->values = NULL;
|
|
|
|
reg->root_key->subkeys = NULL;
|
|
|
|
reg->root_key->name = "HKEY_LOCAL_MACHINE";
|
|
|
|
reg_load(reg);
|
2012-06-06 05:50:03 +04:00
|
|
|
return reg;
|
2022-01-10 17:33:39 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
reg_close(reg);
|
|
|
|
return NULL;
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void reg_close(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
|
|
|
if (reg)
|
|
|
|
{
|
|
|
|
reg_unload(reg);
|
2022-01-10 19:13:24 +03:00
|
|
|
if (reg->fp)
|
|
|
|
fclose(reg->fp);
|
2012-06-06 05:50:03 +04:00
|
|
|
free(reg);
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 15:01:52 +03:00
|
|
|
|
|
|
|
const char* reg_type_string(DWORD type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case REG_NONE:
|
|
|
|
return "REG_NONE";
|
|
|
|
case REG_SZ:
|
|
|
|
return "REG_SZ";
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
return "REG_EXPAND_SZ";
|
|
|
|
case REG_BINARY:
|
|
|
|
return "REG_BINARY";
|
|
|
|
case REG_DWORD:
|
|
|
|
return "REG_DWORD";
|
|
|
|
case REG_DWORD_BIG_ENDIAN:
|
|
|
|
return "REG_DWORD_BIG_ENDIAN";
|
|
|
|
case REG_LINK:
|
|
|
|
return "REG_LINK";
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
return "REG_MULTI_SZ";
|
|
|
|
case REG_RESOURCE_LIST:
|
|
|
|
return "REG_RESOURCE_LIST";
|
|
|
|
case REG_FULL_RESOURCE_DESCRIPTOR:
|
|
|
|
return "REG_FULL_RESOURCE_DESCRIPTOR";
|
|
|
|
case REG_RESOURCE_REQUIREMENTS_LIST:
|
|
|
|
return "REG_RESOURCE_REQUIREMENTS_LIST";
|
|
|
|
case REG_QWORD:
|
|
|
|
return "REG_QWORD";
|
|
|
|
default:
|
|
|
|
return "REG_UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|