1 /* SPDX-License-Identifier: GPL-3.0-or-later
2  * Copyright © 2016-2018 The TokTok team.
3  * Copyright © 2014-2016 Tox project.
4  */
5 
6 /*
7  * Tox DHT bootstrap daemon.
8  * Functionality related to dealing with the config file.
9  */
10 #include "config.h"
11 
12 #include "config_defaults.h"
13 #include "log.h"
14 
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include <libconfig.h>
19 
20 #include "../../bootstrap_node_packets.h"
21 
22 /**
23  * Parses tcp relay ports from `cfg` and puts them into `tcp_relay_ports` array.
24  *
25  * Supposed to be called from get_general_config only.
26  *
27  * Important: iff `tcp_relay_port_count` > 0, then you are responsible for freeing `tcp_relay_ports`.
28  */
parse_tcp_relay_ports_config(config_t * cfg,uint16_t ** tcp_relay_ports,int * tcp_relay_port_count)29 static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_ports, int *tcp_relay_port_count)
30 {
31     const char *NAME_TCP_RELAY_PORTS = "tcp_relay_ports";
32 
33     *tcp_relay_port_count = 0;
34 
35     config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS);
36 
37     if (ports_array == nullptr) {
38         log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS);
39         log_write(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS);
40 
41         uint16_t default_ports[DEFAULT_TCP_RELAY_PORTS_COUNT] = {DEFAULT_TCP_RELAY_PORTS};
42 
43         int i;
44 
45         for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
46             log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, default_ports[i]);
47         }
48 
49         // similar procedure to the one of reading config file below
50         *tcp_relay_ports = (uint16_t *)malloc(DEFAULT_TCP_RELAY_PORTS_COUNT * sizeof(uint16_t));
51 
52         for (i = 0; i < DEFAULT_TCP_RELAY_PORTS_COUNT; i ++) {
53 
54             (*tcp_relay_ports)[*tcp_relay_port_count] = default_ports[i];
55 
56             if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
57                     || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
58                 log_write(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
59                           (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
60                 continue;
61             }
62 
63             (*tcp_relay_port_count) ++;
64         }
65 
66         // the loop above skips invalid ports, so we adjust the allocated memory size
67         if ((*tcp_relay_port_count) > 0) {
68             *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t));
69         } else {
70             free(*tcp_relay_ports);
71             *tcp_relay_ports = nullptr;
72         }
73 
74         return;
75     }
76 
77     if (config_setting_is_array(ports_array) == CONFIG_FALSE) {
78         log_write(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n",
79                   NAME_TCP_RELAY_PORTS);
80         return;
81     }
82 
83     int config_port_count = config_setting_length(ports_array);
84 
85     if (config_port_count == 0) {
86         log_write(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
87         return;
88     }
89 
90     *tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t));
91 
92     int i;
93 
94     for (i = 0; i < config_port_count; i ++) {
95         config_setting_t *elem = config_setting_get_elem(ports_array, i);
96 
97         if (elem == nullptr) {
98             // it's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be
99             log_write(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i);
100             break;
101         }
102 
103         if (config_setting_is_number(elem) == CONFIG_FALSE) {
104             log_write(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i);
105             continue;
106         }
107 
108         (*tcp_relay_ports)[*tcp_relay_port_count] = config_setting_get_int(elem);
109 
110         if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
111                 || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) {
112             log_write(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i,
113                       (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
114             continue;
115         }
116 
117         (*tcp_relay_port_count) ++;
118     }
119 
120     // the loop above skips invalid ports, so we adjust the allocated memory size
121     if ((*tcp_relay_port_count) > 0) {
122         *tcp_relay_ports = (uint16_t *)realloc(*tcp_relay_ports, (*tcp_relay_port_count) * sizeof(uint16_t));
123     } else {
124         free(*tcp_relay_ports);
125         *tcp_relay_ports = nullptr;
126     }
127 }
128 
get_general_config(const char * cfg_file_path,char ** pid_file_path,char ** keys_file_path,int * port,int * enable_ipv6,int * enable_ipv4_fallback,int * enable_lan_discovery,int * enable_tcp_relay,uint16_t ** tcp_relay_ports,int * tcp_relay_port_count,int * enable_motd,char ** motd)129 int get_general_config(const char *cfg_file_path, char **pid_file_path, char **keys_file_path, int *port,
130                        int *enable_ipv6, int *enable_ipv4_fallback, int *enable_lan_discovery, int *enable_tcp_relay,
131                        uint16_t **tcp_relay_ports, int *tcp_relay_port_count, int *enable_motd, char **motd)
132 {
133     config_t cfg;
134 
135     const char *NAME_PORT                 = "port";
136     const char *NAME_PID_FILE_PATH        = "pid_file_path";
137     const char *NAME_KEYS_FILE_PATH       = "keys_file_path";
138     const char *NAME_ENABLE_IPV6          = "enable_ipv6";
139     const char *NAME_ENABLE_IPV4_FALLBACK = "enable_ipv4_fallback";
140     const char *NAME_ENABLE_LAN_DISCOVERY = "enable_lan_discovery";
141     const char *NAME_ENABLE_TCP_RELAY     = "enable_tcp_relay";
142     const char *NAME_ENABLE_MOTD          = "enable_motd";
143     const char *NAME_MOTD                 = "motd";
144 
145     config_init(&cfg);
146 
147     // Read the file. If there is an error, report it and exit.
148     if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
149         log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
150         config_destroy(&cfg);
151         return 0;
152     }
153 
154     // Get port
155     if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) {
156         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT);
157         log_write(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT);
158         *port = DEFAULT_PORT;
159     }
160 
161     // Get PID file location
162     const char *tmp_pid_file;
163 
164     if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) {
165         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH);
166         log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH);
167         tmp_pid_file = DEFAULT_PID_FILE_PATH;
168     }
169 
170     *pid_file_path = (char *)malloc(strlen(tmp_pid_file) + 1);
171     strcpy(*pid_file_path, tmp_pid_file);
172 
173     // Get keys file location
174     const char *tmp_keys_file;
175 
176     if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) {
177         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH);
178         log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH);
179         tmp_keys_file = DEFAULT_KEYS_FILE_PATH;
180     }
181 
182     *keys_file_path = (char *)malloc(strlen(tmp_keys_file) + 1);
183     strcpy(*keys_file_path, tmp_keys_file);
184 
185     // Get IPv6 option
186     if (config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) {
187         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6);
188         log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false");
189         *enable_ipv6 = DEFAULT_ENABLE_IPV6;
190     }
191 
192     // Get IPv4 fallback option
193     if (config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) {
194         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK);
195         log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK,
196                   DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
197         *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
198     }
199 
200     // Get LAN discovery option
201     if (config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) {
202         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY);
203         log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
204                   DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
205         *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
206     }
207 
208     // Get TCP relay option
209     if (config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) {
210         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY);
211         log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY,
212                   DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
213         *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY;
214     }
215 
216     if (*enable_tcp_relay) {
217         parse_tcp_relay_ports_config(&cfg, tcp_relay_ports, tcp_relay_port_count);
218     } else {
219         *tcp_relay_port_count = 0;
220     }
221 
222     // Get MOTD option
223     if (config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) {
224         log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD);
225         log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
226                   DEFAULT_ENABLE_MOTD ? "true" : "false");
227         *enable_motd = DEFAULT_ENABLE_MOTD;
228     }
229 
230     if (*enable_motd) {
231         // Get MOTD
232         const char *tmp_motd;
233 
234         if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) {
235             log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD);
236             log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
237             tmp_motd = DEFAULT_MOTD;
238         }
239 
240         size_t tmp_motd_length = strlen(tmp_motd) + 1;
241         size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
242         *motd = (char *)malloc(motd_length);
243         strncpy(*motd, tmp_motd, motd_length);
244         (*motd)[motd_length - 1] = '\0';
245     }
246 
247     config_destroy(&cfg);
248 
249     log_write(LOG_LEVEL_INFO, "Successfully read:\n");
250     log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH,        *pid_file_path);
251     log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH,       *keys_file_path);
252     log_write(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT,                 *port);
253     log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6,          *enable_ipv6          ? "true" : "false");
254     log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false");
255     log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false");
256 
257     log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY,     *enable_tcp_relay     ? "true" : "false");
258 
259     // show info about tcp ports only if tcp relay is enabled
260     if (*enable_tcp_relay) {
261         if (*tcp_relay_port_count == 0) {
262             log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n");
263         } else {
264             log_write(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count);
265             int i;
266 
267             for (i = 0; i < *tcp_relay_port_count; i ++) {
268                 log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]);
269             }
270         }
271     }
272 
273     log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD,          *enable_motd          ? "true" : "false");
274 
275     if (*enable_motd) {
276         log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd);
277     }
278 
279     return 1;
280 }
281 
282 /**
283  *
284  * Converts a hex string with even number of characters into binary.
285  *
286  * Important: You are responsible for freeing the return value.
287  *
288  * @return binary on success,
289  *         NULL on failure.
290  */
bootstrap_hex_string_to_bin(const char * hex_string)291 static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string)
292 {
293     if (strlen(hex_string) % 2 != 0) {
294         return nullptr;
295     }
296 
297     size_t len = strlen(hex_string) / 2;
298     uint8_t *ret = (uint8_t *)malloc(len);
299 
300     const char *pos = hex_string;
301     size_t i;
302 
303     for (i = 0; i < len; ++i, pos += 2) {
304         unsigned int val;
305         sscanf(pos, "%02x", &val);
306         ret[i] = val;
307     }
308 
309     return ret;
310 }
311 
bootstrap_from_config(const char * cfg_file_path,DHT * dht,int enable_ipv6)312 int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6)
313 {
314     const char *NAME_BOOTSTRAP_NODES = "bootstrap_nodes";
315 
316     const char *NAME_PUBLIC_KEY = "public_key";
317     const char *NAME_PORT       = "port";
318     const char *NAME_ADDRESS    = "address";
319 
320     config_t cfg;
321 
322     config_init(&cfg);
323 
324     if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) {
325         log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
326         config_destroy(&cfg);
327         return 0;
328     }
329 
330     config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES);
331 
332     if (node_list == nullptr) {
333         log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n",
334                   NAME_BOOTSTRAP_NODES);
335         config_destroy(&cfg);
336         return 1;
337     }
338 
339     if (config_setting_length(node_list) == 0) {
340         log_write(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n");
341         config_destroy(&cfg);
342         return 1;
343     }
344 
345     int bs_port;
346     const char *bs_address;
347     const char *bs_public_key;
348 
349     config_setting_t *node;
350 
351     int i = 0;
352 
353     while (config_setting_length(node_list)) {
354         int address_resolved;
355         uint8_t *bs_public_key_bin;
356 
357         node = config_setting_get_elem(node_list, 0);
358 
359         if (node == nullptr) {
360             config_destroy(&cfg);
361             return 0;
362         }
363 
364         // Check that all settings are present
365         if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) {
366             log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i,
367                       NAME_PUBLIC_KEY);
368             goto next;
369         }
370 
371         if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) {
372             log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT);
373             goto next;
374         }
375 
376         if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) {
377             log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS);
378             goto next;
379         }
380 
381         // Process settings
382         if (strlen(bs_public_key) != CRYPTO_PUBLIC_KEY_SIZE * 2) {
383             log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY,
384                       bs_public_key);
385             goto next;
386         }
387 
388         if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) {
389             log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i,
390                       NAME_PORT,
391                       bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
392             goto next;
393         }
394 
395         bs_public_key_bin = bootstrap_hex_string_to_bin(bs_public_key);
396         address_resolved = dht_bootstrap_from_address(dht, bs_address, enable_ipv6, net_htons(bs_port),
397                            bs_public_key_bin);
398         free(bs_public_key_bin);
399 
400         if (!address_resolved) {
401             log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address);
402             goto next;
403         }
404 
405         log_write(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key);
406 
407 next:
408         // config_setting_lookup_string() allocates string inside and doesn't allow us to free it direcly
409         // though it's freed when the element is removed, so we free it right away in order to keep memory
410         // consumption minimal
411         config_setting_remove_elem(node_list, 0);
412         i++;
413     }
414 
415     config_destroy(&cfg);
416 
417     return 1;
418 }
419