Skip to content

Commit 9b2d3e5

Browse files
committed
Merge branch 'bootstrap_daemon-leaks-1' of https://github.com/tux3/toxcore into tux3-bootstrap_daemon-leaks-1
2 parents 3c64c87 + 4c12ee3 commit 9b2d3e5

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

other/bootstrap_daemon/tox_bootstrap_daemon.c

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
9191
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
9292

9393
if (read_size != KEYS_SIZE) {
94+
fclose(keys_file);
9495
return 0;
9596
}
9697

@@ -106,6 +107,7 @@ int manage_keys(DHT *dht, char *keys_file_path)
106107
size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
107108

108109
if (write_size != KEYS_SIZE) {
110+
fclose(keys_file);
109111
return 0;
110112
}
111113
}
@@ -147,8 +149,11 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
147149
for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
148150

149151
(*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i];
150-
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
151-
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);
152+
153+
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
154+
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
155+
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
156+
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
152157
continue;
153158
}
154159

@@ -162,24 +167,24 @@ void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int
162167
}
163168

164169
if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
165-
syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS);
170+
syslog(LOG_WARNING, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
171+
NAME_TCP_RELAY_PORTS);
166172
return;
167173
}
168174

169175
int config_port_count = config_setting_length(ports_array);
176+
170177
if (config_port_count == 0) {
171178
syslog(LOG_WARNING, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
172179
return;
173180
}
174181

175182
*tcp_relay_ports = malloc(config_port_count * sizeof(uint16_t));
176183

177-
config_setting_t *elem;
178184
int i;
179185

180186
for (i = 0; i < config_port_count; i ++) {
181-
182-
elem = config_setting_get_elem(ports_array, i);
187+
config_setting_t *elem = config_setting_get_elem(ports_array, i);
183188

184189
if (elem == NULL) {
185190
// 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
193198
}
194199

195200
(*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
196-
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
197-
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);
201+
202+
if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
203+
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
204+
syslog(LOG_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
205+
(*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
198206
continue;
199207
}
200208

@@ -315,6 +323,7 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
315323
syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
316324
tmp_motd = DEFAULT_MOTD;
317325
}
326+
318327
size_t tmp_motd_length = strlen(tmp_motd) + 1;
319328
size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
320329
*motd = malloc(motd_length);
@@ -332,20 +341,23 @@ int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_fi
332341
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
333342

334343
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false");
344+
335345
// show info about tcp ports only if tcp relay is enabled
336346
if (*enable_tcp_relay) {
337347
if (*tcp_relay_port_count == 0) {
338348
syslog(LOG_DEBUG, "No TCP ports could be read.\n");
339349
} else {
340350
syslog(LOG_DEBUG, "Read %d TCP ports:\n", *tcp_relay_port_count);
341351
int i;
352+
342353
for (i = 0; i < *tcp_relay_port_count; i ++) {
343354
syslog(LOG_DEBUG, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
344355
}
345356
}
346357
}
347358

348359
syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false");
360+
349361
if (*enable_motd) {
350362
syslog(LOG_DEBUG, "'%s': %s\n", NAME_MOTD, *motd);
351363
}
@@ -424,14 +436,15 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
424436
}
425437

426438
// Process settings
427-
if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES*2) {
439+
if (strlen(bs_public_key) != crypto_box_PUBLICKEYBYTES * 2) {
428440
syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
429441
bs_public_key);
430442
goto next;
431443
}
432444

433445
if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
434-
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);
446+
syslog(LOG_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT,
447+
bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
435448
goto next;
436449
}
437450

@@ -464,7 +477,7 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
464477

465478
void print_public_key(uint8_t *public_key)
466479
{
467-
char buffer[2*crypto_box_PUBLICKEYBYTES + 1];
480+
char buffer[2 * crypto_box_PUBLICKEYBYTES + 1];
468481
int index = 0;
469482

470483
int i;
@@ -500,7 +513,8 @@ int main(int argc, char *argv[])
500513
int enable_motd;
501514
char *motd;
502515

503-
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)) {
516+
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery,
517+
&enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
504518
syslog(LOG_DEBUG, "General config read successfully\n");
505519
} else {
506520
syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
@@ -513,8 +527,11 @@ int main(int argc, char *argv[])
513527
}
514528

515529
// Check if the PID file exists
516-
if (fopen(pid_file_path, "r")) {
530+
FILE *pid_file;
531+
532+
if (pid_file = fopen(pid_file_path, "r")) {
517533
syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path);
534+
fclose(pid_file);
518535
}
519536

520537
IP ip;
@@ -536,12 +553,13 @@ int main(int argc, char *argv[])
536553
}
537554

538555
if (enable_motd) {
539-
if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t*)motd, strlen(motd) + 1) == 0) {
556+
if (bootstrap_set_callbacks(dht->net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
540557
syslog(LOG_DEBUG, "Set MOTD successfully.\n");
541558
} else {
542559
syslog(LOG_ERR, "Couldn't set MOTD: %s. Exiting.\n", motd);
543560
return 1;
544561
}
562+
545563
free(motd);
546564
}
547565

@@ -560,7 +578,8 @@ int main(int argc, char *argv[])
560578
return 1;
561579
}
562580

563-
tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key, dht->self_secret_key, onion);
581+
tcp_server = new_TCP_server(enable_ipv6, tcp_relay_port_count, tcp_relay_ports, dht->self_public_key,
582+
dht->self_secret_key, onion);
564583

565584
// tcp_relay_port_count != 0 at this point
566585
free(tcp_relay_ports);
@@ -596,17 +615,18 @@ int main(int argc, char *argv[])
596615
// Fork off from the parent process
597616
pid_t pid = fork();
598617

599-
if (pid < 0) {
600-
fclose(pidf);
601-
syslog(LOG_ERR, "Forking failed. Exiting.\n");
602-
return 1;
603-
}
604-
605618
if (pid > 0) {
606619
fprintf(pidf, "%d ", pid);
607620
fclose(pidf);
608621
syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid);
609622
return 0;
623+
} else {
624+
fclose(pidf);
625+
}
626+
627+
if (pid < 0) {
628+
syslog(LOG_ERR, "Forking failed. Exiting.\n");
629+
return 1;
610630
}
611631

612632
// Change the file mode mask

0 commit comments

Comments
 (0)