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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
#define WINPR_HKLM_HIVE "/etc/winpr/HKLM.reg"
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void reg_print_key(Reg* reg, RegKey* key);
|
|
|
|
static void reg_print_value(Reg* reg, RegVal* value);
|
2012-07-30 22:52:50 +04:00
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
struct reg_data_type
|
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* tag;
|
2012-06-06 05:50:03 +04:00
|
|
|
int length;
|
|
|
|
DWORD type;
|
|
|
|
};
|
|
|
|
|
2013-08-30 18:29:25 +04:00
|
|
|
static struct reg_data_type REG_DATA_TYPE_TABLE[] =
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
|
|
|
{ "\"", 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 },
|
|
|
|
{ "hex(b):\"", 8, REG_QWORD },
|
|
|
|
{ NULL, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static char* REG_DATA_TYPE_STRINGS[] =
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
|
|
|
"REG_NONE",
|
|
|
|
"REG_SZ",
|
|
|
|
"REG_EXPAND_SZ",
|
|
|
|
"REG_BINARY",
|
|
|
|
"REG_DWORD",
|
|
|
|
"REG_DWORD_BIG_ENDIAN",
|
|
|
|
"REG_LINK",
|
|
|
|
"REG_MULTI_SZ",
|
|
|
|
"REG_RESOURCE_LIST",
|
|
|
|
"REG_FULL_RESOURCE_DESCRIPTOR",
|
|
|
|
"REG_RESOURCE_REQUIREMENTS_LIST",
|
|
|
|
"REG_QWORD"
|
|
|
|
};
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static void reg_load_start(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
|
|
|
long int file_size;
|
|
|
|
fseek(reg->fp, 0, SEEK_END);
|
|
|
|
file_size = ftell(reg->fp);
|
|
|
|
fseek(reg->fp, 0, SEEK_SET);
|
2013-08-30 18:29:25 +04:00
|
|
|
reg->line = NULL;
|
|
|
|
reg->next_line = NULL;
|
|
|
|
reg->buffer = NULL;
|
|
|
|
|
2012-06-06 05:50:03 +04:00
|
|
|
if (file_size < 1)
|
|
|
|
return;
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
reg->buffer = (char*) malloc(file_size + 2);
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!reg->buffer)
|
|
|
|
return ;
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
if (fread(reg->buffer, file_size, 1, reg->fp) != 1)
|
|
|
|
{
|
|
|
|
free(reg->buffer);
|
2013-08-30 18:29:25 +04:00
|
|
|
reg->buffer = NULL;
|
2012-06-06 05:50:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reg->buffer[file_size] = '\n';
|
|
|
|
reg->buffer[file_size + 1] = '\0';
|
|
|
|
reg->next_line = strtok(reg->buffer, "\n");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
static RegVal* reg_load_value(Reg* reg, RegKey* key)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
|
|
|
int index;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* p[5];
|
2012-06-06 05:50:03 +04:00
|
|
|
int length;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* name;
|
|
|
|
char* type;
|
|
|
|
char* data;
|
|
|
|
RegVal* value;
|
2012-06-06 05:50:03 +04:00
|
|
|
p[0] = reg->line + 1;
|
|
|
|
p[1] = strstr(p[0], "\"=");
|
|
|
|
p[2] = p[1] + 2;
|
|
|
|
type = p[2];
|
|
|
|
|
|
|
|
if (p[2][0] == '"')
|
|
|
|
p[3] = p[2];
|
|
|
|
else
|
|
|
|
p[3] = strchr(p[2], ':');
|
|
|
|
|
|
|
|
data = p[3] + 1;
|
|
|
|
length = p[1] - p[0];
|
2014-08-18 21:34:47 +04:00
|
|
|
name = (char*) malloc(length + 1);
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!name)
|
|
|
|
return 0;
|
2012-06-06 05:50:03 +04:00
|
|
|
memcpy(name, p[0], length);
|
|
|
|
name[length] = '\0';
|
2014-08-18 21:34:47 +04:00
|
|
|
value = (RegVal*) malloc(sizeof(RegVal));
|
2012-06-06 05:50:03 +04:00
|
|
|
value->name = name;
|
|
|
|
value->type = REG_NONE;
|
|
|
|
value->next = value->prev = NULL;
|
|
|
|
|
|
|
|
for (index = 0; REG_DATA_TYPE_TABLE[index].length > 0; index++)
|
|
|
|
{
|
|
|
|
if (strncmp(type, REG_DATA_TYPE_TABLE[index].tag, REG_DATA_TYPE_TABLE[index].length) == 0)
|
|
|
|
{
|
|
|
|
value->type = REG_DATA_TYPE_TABLE[index].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value->type == REG_DWORD)
|
|
|
|
{
|
2012-06-16 01:06:26 +04:00
|
|
|
value->data.dword = strtoul(data, NULL, 16);
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
else if (value->type == REG_SZ)
|
|
|
|
{
|
|
|
|
p[4] = strchr(data, '"');
|
|
|
|
p[4][0] = '\0';
|
2012-06-06 10:59:37 +04:00
|
|
|
value->data.string = _strdup(data);
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "unimplemented format: %s", REG_DATA_TYPE_STRINGS[value->type]);
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
char* name;
|
|
|
|
char* path;
|
|
|
|
char* save;
|
2012-06-06 05:50:03 +04:00
|
|
|
int length;
|
|
|
|
path = _strdup(subkey->name);
|
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)
|
|
|
|
{
|
|
|
|
length = strlen(name);
|
|
|
|
name += length + 1;
|
|
|
|
subkey->subname = _strdup(name);
|
|
|
|
}
|
|
|
|
|
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];
|
2012-06-06 05:50:03 +04:00
|
|
|
int length;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* line;
|
|
|
|
RegKey* subkey;
|
2012-06-06 05:50:03 +04:00
|
|
|
p[0] = reg->line + 1;
|
|
|
|
p[1] = strrchr(p[0], ']');
|
2014-08-18 21:34:47 +04:00
|
|
|
subkey = (RegKey*) malloc(sizeof(RegKey));
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!subkey)
|
|
|
|
return NULL;
|
2012-06-06 05:50:03 +04:00
|
|
|
subkey->values = NULL;
|
|
|
|
subkey->prev = subkey->next = NULL;
|
|
|
|
length = p[1] - p[0];
|
2014-08-18 21:34:47 +04:00
|
|
|
subkey->name = (char*) malloc(length + 1);
|
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))
|
|
|
|
{
|
|
|
|
line = reg_load_peek_next_line(reg);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
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
|
|
|
{
|
|
|
|
if (value->type == REG_DWORD)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (value->type == REG_SZ)
|
|
|
|
{
|
|
|
|
free(value->data.string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "unimplemented format: %s", REG_DATA_TYPE_STRINGS[value->type]);
|
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;
|
|
|
|
RegVal* pValueNext;
|
2012-06-06 05:50:03 +04:00
|
|
|
pValue = key->values;
|
|
|
|
|
|
|
|
while (pValue != NULL)
|
|
|
|
{
|
|
|
|
pValueNext = pValue->next;
|
|
|
|
reg_unload_value(reg, pValue);
|
|
|
|
pValue = pValueNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(key->name);
|
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void reg_unload(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
RegKey* pKey;
|
|
|
|
RegKey* pKeyNext;
|
2012-06-06 05:50:03 +04:00
|
|
|
pKey = reg->root_key->subkeys;
|
|
|
|
|
|
|
|
while (pKey != NULL)
|
|
|
|
{
|
|
|
|
pKeyNext = pKey->next;
|
|
|
|
reg_unload_key(reg, pKey);
|
|
|
|
pKey = pKeyNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(reg->root_key);
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
Reg* reg_open(BOOL read_only)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
Reg* reg;
|
|
|
|
reg = (Reg*) malloc(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
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
reg->read_only = read_only;
|
|
|
|
reg->filename = WINPR_HKLM_HIVE;
|
|
|
|
|
|
|
|
if (reg->read_only)
|
|
|
|
{
|
|
|
|
reg->fp = fopen(reg->filename, "r");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reg->fp = fopen(reg->filename, "r+");
|
|
|
|
|
|
|
|
if (!reg->fp)
|
|
|
|
reg->fp = fopen(reg->filename, "w+");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reg->fp)
|
|
|
|
{
|
|
|
|
free(reg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
reg->root_key = (RegKey*) malloc(sizeof(RegKey));
|
|
|
|
if (!reg->root_key)
|
|
|
|
{
|
|
|
|
fclose(reg->fp);
|
|
|
|
free(reg);
|
|
|
|
return NULL;
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
2015-03-23 19:25:23 +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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
fclose(reg->fp);
|
|
|
|
free(reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void reg_print_value(Reg* reg, RegVal* value)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2014-08-18 19:22:22 +04:00
|
|
|
WLog_INFO(TAG, "\"%s\"=", value->name);
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
if (value->type == REG_DWORD)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_INFO(TAG, "dword:%08X", (int) value->data.dword);
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
else if (value->type == REG_SZ)
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_INFO(TAG, "%s\"", value->data.string);
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_ERR(TAG, "unimplemented format: %s", REG_DATA_TYPE_STRINGS[value->type]);
|
2012-06-06 05:50:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void reg_print_key(Reg* reg, RegKey* key)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
RegVal* pValue;
|
2012-06-06 05:50:03 +04:00
|
|
|
pValue = key->values;
|
2014-08-18 20:57:08 +04:00
|
|
|
WLog_INFO(TAG, "[%s]", key->name);
|
2012-06-06 05:50:03 +04:00
|
|
|
|
|
|
|
while (pValue != NULL)
|
|
|
|
{
|
|
|
|
reg_print_value(reg, pValue);
|
|
|
|
pValue = pValue->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void reg_print(Reg* reg)
|
2012-06-06 05:50:03 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
RegKey* pKey;
|
2012-06-06 05:50:03 +04:00
|
|
|
pKey = reg->root_key->subkeys;
|
|
|
|
|
|
|
|
while (pKey != NULL)
|
|
|
|
{
|
|
|
|
reg_print_key(reg, pKey);
|
|
|
|
pKey = pKey->next;
|
|
|
|
}
|
|
|
|
}
|