2 econf.c -- configuration code
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "meshlink_internal.h"
30 /// Generate a path to the main configuration file.
31 static void make_main_path(meshlink_handle_t *mesh, char *path, size_t len) {
32 snprintf(path, len, "%s" SLASH "meshlink.conf", mesh->confbase);
35 /// Generate a path to a host configuration file.
36 static void make_host_path(meshlink_handle_t *mesh, const char *name, char *path, size_t len) {
37 snprintf(path, len, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
40 /// Generate a path to an unused invitation file.
41 static void make_invitation_path(meshlink_handle_t *mesh, const char *name, char *path, size_t len) {
42 snprintf(path, len, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, name);
45 /// Generate a path to a used invitation file.
46 static void make_used_invitation_path(meshlink_handle_t *mesh, const char *name, char *path, size_t len) {
47 snprintf(path, len, "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, name);
50 /// Remove a directory recursively
51 static void deltree(const char *dirname) {
52 DIR *d = opendir(dirname);
57 while((ent = readdir(d))) {
58 if(ent->d_name[0] == '.') {
62 char filename[PATH_MAX];
63 snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
65 if(unlink(filename)) {
76 /// Create a fresh configuration directory
77 bool config_init(meshlink_handle_t *mesh) {
82 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
83 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
89 // Remove meshlink.conf
90 snprintf(path, sizeof(path), "%s" SLASH "meshlink.conf", mesh->confbase);
93 // Remove any host config files
94 snprintf(path, sizeof(path), "%s" SLASH "hosts", mesh->confbase);
97 if(mkdir(path, 0700) && errno != EEXIST) {
98 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
102 // Remove any invitation files
103 snprintf(path, sizeof(path), "%s" SLASH "invitations", mesh->confbase);
106 if(mkdir(path, 0700) && errno != EEXIST) {
107 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
114 /// Wipe an existing configuration directory
115 bool config_destroy(const char *confbase) {
118 // Remove meshlink.conf
119 snprintf(path, sizeof(path), "%s" SLASH "meshlink.conf", confbase);
122 if(errno == ENOENT) {
123 meshlink_errno = MESHLINK_ENOENT;
126 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
127 meshlink_errno = MESHLINK_ESTORAGE;
136 /// Check the presence of the main configuration file.
137 bool main_config_exists(meshlink_handle_t *mesh) {
138 if(!mesh->confbase) {
143 make_main_path(mesh, path, sizeof(path));
145 return access(path, F_OK) == 0;
148 /// Lock the main configuration file.
149 bool main_config_lock(meshlink_handle_t *mesh) {
150 if(!mesh->confbase) {
155 make_main_path(mesh, path, sizeof(path));
157 mesh->conffile = fopen(path, "r");
159 if(!mesh->conffile) {
160 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
161 meshlink_errno = MESHLINK_ESTORAGE;
166 fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
170 // TODO: use _locking()?
173 if(flock(fileno(mesh->conffile), LOCK_EX | LOCK_NB) != 0) {
174 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
175 fclose(mesh->conffile);
176 mesh->conffile = NULL;
177 meshlink_errno = MESHLINK_EBUSY;
186 /// Unlock the main configuration file.
187 void main_config_unlock(meshlink_handle_t *mesh) {
189 fclose(mesh->conffile);
190 mesh->conffile = NULL;
194 /// Read a configuration file from a FILE handle.
195 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config) {
196 if(!mesh->confbase) {
203 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
204 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
205 meshlink_errno = MESHLINK_ESTORAGE;
210 uint8_t *buf = xmalloc(len);
212 if(fread(buf, len, 1, f) != 1) {
213 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
214 meshlink_errno = MESHLINK_ESTORAGE;
219 if(mesh->config_key) {
220 uint8_t *decrypted = xmalloc(len);
221 size_t decrypted_len = len;
222 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
223 chacha_poly1305_set_key(ctx, mesh->config_key);
225 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
226 chacha_poly1305_exit(ctx);
228 config->buf = decrypted;
229 config->len = decrypted_len;
232 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
233 meshlink_errno = MESHLINK_ESTORAGE;
234 chacha_poly1305_exit(ctx);
247 /// Write a configuration file to a FILE handle.
248 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config) {
249 if(!mesh->confbase) {
253 if(mesh->config_key) {
254 uint8_t buf[config->len + 16];
255 size_t len = sizeof(buf);
257 randomize(&seqbuf, sizeof(seqbuf));
258 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
259 chacha_poly1305_set_key(ctx, mesh->config_key);
260 bool success = false;
262 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
263 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
265 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
266 meshlink_errno = MESHLINK_ESTORAGE;
269 chacha_poly1305_exit(ctx);
273 if(fwrite(config->buf, config->len, 1, f) != 1) {
274 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
275 meshlink_errno = MESHLINK_ESTORAGE;
282 /// Free resources of a loaded configuration file.
283 void config_free(config_t *config) {
284 free((uint8_t *)config->buf);
289 /// Check the presence of a host configuration file.
290 bool config_exists(meshlink_handle_t *mesh, const char *name) {
291 if(!mesh->confbase) {
296 make_host_path(mesh, name, path, sizeof(path));
298 return access(path, F_OK) == 0;
301 /// Read a host configuration file.
302 bool config_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
303 if(!mesh->confbase) {
308 make_host_path(mesh, name, path, sizeof(path));
310 FILE *f = fopen(path, "r");
313 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
317 if(!config_read_file(mesh, f, config)) {
318 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
327 void config_scan_all(meshlink_handle_t *mesh, config_scan_action_t action) {
328 if(!mesh->confbase) {
334 char dname[PATH_MAX];
335 make_host_path(mesh, "", dname, sizeof(dname));
337 dir = opendir(dname);
340 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
341 meshlink_errno = MESHLINK_ESTORAGE;
345 while((ent = readdir(dir))) {
346 action(mesh, ent->d_name);
352 /// Write a host configuration file.
353 bool config_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
354 if(!mesh->confbase) {
359 make_host_path(mesh, name, path, sizeof(path));
361 FILE *f = fopen(path, "w");
364 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
368 if(!config_write_file(mesh, f, config)) {
369 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
378 /// Read the main configuration file.
379 bool main_config_read(meshlink_handle_t *mesh, config_t *config) {
380 if(!mesh->confbase) {
385 make_main_path(mesh, path, sizeof(path));
387 FILE *f = fopen(path, "r");
390 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
394 if(!config_read_file(mesh, f, config)) {
395 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
404 /// Write the main configuration file.
405 bool main_config_write(meshlink_handle_t *mesh, const config_t *config) {
406 if(!mesh->confbase) {
411 make_main_path(mesh, path, sizeof(path));
413 FILE *f = fopen(path, "w");
416 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
420 if(!config_write_file(mesh, f, config)) {
421 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
430 /// Read an invitation file, and immediately delete it.
431 bool invitation_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
432 if(!mesh->confbase) {
437 char used_path[PATH_MAX];
438 make_invitation_path(mesh, name, path, sizeof(path));
439 make_used_invitation_path(mesh, name, used_path, sizeof(used_path));
441 // Atomically rename the invitation file
442 if(rename(path, used_path)) {
443 if(errno == ENOENT) {
444 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
446 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
452 FILE *f = fopen(used_path, "r");
455 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
459 // Check the timestamp
462 if(fstat(fileno(f), &st)) {
463 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
469 if(time(NULL) > st.st_mtime + mesh->invitation_timeout) {
470 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
476 if(!config_read_file(mesh, f, config)) {
477 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
488 /// Write an invitation file.
489 bool invitation_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
490 if(!mesh->confbase) {
495 make_invitation_path(mesh, name, path, sizeof(path));
497 FILE *f = fopen(path, "w");
500 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
504 if(!config_write_file(mesh, f, config)) {
505 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
514 /// Purge old invitation files
515 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
516 if(!mesh->confbase) {
521 make_invitation_path(mesh, "", path, sizeof(path));
523 DIR *dir = opendir(path);
526 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
527 meshlink_errno = MESHLINK_ESTORAGE;
535 while((ent = readdir(dir))) {
536 if(strlen(ent->d_name) != 24) {
540 char invname[PATH_MAX];
543 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
544 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
548 if(!stat(invname, &st)) {
549 if(mesh->invitation_key && deadline < st.st_mtime) {
555 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
561 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
563 meshlink_errno = MESHLINK_ESTORAGE;