Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server logs refactoring #865

Merged
merged 21 commits into from
Jul 20, 2023
30 changes: 16 additions & 14 deletions net/net-aes-keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "common/server/signals.h"
#include "common/wrappers/memory-utils.h"

DEFINE_VERBOSITY(net_crypto_aes)

static_assert(AES_KEY_MIN_LEN >= sizeof(((aes_key_t *) NULL)->id), "key_size");

static aes_key_t **aes_loaded_keys;
Expand All @@ -47,15 +49,15 @@ bool aes_key_add(aes_key_t *aes_key) {
aes_key_t *added_key = aes_loaded_keys[i];

if (aes_key->id == added_key->id || !strcmp(aes_key->filename, added_key->filename)) {
vkprintf(2, "Cannot add AES key %d(\"%s\"): already added %d(\"%s\")\n", aes_key->id, aes_key->filename, added_key->id, added_key->filename);
tvkprintf(net_crypto_aes, 4, "Cannot add AES key %d(\"%s\"): already added %d(\"%s\")\n", aes_key->id, aes_key->filename, added_key->id, added_key->filename);
DrDet marked this conversation as resolved.
Show resolved Hide resolved
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

aes_loaded_keys = static_cast<aes_key_t**>(realloc(aes_loaded_keys, sizeof(aes_key) * (aes_loaded_keys_size + 1)));
aes_loaded_keys[aes_loaded_keys_size++] = aes_key;

vkprintf(1, "Add AES key %u(\"%s\")\n", aes_key->id, aes_key->filename);
tvkprintf(net_crypto_aes, 4, "Add AES key %u(\"%s\")\n", aes_key->id, aes_key->filename);
DrDet marked this conversation as resolved.
Show resolved Hide resolved
astrophysik marked this conversation as resolved.
Show resolved Hide resolved

return true;
}
Expand All @@ -65,7 +67,7 @@ static bool aes_key_set_default(const char *filename) {
for (size_t i = 0; i < aes_loaded_keys_size; ++i) {
aes_key_t *key = aes_loaded_keys[i];
if (!strcmp(key->filename, filename)) {
vkprintf(1, "Setting default AES key to: %d(\"%s\")\n", key->id, key->filename);
tvkprintf(net_crypto_aes, 4, "Setting default AES key to: %d(\"%s\")\n", key->id, key->filename);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
default_aes_key = key;
return true;
}
Expand All @@ -91,23 +93,23 @@ aes_key_t *aes_key_load_memory(const char* filename, uint8_t *key, int32_t key_l
static aes_key_t *aes_key_load_fd(int fd, const char *filename) {
struct stat st;
if (fstat(fd, &st) == -1) {
vkprintf(2, "Cannot fstat() AES key fd: %d(\"%s\"): %m\n", fd, filename);
tvkprintf(net_crypto_aes, 4, "Cannot fstat() AES key fd: %d(\"%s\"): %m\n", fd, filename);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}

if (st.st_size < AES_KEY_MIN_LEN) {
vkprintf(2, "Ignoring too small AES key: %jd(min %d)\n", (intmax_t)(st.st_size), AES_KEY_MIN_LEN);
tvkprintf(net_crypto_aes, 4, "Ignoring too small AES key: %jd(min %d)\n", (intmax_t)(st.st_size), AES_KEY_MIN_LEN);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}

if (st.st_size > AES_KEY_MAX_LEN) {
vkprintf(2, "Ignoring too large AES key: %jd(max %d)\n", (intmax_t)(st.st_size), AES_KEY_MAX_LEN);
tvkprintf(net_crypto_aes, 4, "Ignoring too large AES key: %jd(max %d)\n", (intmax_t)(st.st_size), AES_KEY_MAX_LEN);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}

char buffer[AES_KEY_MAX_LEN];
if (!read_exact(fd, buffer, st.st_size)) {
vkprintf(2, "Cannot read AES key fd: %d: %m\n", fd);
tvkprintf(net_crypto_aes, 4, "Cannot read AES key fd: %d: %m\n", fd);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}

Expand All @@ -125,7 +127,7 @@ static bool aes_key_load_file(int fd, const char *path) {
close(fd);
if (!key) {
free(tmp_path);
vkprintf(1, "Cannot load AES key from fd: %d(\"%s\"): %m\n", fd, path);
tvkprintf(net_crypto_aes, 4, "Cannot load AES key from fd: %d(\"%s\"): %m\n", fd, path);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Expand All @@ -145,14 +147,14 @@ static bool aes_key_load_dir(int fd) {
const int fd = openat(dir_fd, entry->d_name, O_NOFOLLOW);
if (fd == -1) {
if(errno != ELOOP) {
vkprintf(1, "Cannot openat() AES key dir entry: \"%s\": %m\n", entry->d_name);
tvkprintf(net_crypto_aes, 4, "Cannot openat() AES key dir entry: \"%s\": %m\n", entry->d_name);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
}
continue;
}

struct stat st;
if (fstat(fd, &st) == -1) {
vkprintf(1, "Cannot fstatat() AES key dir entry: \"%s\": %m\n", entry->d_name);
tvkprintf(net_crypto_aes, 4, "Cannot fstatat() AES key dir entry: \"%s\": %m\n", entry->d_name);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

Expand All @@ -168,7 +170,7 @@ static bool aes_key_load_dir(int fd) {
char buffer[NAME_MAX + 1];
if (readlinkat(dir_fd, "default", buffer, sizeof(buffer)) == -1) {
assert(!closedir(dir));
vkprintf(1, "Cannot readlinkat() \"default\" AES key symlink\n");
tvkprintf(net_crypto_aes, 4, "Cannot readlinkat() \"default\" AES key symlink\n");
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
assert(!closedir(dir));
Expand All @@ -179,14 +181,14 @@ static bool aes_key_load_dir(int fd) {
bool aes_key_load_path(const char *path) {
const int fd = open(path, O_RDONLY);
if (fd == -1) {
vkprintf(1, "Cannot open() AES key path: \"%s\": %m\n", path);
tvkprintf(net_crypto_aes, 4, "Cannot open() AES key path: \"%s\": %m\n", path);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

struct stat st;
if (fstat(fd, &st) == -1) {
close(fd);
vkprintf(1, "Cannot fstat() AES key path fd: %d: %m\n", fd);
tvkprintf(net_crypto_aes, 4, "Cannot fstat() AES key path fd: %d: %m\n", fd);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Expand All @@ -199,7 +201,7 @@ bool aes_key_load_path(const char *path) {
}

close(fd);
vkprintf(1, "Unexpected file type for AES key path: %u\n", S_IFMT & st.st_mode);
tvkprintf(net_crypto_aes, 4, "Unexpected file type for AES key path: %u\n", S_IFMT & st.st_mode);
astrophysik marked this conversation as resolved.
Show resolved Hide resolved

return false;
}
Expand Down
4 changes: 4 additions & 0 deletions net/net-aes-keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
#include <stdbool.h>
#include <stdint.h>

#include "common/kprintf.h"

#define AES_KEY_MIN_LEN 32
#define AES_KEY_MAX_LEN 256

DECLARE_VERBOSITY(net_crypto_aes);

struct aes_key {
const char *filename;
int32_t id;
Expand Down
Loading
Loading