summaryrefslogtreecommitdiff
path: root/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/bootstrap_serverdaemon/tox_bootstrap_daemon.c')
-rw-r--r--other/bootstrap_serverdaemon/tox_bootstrap_daemon.c457
1 files changed, 457 insertions, 0 deletions
diff --git a/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c
new file mode 100644
index 00000000..f2c54bb1
--- /dev/null
+++ b/other/bootstrap_serverdaemon/tox_bootstrap_daemon.c
@@ -0,0 +1,457 @@
1/* tox_bootstrap_daemon.c
2 *
3 * Tox DHT bootstrap server daemon.
4 *
5 * Copyright (C) 2014 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27#include <syslog.h>
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <libconfig.h>
32#include <arpa/inet.h>
33#include <string.h>
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#include "../../toxcore/DHT.h"
40#include "../../toxcore/friend_requests.h"
41#include "../../toxcore/LAN_discovery.h"
42
43#include "../../testing/misc_tools.c"
44
45#define DAEMON_NAME "tox_bootstrap_daemon"
46
47#define SLEEP_TIME_MILLISECONDS 30
48#define sleep usleep(1000*SLEEP_TIME_MILLISECONDS)
49
50#define DEFAULT_PID_FILE_PATH ".tox_bootstrap_daemon.pid"
51#define DEFAULT_KEYS_FILE_PATH ".tox_bootstrap_daemon.keys"
52#define DEFAULT_PORT 33445
53#define DEFAULT_ENABLE_IPV6 0 // 1 - true, 0 - false
54#define DEFAULT_ENABLE_LAN_DISCOVERY 1 // 1 - true, 0 - false
55
56
57// Uses the already existing key or creates one if it didn't exist
58//
59// retirns 1 on success
60// 0 on failure - no keys were read or stored
61
62int manage_keys(DHT *dht, char *keys_file_path)
63{
64 const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
65 uint8_t keys[KEYS_SIZE];
66 FILE *keys_file;
67
68 // Check if file exits, proceed to open and load keys
69 keys_file = fopen(keys_file_path, "r");
70
71 if (keys_file != NULL) {
72 size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
73
74 if (read_size != KEYS_SIZE) {
75 return 0;
76 }
77
78 load_keys(dht->c, keys);
79 } else {
80 // Otherwise save new keys
81 new_keys(dht->c);
82 save_keys(dht->c, keys);
83
84 keys_file = fopen(keys_file_path, "w");
85
86 size_t write_size = fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
87
88 if (write_size != KEYS_SIZE) {
89 return 0;
90 }
91 }
92
93 fclose(keys_file);
94
95 // We want our DHT public key to be the same as our internal one since this is a bootstrap server
96 memcpy(dht->self_public_key, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES);
97 memcpy(dht->self_secret_key, dht->c->self_secret_key, crypto_box_SECRETKEYBYTES);
98
99 return 1;
100}
101
102// Gets general config options
103//
104// Important: you are responsible for freeing `pid_file_path` and `keys_file_path`
105//
106// returns 1 on success
107// 0 on failure, doesn't modify any data pointed by arguments
108
109int get_general_config(char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port, int *enable_ipv6,
110 int *enable_lan_discovery)
111{
112 config_t cfg;
113
114 const char *NAME_PORT = "port";
115 const char *NAME_PID_FILE_PATH = "pid_file_path";
116 const char *NAME_KEYS_FILE_PATH = "keys_file_path";
117 const char *NAME_ENABLE_IPV6 = "enable_ipv6";
118 const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery";
119
120 config_init(&cfg);
121
122 // Read the file. If there is an error, report it and exit.
123 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
124 syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
125 config_destroy(&cfg);
126 return 0;
127 }
128
129 // Get port
130 if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) {
131 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT);
132 syslog(LOG_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT);
133 *port = DEFAULT_PORT;
134 }
135
136 // Get PID file location
137 const char *tmp_pid_file;
138
139 if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) {
140 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH);
141 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH);
142 tmp_pid_file = DEFAULT_PID_FILE_PATH;
143 }
144
145 *pid_file_path = malloc(strlen(tmp_pid_file) + 1);
146 strcpy(*pid_file_path, tmp_pid_file);
147
148 // Get keys file location
149 const char *tmp_keys_file;
150
151 if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) {
152 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH);
153 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH);
154 tmp_keys_file = DEFAULT_KEYS_FILE_PATH;
155 }
156
157 *keys_file_path = malloc(strlen(tmp_keys_file) + 1);
158 strcpy(*keys_file_path, tmp_keys_file);
159
160 // Get IPv6 option
161 if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) {
162 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6);
163 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false");
164 *enable_ipv6 = DEFAULT_ENABLE_IPV6;
165 }
166
167 // Get LAN discovery option
168 if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
169 syslog(LOG_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
170 syslog(LOG_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
171 DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
172 *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
173 }
174
175 config_destroy(&cfg);
176
177 syslog(LOG_DEBUG, "Successfully read:\n");
178 syslog(LOG_DEBUG, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path);
179 syslog(LOG_DEBUG, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path);
180 syslog(LOG_DEBUG, "'%s': %d\n", NAME_PORT, *port);
181 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false");
182 syslog(LOG_DEBUG, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
183
184 return 1;
185}
186
187// Bootstraps servers listed in the config file
188//
189// returns 1 on success, some or no bootstrap servers were added
190// 0 on failure, a error accured while parsing config file
191
192int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
193{
194 const char *NAME_BOOTSTRAP_SERVERS = "bootstrap_servers";
195
196 const char *NAME_PUBLIC_KEY = "public_key";
197 const char *NAME_PORT = "port";
198 const char *NAME_ADDRESS = "address";
199
200 config_t cfg;
201
202 config_init(&cfg);
203
204 if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
205 syslog(LOG_ERR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
206 config_destroy(&cfg);
207 return 0;
208 }
209
210 config_setting_t *server_list = config_lookup(&cfg, NAME_BOOTSTRAP_SERVERS);
211
212 if (server_list == NULL) {
213 syslog(LOG_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_SERVERS);
214 config_destroy(&cfg);
215 return 1;
216 }
217
218 if (config_setting_length(server_list) == 0) {
219 syslog(LOG_WARNING, "No bootstrap servers found. Skipping bootstrapping.\n");
220 config_destroy(&cfg);
221 return 1;
222 }
223
224 int bs_port;
225 const char *bs_address;
226 const char *bs_public_key;
227
228 config_setting_t *server;
229
230 int i = 0;
231
232 while (config_setting_length(server_list)) {
233
234 server = config_setting_get_elem(server_list, 0);
235
236 if (server == NULL) {
237 config_destroy(&cfg);
238 return 0;
239 }
240
241 // Check that all settings are present
242 if (config_setting_lookup_string(server, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) {
243 syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PUBLIC_KEY);
244 goto next;
245 }
246
247 if (config_setting_lookup_int(server, NAME_PORT, &bs_port) == CONFIG_FALSE) {
248 syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_PORT);
249 goto next;
250 }
251
252 if (config_setting_lookup_string(server, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) {
253 syslog(LOG_WARNING, "Bootstrap server #%d: Couldn't find '%s' setting. Skipping the server.\n", i, NAME_ADDRESS);
254 goto next;
255 }
256
257 // Process settings
258 if (strlen(bs_public_key) != 64) {
259 syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_PUBLIC_KEY, bs_public_key);
260 goto next;
261 }
262
263 // not (1 <= port <= 65535)
264 if (bs_port < 1 || bs_port > 65535) {
265 syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %d. Skipping the server.\n", i, NAME_PORT, bs_port);
266 goto next;
267 }
268
269 const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port),
270 hex_string_to_bin((char *)bs_public_key));
271
272 if (!address_resolved) {
273 syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address);
274 goto next;
275 }
276
277 syslog(LOG_DEBUG, "Successfully added bootstrap server #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key);
278
279next:
280 // config_setting_lookup_string() allocates string inside and doesn't allow us to free it
281 // so in order to reuse `bs_public_key` and `bs_address` we have to remove the element
282 // which will cause libconfig to free allocated strings
283 config_setting_remove_elem(server_list, 0);
284 i++;
285 }
286
287 config_destroy(&cfg);
288
289 return 1;
290}
291
292// Prints public key
293
294void print_public_key(uint8_t *public_key)
295{
296 char buffer[64 + 1];
297 int index = 0;
298
299 int i;
300
301 for (i = 0; i < 32; i++) {
302 if (public_key[i] < 16) {
303 index += sprintf(buffer + index, "0");
304 }
305
306 index += sprintf(buffer + index, "%hhX", public_key[i]);
307 }
308
309 syslog(LOG_INFO, "Public Key: %s\n", buffer);
310
311 return;
312}
313
314int main(int argc, char *argv[])
315{
316 openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON);
317
318 if (argc < 2) {
319 syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n");
320 return 1;
321 }
322
323 char *cfg_file_path = argv[1];
324 char *pid_file_path, *keys_file_path;
325 int port;
326 int enable_ipv6;
327 int enable_lan_discovery;
328
329 if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery)) {
330 syslog(LOG_DEBUG, "General config read successfully\n");
331 } else {
332 syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
333 return 1;
334 }
335
336 // not (1 <= port <= 65535)
337 if (port < 1 || port > 65535) {
338 syslog(LOG_ERR, "Invalid port: %d, must be 1 <= port <= 65535. Exiting.\n", port);
339 return 1;
340 }
341
342 // Check if the PID file exists
343 if (fopen(pid_file_path, "r")) {
344 syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists. Exiting.\n", pid_file_path);
345 return 1;
346 }
347
348 IP ip;
349 ip_init(&ip, enable_ipv6);
350
351 DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port)));
352
353 if (dht == NULL) {
354 syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n");
355 return 1;
356 }
357
358 Onion *onion = new_onion(dht);
359 Onion_Announce *onion_a = new_onion_announce(dht);
360
361 if (!(onion && onion_a)) {
362 syslog(LOG_ERR, "Couldn't initialize Tox Onion. Exiting.\n");
363 return 1;
364 }
365
366 if (enable_lan_discovery) {
367 LANdiscovery_init(dht);
368 }
369
370 if (manage_keys(dht, keys_file_path)) {
371 syslog(LOG_DEBUG, "Keys are managed successfully\n");
372 } else {
373 syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path);
374 return 1;
375 }
376
377 if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) {
378 syslog(LOG_DEBUG, "List of bootstrap servers read successfully\n");
379 } else {
380 syslog(LOG_ERR, "Couldn't read list of bootstrap servers in %s. Exiting.\n", cfg_file_path);
381 return 1;
382 }
383
384 print_public_key(dht->c->self_public_key);
385
386 // Write the PID file
387 FILE *pidf = fopen(pid_file_path, "w");
388
389 if (pidf == NULL) {
390 syslog(LOG_ERR, "Can't open the PID file for writing: %s. Exiting.\n", pid_file_path);
391 return 1;
392 }
393
394 free(pid_file_path);
395 free(keys_file_path);
396
397 // Fork off from the parent process
398 pid_t pid = fork();
399
400 if (pid < 0) {
401 fclose(pidf);
402 syslog(LOG_ERR, "Forking failed. Exiting.\n");
403 return 1;
404 }
405
406 if (pid > 0) {
407 fprintf(pidf, "%d\n", pid);
408 fclose(pidf);
409 syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid);
410 return 0;
411 }
412
413 // Change the file mode mask
414 umask(0);
415
416 // Create a new SID for the child process
417 if (setsid() < 0) {
418 syslog(LOG_ERR, "SID creation failure. Exiting.\n");
419 return 1;
420 }
421
422 // Change the current working directory
423 if ((chdir("/")) < 0) {
424 syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n");
425 return 1;
426 }
427
428 // Go quiet
429 close(STDOUT_FILENO);
430 close(STDIN_FILENO);
431 close(STDERR_FILENO);
432
433 uint64_t last_LANdiscovery = 0;
434 uint16_t htons_port = htons(port);
435
436 int waiting_for_dht_connection = 1;
437
438 while (1) {
439 do_DHT(dht);
440
441 if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) {
442 send_LANdiscovery(htons_port, dht);
443 last_LANdiscovery = unix_time();
444 }
445
446 networking_poll(dht->net);
447
448 if (waiting_for_dht_connection && DHT_isconnected(dht)) {
449 syslog(LOG_DEBUG, "Connected to other bootstrap server successfully.\n");
450 waiting_for_dht_connection = 0;
451 }
452
453 sleep;
454 }
455
456 return 1;
457}