Linux: Support keys in config file (#1093)
- Signing and encryption keys are set through the configuration file. - SoftEeprom update.
This commit is contained in:
committed by
Mikael Falkvidd
parent
4d90c00f98
commit
3c0b2727a5
@@ -60,9 +60,6 @@ int SoftEeprom::init(const char *fileName, size_t length)
|
||||
|
||||
_length = length;
|
||||
_values = new uint8_t[_length];
|
||||
for (size_t i = 0; i < _length; ++i) {
|
||||
_values[i] = 0xFF;
|
||||
}
|
||||
|
||||
if (stat(_fileName, &fileInfo) != 0) {
|
||||
//File does not exist. Create it.
|
||||
@@ -72,11 +69,16 @@ int SoftEeprom::init(const char *fileName, size_t length)
|
||||
logError("Unable to create config file %s.\n", _fileName);
|
||||
return -1;
|
||||
}
|
||||
// Fill the eeprom with 1s
|
||||
for (size_t i = 0; i < _length; ++i) {
|
||||
_values[i] = 0xFF;
|
||||
}
|
||||
myFile.write((const char*)_values, _length);
|
||||
myFile.close();
|
||||
} else if (fileInfo.st_size < 0 || (size_t)fileInfo.st_size != _length) {
|
||||
logError("EEPROM file %s is not the correct size of %zu. Please remove the file and a new one will be created.\n",
|
||||
_fileName, _length);
|
||||
destroy();
|
||||
return -1;
|
||||
} else {
|
||||
//Read config into local memory.
|
||||
@@ -100,27 +102,19 @@ void SoftEeprom::destroy()
|
||||
if (_fileName) {
|
||||
free(_fileName);
|
||||
}
|
||||
_length = 0;
|
||||
}
|
||||
|
||||
void SoftEeprom::readBlock(void* buf, void* addr, size_t length)
|
||||
{
|
||||
static bool config_to_mem = false;
|
||||
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
|
||||
|
||||
if (!config_to_mem && length) {
|
||||
//Read config into local memory.
|
||||
std::ifstream myFile(_fileName, std::ios::in | std::ios::binary);
|
||||
if (!myFile) {
|
||||
logError("Unable to open config to file %s for reading.\n", _fileName);
|
||||
exit(1);
|
||||
}
|
||||
myFile.read((char*)_values, _length);
|
||||
myFile.close();
|
||||
|
||||
config_to_mem = true;
|
||||
if (!length) {
|
||||
logError("EEPROM being read without being initialized!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (length && offs + length <= _length) {
|
||||
if (offs + length <= _length) {
|
||||
memcpy(buf, _values+offs, length);
|
||||
}
|
||||
}
|
||||
@@ -129,7 +123,16 @@ void SoftEeprom::writeBlock(void* buf, void* addr, size_t length)
|
||||
{
|
||||
unsigned long int offs = reinterpret_cast<unsigned long int>(addr);
|
||||
|
||||
if (length && offs + length <= _length) {
|
||||
if (!length) {
|
||||
logError("EEPROM being written without being initialized!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (offs + length <= _length) {
|
||||
if (memcmp(_values+offs, buf, length) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(_values+offs, buf, length);
|
||||
|
||||
std::ofstream myFile(_fileName, std::ios::out | std::ios::in | std::ios::binary);
|
||||
|
||||
+61
-6
@@ -49,12 +49,15 @@ int config_parse(const char *config_file)
|
||||
return -1;
|
||||
}
|
||||
|
||||
conf.verbose = 4;
|
||||
conf.verbose = 7;
|
||||
conf.log_pipe = 0;
|
||||
conf.log_pipe_file = NULL;
|
||||
conf.syslog = 0;
|
||||
conf.eeprom_file = NULL;
|
||||
conf.eeprom_size = 1024;
|
||||
conf.eeprom_size = 0;
|
||||
conf.soft_hmac_key = NULL;
|
||||
conf.soft_serial_key = NULL;
|
||||
conf.aes_key = NULL;
|
||||
|
||||
while (fgets(buf, 1024, fptr)) {
|
||||
if (buf[0] != '#' && buf[0] != 10 && buf[0] != 13) {
|
||||
@@ -145,6 +148,21 @@ int config_parse(const char *config_file)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else if (!strncmp(buf, "soft_hmac_key=", 14)) {
|
||||
if (_config_parse_string(&(buf[14]), "soft_hmac_key", &conf.soft_hmac_key)) {
|
||||
fclose(fptr);
|
||||
return -1;
|
||||
}
|
||||
} else if (!strncmp(buf, "soft_serial_key=", 16)) {
|
||||
if (_config_parse_string(&(buf[16]), "soft_serial_key", &conf.soft_serial_key)) {
|
||||
fclose(fptr);
|
||||
return -1;
|
||||
}
|
||||
} else if (!strncmp(buf, "aes_key=", 8)) {
|
||||
if (_config_parse_string(&(buf[8]), "aes_key", &conf.aes_key)) {
|
||||
fclose(fptr);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
logWarning("Unknown config option \"%s\".\n", buf);
|
||||
}
|
||||
@@ -172,12 +190,24 @@ int config_parse(const char *config_file)
|
||||
|
||||
void config_cleanup(void)
|
||||
{
|
||||
if (conf.eeprom_file) {
|
||||
free(conf.eeprom_file);
|
||||
if (conf.log_filepath) {
|
||||
free(conf.log_filepath);
|
||||
}
|
||||
if (conf.log_pipe_file) {
|
||||
free(conf.log_pipe_file);
|
||||
}
|
||||
if (conf.eeprom_file) {
|
||||
free(conf.eeprom_file);
|
||||
}
|
||||
if (conf.soft_hmac_key) {
|
||||
free(conf.soft_hmac_key);
|
||||
}
|
||||
if (conf.soft_serial_key) {
|
||||
free(conf.soft_serial_key);
|
||||
}
|
||||
if (conf.aes_key) {
|
||||
free(conf.aes_key);
|
||||
}
|
||||
}
|
||||
|
||||
int _config_create(const char *config_file)
|
||||
@@ -185,12 +215,15 @@ int _config_create(const char *config_file)
|
||||
FILE *myFile;
|
||||
int ret;
|
||||
|
||||
const char default_conf[] = "# Logging verbosity: debug,info,notice,warn,err\n" \
|
||||
const char default_conf[] = "# Logging\n" \
|
||||
"# Verbosity: debug,info,notice,warn,err\n" \
|
||||
"verbose=debug\n" \
|
||||
"\n" \
|
||||
"# Enable logging to a file.\n" \
|
||||
"log_file=0\n" \
|
||||
"# Log file path.\n" \
|
||||
"log_filepath=/tmp/mysgw.log\n" \
|
||||
"\n" \
|
||||
"# Enable logging to a named pipe.\n" \
|
||||
"# Use this option to view your gateway's log messages\n" \
|
||||
"# from the log_pipe_file defined bellow.\n" \
|
||||
@@ -198,10 +231,32 @@ int _config_create(const char *config_file)
|
||||
"# cat \"log_pipe_file\"\n" \
|
||||
"log_pipe=0\n" \
|
||||
"log_pipe_file=/tmp/mysgw.pipe\n" \
|
||||
"\n" \
|
||||
"# Enable logging to syslog.\n" \
|
||||
"syslog=0\n" \
|
||||
"\n" \
|
||||
"# EEPROM settings\n" \
|
||||
"eeprom_file=/etc/mysensors.eeprom\n" \
|
||||
"eeprom_size=1024\n";
|
||||
"eeprom_size=1024\n" \
|
||||
"\n" \
|
||||
"# Software signing settings\n" \
|
||||
"# Note: The gateway must have been built with signing\n" \
|
||||
"# support to use the options below.\n" \
|
||||
"#\n" \
|
||||
"# To generate a HMAC key run mysgw with: --gen-soft-hmac-key\n" \
|
||||
"# copy the new key in the line below and uncomment it.\n" \
|
||||
"#soft_hmac_key=\n" \
|
||||
"# To generate a serial key run mysgw with: --gen-soft-serial-key\n" \
|
||||
"# copy the new key in the line below and uncomment it.\n" \
|
||||
"#soft_serial_key=\n" \
|
||||
"\n" \
|
||||
"# Encryption settings\n" \
|
||||
"# Note: The gateway must have been built with encryption\n" \
|
||||
"# support to use the options below.\n" \
|
||||
"#\n" \
|
||||
"# To generate a AES key run mysgw with: --gen-aes-key\n" \
|
||||
"# copy the new key in the line below and uncomment it.\n" \
|
||||
"#aes_key=\n";
|
||||
|
||||
myFile = fopen(config_file, "w");
|
||||
if (!myFile) {
|
||||
|
||||
@@ -33,6 +33,9 @@ struct config {
|
||||
int syslog;
|
||||
char *eeprom_file;
|
||||
int eeprom_size;
|
||||
char *soft_hmac_key;
|
||||
char *soft_serial_key;
|
||||
char *aes_key;
|
||||
} conf;
|
||||
|
||||
int config_parse(const char *config_file);
|
||||
|
||||
Reference in New Issue
Block a user