Merge branch 'bootstrap_daemon-leaks-1' of https://github.com/tux3/toxcore into tux3-bootstrap_daemon-leaks-1

This commit is contained in:
irungentoo 2014-06-19 16:23:00 -04:00
commit 9b2d3e50b2
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98

View File

@ -91,6 +91,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
if (read_size != KEYS_SIZE) { if (read_size != KEYS_SIZE) {
fclose(keys_file);
return 0; return 0;
} }
@ -106,6 +107,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file); size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
if (write_size != KEYS_SIZE) { if (write_size != KEYS_SIZE) {
fclose(keys_file);
return 0; return 0;
} }
} }
@ -147,8 +149,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) { for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
(*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i]; (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i];
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
continue; continue;
} }
@ -162,11 +167,13 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
} }
if (config_setting_is_array(ports_array) == CONFIG_FALSE) { if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS); syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
NAME_TCP_RELAY_PORTS);
return; return;
} }
int config_port_count = config_setting_length(ports_array); int config_port_count = config_setting_length(ports_array);
if (config_port_count == 0) { if (config_port_count == 0) {
syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
return; return;
@ -174,12 +181,10 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
*tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t)); *tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t));
config_setting_t *elem;
int i; int i;
for (i = 0; i < config_port_count; i ++) { for (i = 0; i < config_port_count; i ++) {
config_setting_t *elem = config_setting_get_elem(ports_array, i);
elem = config_setting_get_elem(ports_array, i);
if (elem == NULL) { if (elem == NULL) {
// it's NULL if `ports_array` is not an array (we have that check ealier) or if `i` is out of range, which should not be // it's NULL if `ports_array` is not an array (we have that check ealier) or if `i` is out of range, which should not be
@ -193,8 +198,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
} }
(*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem); (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
continue; continue;
} }
@ -315,6 +323,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
tmp_motd = DEFAULT_MOTD; tmp_motd = DEFAULT_MOTD;
} }
size_t tmp_motd_length = strlen(tmp_motd) + 1; size_t tmp_motd_length = strlen(tmp_motd) + 1;
size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length; size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
*motd = malloc(motd_length); *motd = malloc(motd_length);
@ -332,6 +341,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
// show info about tcp ports only if tcp relay is enabled // show info about tcp ports only if tcp relay is enabled
if (*enable_tcp_relay) { if (*enable_tcp_relay) {
if (*tcp_relay_port_count == 0) { if (*tcp_relay_port_count == 0) {
@ -339,6 +349,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
} else { } else {
syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count); syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count);
int i; int i;
for (i = 0; i < *tcp_relay_port_count; i ++) { for (i = 0; i < *tcp_relay_port_count; i ++) {
syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
} }
@ -346,6 +357,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
} }
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
if (*enable_motd) { if (*enable_motd) {
syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd); syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd);
} }
@ -424,14 +436,15 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
} }
// Process settings // Process settings
if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES*2) { if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) {
syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
bs_public_key); bs_public_key);
goto next; goto next;
} }
if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT,
bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
goto next; goto next;
} }
@ -464,7 +477,7 @@ next:
void print_public_key(uint8_t *public_key) void print_public_key(uint8_t *public_key)
{ {
char buffer[2*crypto_box_PUBLICKEYBYTES + 1]; char buffer[2 * crypto_box_PUBLICKEYBYTES + 1];
int index = 0; int index = 0;
int i; int i;
@ -500,7 +513,8 @@ int main(int argc, char *argv[])
int enable_motd; int enable_motd;
char *motd; char *motd;
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery,
&enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
syslog(LOG_DEBUG, "General config read successfully\n"); syslog(LOG_DEBUG, "General config read successfully\n");
} else { } else {
syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
@ -513,8 +527,11 @@ int main(int argc, char *argv[])
} }
// Check if the PID file exists // Check if the PID file exists
if (fopen(pid_file_path, "r")) { FILE *pid_file;
if (pid_file = fopen(pid_file_path, "r")) {
syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
fclose(pid_file);
} }
IP ip; IP ip;
@ -536,12 +553,13 @@ int main(int argc, char *argv[])
} }
if (enable_motd) { if (enable_motd) {
if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t*)motd, strlen(motd) + 1) == 0) { if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
syslog(LOG_DEBUG, "Set MOTD successfully.\n"); syslog(LOG_DEBUG, "Set MOTD successfully.\n");
} else { } else {
syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd); syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd);
return 1; return 1;
} }
free(motd); free(motd);
} }
@ -560,7 +578,8 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key, dht->self_secret_key, onion); tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key,
dht->self_secret_key, onion);
// tcp_relay_port_count != 0 at this point // tcp_relay_port_count != 0 at this point
free(tcp_relay_ports); free(tcp_relay_ports);
@ -596,17 +615,18 @@ int main(int argc, char *argv[])
// Fork off from the parent process // Fork off from the parent process
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) {
fclose(pidf);
syslog(LOG_ERR, "Forking failed. Exiting.\n");
return 1;
}
if (pid > 0) { if (pid > 0) {
fprintf(pidf, "%d ", pid); fprintf(pidf, "%d ", pid);
fclose(pidf); fclose(pidf);
syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid);
return 0; return 0;
} else {
fclose(pidf);
}
if (pid < 0) {
syslog(LOG_ERR, "Forking failed. Exiting.\n");
return 1;
} }
// Change the file mode mask // Change the file mode mask