v0.2.11 - Picky shit
This commit is contained in:
277
src/config.c
277
src/config.c
@@ -154,7 +154,7 @@ int init_config_database_statements(void) {
|
||||
log_info("Initializing configuration database statements...");
|
||||
|
||||
// Prepare statement for getting configuration values
|
||||
const char* get_sql = "SELECT value FROM server_config WHERE key = ?";
|
||||
const char* get_sql = "SELECT value FROM config WHERE key = ?";
|
||||
int rc = sqlite3_prepare_v2(g_db, get_sql, -1, &g_config_manager.get_config_stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
log_error("Failed to prepare get_config statement");
|
||||
@@ -162,7 +162,7 @@ int init_config_database_statements(void) {
|
||||
}
|
||||
|
||||
// Prepare statement for setting configuration values
|
||||
const char* set_sql = "INSERT OR REPLACE INTO server_config (key, value, updated_at) VALUES (?, ?, strftime('%s', 'now'))";
|
||||
const char* set_sql = "INSERT OR REPLACE INTO config (key, value, updated_at) VALUES (?, ?, strftime('%s', 'now'))";
|
||||
rc = sqlite3_prepare_v2(g_db, set_sql, -1, &g_config_manager.set_config_stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
log_error("Failed to prepare set_config statement");
|
||||
@@ -242,7 +242,7 @@ int load_config_from_database(void) {
|
||||
// Database configuration is already populated by schema defaults
|
||||
// This function validates that the configuration tables exist and are accessible
|
||||
|
||||
const char* test_sql = "SELECT COUNT(*) FROM server_config WHERE config_type IN ('system', 'user')";
|
||||
const char* test_sql = "SELECT COUNT(*) FROM config WHERE config_type IN ('system', 'user')";
|
||||
sqlite3_stmt* test_stmt;
|
||||
|
||||
int rc = sqlite3_prepare_v2(g_db, test_sql, -1, &test_stmt, NULL);
|
||||
@@ -677,7 +677,7 @@ int config_requires_restart(const char* key) {
|
||||
if (!key) return 0;
|
||||
|
||||
// Check database for requires_restart flag
|
||||
const char* sql = "SELECT requires_restart FROM server_config WHERE key = ?";
|
||||
const char* sql = "SELECT requires_restart FROM config WHERE key = ?";
|
||||
sqlite3_stmt* stmt;
|
||||
|
||||
int rc = sqlite3_prepare_v2(g_db, sql, -1, &stmt, NULL);
|
||||
@@ -735,6 +735,7 @@ cJSON* create_config_nostr_event(const char* privkey_hex) {
|
||||
{"relay_description", "High-performance C Nostr relay with SQLite storage"},
|
||||
{"relay_contact", ""},
|
||||
{"relay_pubkey", ""},
|
||||
{"relay_privkey", ""},
|
||||
{"relay_software", "https://git.laantungir.net/laantungir/c-relay.git"},
|
||||
{"relay_version", VERSION},
|
||||
|
||||
@@ -767,7 +768,7 @@ cJSON* create_config_nostr_event(const char* privkey_hex) {
|
||||
int defaults_count = sizeof(defaults) / sizeof(defaults[0]);
|
||||
|
||||
// First try to load from database, fall back to defaults
|
||||
const char* sql = "SELECT key, value FROM server_config WHERE config_type IN ('system', 'user') ORDER BY key";
|
||||
const char* sql = "SELECT key, value FROM config WHERE config_type IN ('system', 'user') ORDER BY key";
|
||||
sqlite3_stmt* stmt;
|
||||
|
||||
int rc = sqlite3_prepare_v2(g_db, sql, -1, &stmt, NULL);
|
||||
@@ -901,6 +902,56 @@ int write_config_event_to_file(const cJSON* event) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Helper function to generate random private key
|
||||
int generate_random_private_key(char* privkey_hex, size_t buffer_size) {
|
||||
if (!privkey_hex || buffer_size < 65) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE* urandom = fopen("/dev/urandom", "rb");
|
||||
if (!urandom) {
|
||||
log_error("Failed to open /dev/urandom for key generation");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char privkey_bytes[32];
|
||||
if (fread(privkey_bytes, 1, 32, urandom) != 32) {
|
||||
log_error("Failed to read random bytes for private key");
|
||||
fclose(urandom);
|
||||
return -1;
|
||||
}
|
||||
fclose(urandom);
|
||||
|
||||
// Convert to hex
|
||||
nostr_bytes_to_hex(privkey_bytes, 32, privkey_hex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Helper function to derive public key from private key
|
||||
int derive_public_key(const char* privkey_hex, char* pubkey_hex, size_t buffer_size) {
|
||||
if (!privkey_hex || !pubkey_hex || buffer_size < 65) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convert hex private key to bytes
|
||||
unsigned char privkey_bytes[32];
|
||||
if (nostr_hex_to_bytes(privkey_hex, privkey_bytes, 32) != 0) {
|
||||
log_error("Failed to convert private key from hex");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Generate corresponding public key
|
||||
unsigned char pubkey_bytes[32];
|
||||
if (nostr_ec_public_key_from_private_key(privkey_bytes, pubkey_bytes) == 0) {
|
||||
nostr_bytes_to_hex(pubkey_bytes, 32, pubkey_hex);
|
||||
return 0;
|
||||
} else {
|
||||
log_error("Failed to derive public key from private key");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int generate_config_file_if_missing(void) {
|
||||
// Check if config file already exists
|
||||
if (config_file_exists()) {
|
||||
@@ -910,84 +961,170 @@ int generate_config_file_if_missing(void) {
|
||||
|
||||
log_info("Generating missing configuration file...");
|
||||
|
||||
// Get private key from environment variable or generate random one
|
||||
char privkey_hex[65];
|
||||
const char* env_privkey = getenv(CONFIG_PRIVKEY_ENV);
|
||||
// Generate or get admin private key for configuration signing
|
||||
char admin_privkey_hex[65];
|
||||
const char* env_admin_privkey = getenv(CONFIG_ADMIN_PRIVKEY_ENV);
|
||||
|
||||
if (env_privkey && strlen(env_privkey) == 64) {
|
||||
// Use provided private key
|
||||
strncpy(privkey_hex, env_privkey, sizeof(privkey_hex) - 1);
|
||||
privkey_hex[sizeof(privkey_hex) - 1] = '\0';
|
||||
log_info("Using private key from environment variable");
|
||||
if (env_admin_privkey && strlen(env_admin_privkey) == 64) {
|
||||
// Use provided admin private key
|
||||
strncpy(admin_privkey_hex, env_admin_privkey, sizeof(admin_privkey_hex) - 1);
|
||||
admin_privkey_hex[sizeof(admin_privkey_hex) - 1] = '\0';
|
||||
log_info("Using admin private key from environment variable");
|
||||
} else {
|
||||
// Generate random private key manually (nostr_core_lib doesn't have a key generation function)
|
||||
FILE* urandom = fopen("/dev/urandom", "rb");
|
||||
if (!urandom) {
|
||||
log_error("Failed to open /dev/urandom for key generation");
|
||||
// Generate random admin private key
|
||||
if (generate_random_private_key(admin_privkey_hex, sizeof(admin_privkey_hex)) != 0) {
|
||||
log_error("Failed to generate admin private key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char privkey_bytes[32];
|
||||
if (fread(privkey_bytes, 1, 32, urandom) != 32) {
|
||||
log_error("Failed to read random bytes for private key");
|
||||
fclose(urandom);
|
||||
return -1;
|
||||
}
|
||||
fclose(urandom);
|
||||
|
||||
// Convert to hex
|
||||
nostr_bytes_to_hex(privkey_bytes, 32, privkey_hex);
|
||||
|
||||
// Generate corresponding public key
|
||||
char pubkey_hex[65];
|
||||
unsigned char pubkey_bytes[32];
|
||||
if (nostr_ec_public_key_from_private_key(privkey_bytes, pubkey_bytes) == 0) {
|
||||
nostr_bytes_to_hex(pubkey_bytes, 32, pubkey_hex);
|
||||
} else {
|
||||
log_error("Failed to derive public key from private key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
log_info("Generated random private key for configuration signing");
|
||||
|
||||
// Print the generated private key prominently for the administrator
|
||||
printf("\n");
|
||||
printf("=================================================================\n");
|
||||
printf("IMPORTANT: GENERATED RELAY ADMIN PRIVATE KEY\n");
|
||||
printf("=================================================================\n");
|
||||
printf("Private Key: %s\n", privkey_hex);
|
||||
printf("Public Key: %s\n", pubkey_hex);
|
||||
printf("\nSAVE THIS PRIVATE KEY SECURELY - IT CONTROLS YOUR RELAY!\n");
|
||||
printf("\nTo use this key in future sessions:\n");
|
||||
printf(" export %s=%s\n", CONFIG_PRIVKEY_ENV, privkey_hex);
|
||||
printf("=================================================================\n");
|
||||
printf("\n");
|
||||
|
||||
char warning_msg[256];
|
||||
snprintf(warning_msg, sizeof(warning_msg),
|
||||
"To use a specific private key, set the %s environment variable", CONFIG_PRIVKEY_ENV);
|
||||
log_warning(warning_msg);
|
||||
log_info("Generated random admin private key for configuration signing");
|
||||
}
|
||||
|
||||
// Create Nostr event
|
||||
cJSON* event = create_config_nostr_event(privkey_hex);
|
||||
if (!event) {
|
||||
log_error("Failed to create configuration event");
|
||||
// Generate or get relay private key for relay identity
|
||||
char relay_privkey_hex[65];
|
||||
const char* env_relay_privkey = getenv(CONFIG_RELAY_PRIVKEY_ENV);
|
||||
|
||||
if (env_relay_privkey && strlen(env_relay_privkey) == 64) {
|
||||
// Use provided relay private key
|
||||
strncpy(relay_privkey_hex, env_relay_privkey, sizeof(relay_privkey_hex) - 1);
|
||||
relay_privkey_hex[sizeof(relay_privkey_hex) - 1] = '\0';
|
||||
log_info("Using relay private key from environment variable");
|
||||
} else {
|
||||
// Generate random relay private key
|
||||
if (generate_random_private_key(relay_privkey_hex, sizeof(relay_privkey_hex)) != 0) {
|
||||
log_error("Failed to generate relay private key");
|
||||
return -1;
|
||||
}
|
||||
log_info("Generated random relay private key for relay identity");
|
||||
}
|
||||
|
||||
// Derive public keys from private keys
|
||||
char admin_pubkey_hex[65];
|
||||
char relay_pubkey_hex[65];
|
||||
|
||||
if (derive_public_key(admin_privkey_hex, admin_pubkey_hex, sizeof(admin_pubkey_hex)) != 0) {
|
||||
log_error("Failed to derive admin public key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Extract and store the admin public key in database configuration
|
||||
cJSON* pubkey_obj = cJSON_GetObjectItem(event, "pubkey");
|
||||
if (pubkey_obj && cJSON_IsString(pubkey_obj)) {
|
||||
const char* admin_pubkey = cJSON_GetStringValue(pubkey_obj);
|
||||
if (set_database_config("admin_pubkey", admin_pubkey, "system") == 0) {
|
||||
log_info("Stored admin public key in configuration database");
|
||||
printf(" Admin Public Key: %s\n", admin_pubkey);
|
||||
if (derive_public_key(relay_privkey_hex, relay_pubkey_hex, sizeof(relay_pubkey_hex)) != 0) {
|
||||
log_error("Failed to derive relay public key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Display both keypairs prominently for the administrator
|
||||
printf("\n");
|
||||
printf("=================================================================\n");
|
||||
printf("IMPORTANT: GENERATED RELAY KEYPAIRS\n");
|
||||
printf("=================================================================\n");
|
||||
printf("ADMIN KEYS (for configuration signing):\n");
|
||||
printf(" Private Key: %s\n", admin_privkey_hex);
|
||||
printf(" Public Key: %s\n", admin_pubkey_hex);
|
||||
printf("\nRELAY KEYS (for relay identity):\n");
|
||||
printf(" Private Key: %s\n", relay_privkey_hex);
|
||||
printf(" Public Key: %s\n", relay_pubkey_hex);
|
||||
printf("\nSAVE THESE PRIVATE KEYS SECURELY!\n");
|
||||
printf("\nTo use specific keys in future sessions:\n");
|
||||
printf(" export %s=%s\n", CONFIG_ADMIN_PRIVKEY_ENV, admin_privkey_hex);
|
||||
printf(" export %s=%s\n", CONFIG_RELAY_PRIVKEY_ENV, relay_privkey_hex);
|
||||
printf("=================================================================\n");
|
||||
printf("\n");
|
||||
|
||||
// Default configuration values (same as in create_config_nostr_event)
|
||||
typedef struct {
|
||||
const char* key;
|
||||
const char* value;
|
||||
} default_config_t;
|
||||
|
||||
static const default_config_t defaults[] = {
|
||||
// Administrative settings
|
||||
{"admin_enabled", "false"},
|
||||
|
||||
// Server core settings
|
||||
{"relay_port", "8888"},
|
||||
{"database_path", "db/c_nostr_relay.db"},
|
||||
{"max_connections", "100"},
|
||||
|
||||
// NIP-11 Relay Information
|
||||
{"relay_name", "C Nostr Relay"},
|
||||
{"relay_description", "High-performance C Nostr relay with SQLite storage"},
|
||||
{"relay_contact", ""},
|
||||
{"relay_software", "https://git.laantungir.net/laantungir/c-relay.git"},
|
||||
{"relay_version", VERSION},
|
||||
|
||||
// NIP-13 Proof of Work
|
||||
{"pow_enabled", "true"},
|
||||
{"pow_min_difficulty", "0"},
|
||||
{"pow_mode", "basic"},
|
||||
|
||||
// NIP-40 Expiration Timestamp
|
||||
{"expiration_enabled", "true"},
|
||||
{"expiration_strict", "true"},
|
||||
{"expiration_filter", "true"},
|
||||
{"expiration_grace_period", "300"},
|
||||
|
||||
// Subscription limits
|
||||
{"max_subscriptions_per_client", "25"},
|
||||
{"max_total_subscriptions", "5000"},
|
||||
{"max_filters_per_subscription", "10"},
|
||||
|
||||
// Event processing limits
|
||||
{"max_event_tags", "100"},
|
||||
{"max_content_length", "8196"},
|
||||
{"max_message_length", "16384"},
|
||||
|
||||
// Performance settings
|
||||
{"default_limit", "500"},
|
||||
{"max_limit", "5000"}
|
||||
};
|
||||
|
||||
int defaults_count = sizeof(defaults) / sizeof(defaults[0]);
|
||||
|
||||
// Store all three keys and all default configuration values in database
|
||||
if (set_database_config("admin_pubkey", admin_pubkey_hex, "system") == 0) {
|
||||
log_info("Stored admin public key in configuration database");
|
||||
} else {
|
||||
log_warning("Failed to store admin public key in database");
|
||||
}
|
||||
|
||||
if (set_database_config("relay_privkey", relay_privkey_hex, "system") == 0) {
|
||||
log_info("Stored relay private key in configuration database");
|
||||
} else {
|
||||
log_warning("Failed to store relay private key in database");
|
||||
}
|
||||
|
||||
if (set_database_config("relay_pubkey", relay_pubkey_hex, "system") == 0) {
|
||||
log_info("Stored relay public key in configuration database");
|
||||
} else {
|
||||
log_warning("Failed to store relay public key in database");
|
||||
}
|
||||
|
||||
// Store all default configuration values
|
||||
log_info("Storing default configuration values in database...");
|
||||
int stored_count = 0;
|
||||
for (int i = 0; i < defaults_count; i++) {
|
||||
if (set_database_config(defaults[i].key, defaults[i].value, "system") == 0) {
|
||||
stored_count++;
|
||||
} else {
|
||||
log_warning("Failed to store admin public key in database");
|
||||
log_warning("Failed to store default configuration");
|
||||
printf(" Key: %s, Value: %s\n", defaults[i].key, defaults[i].value);
|
||||
}
|
||||
}
|
||||
|
||||
if (stored_count == defaults_count) {
|
||||
log_success("All default configuration values stored successfully");
|
||||
printf(" Stored %d configuration entries\n", stored_count);
|
||||
} else {
|
||||
log_warning("Some default configuration values failed to store");
|
||||
printf(" Stored %d of %d configuration entries\n", stored_count, defaults_count);
|
||||
}
|
||||
|
||||
// Create Nostr event using admin private key for signing
|
||||
cJSON* event = create_config_nostr_event(admin_privkey_hex);
|
||||
if (!event) {
|
||||
log_error("Failed to create configuration event");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Write to file
|
||||
int result = write_config_event_to_file(event);
|
||||
cJSON_Delete(event);
|
||||
|
||||
Reference in New Issue
Block a user