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)) {
227 config->buf = decrypted;
228 config->len = decrypted_len;
231 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
232 meshlink_errno = MESHLINK_ESTORAGE;
245 /// Write a configuration file to a FILE handle.
246 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config) {
247 if(!mesh->confbase) {
251 if(mesh->config_key) {
252 uint8_t buf[config->len + 16];
253 size_t len = sizeof(buf);
255 randomize(&seqbuf, sizeof(seqbuf));
256 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
257 chacha_poly1305_set_key(ctx, mesh->config_key);
258 bool success = false;
260 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
261 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
263 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
264 meshlink_errno = MESHLINK_ESTORAGE;
267 chacha_poly1305_exit(ctx);
271 if(fwrite(config->buf, config->len, 1, f) != 1) {
272 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
273 meshlink_errno = MESHLINK_ESTORAGE;
280 /// Free resources of a loaded configuration file.
281 void config_free(config_t *config) {
282 free((uint8_t *)config->buf);
287 /// Check the presence of a host configuration file.
288 bool config_exists(meshlink_handle_t *mesh, const char *name) {
289 if(!mesh->confbase) {
294 make_host_path(mesh, name, path, sizeof(path));
296 return access(path, F_OK) == 0;
299 /// Read a host configuration file.
300 bool config_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
301 if(!mesh->confbase) {
306 make_host_path(mesh, name, path, sizeof(path));
308 FILE *f = fopen(path, "r");
311 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
315 if(!config_read_file(mesh, f, config)) {
316 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
325 void config_scan_all(meshlink_handle_t *mesh, config_scan_action_t action) {
326 if(!mesh->confbase) {
332 char dname[PATH_MAX];
333 make_host_path(mesh, NULL, dname, sizeof(dname));
335 dir = opendir(dname);
338 logger(mesh, MESHLINK_ERROR, "Could not open %s: %s", dname, strerror(errno));
339 meshlink_errno = MESHLINK_ESTORAGE;
343 while((ent = readdir(dir))) {
344 action(mesh, ent->d_name);
350 /// Write a host configuration file.
351 bool config_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
352 if(!mesh->confbase) {
357 make_host_path(mesh, name, path, sizeof(path));
359 FILE *f = fopen(path, "w");
362 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
366 if(!config_write_file(mesh, f, config)) {
367 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
376 /// Read the main configuration file.
377 bool main_config_read(meshlink_handle_t *mesh, config_t *config) {
378 if(!mesh->confbase) {
383 make_main_path(mesh, path, sizeof(path));
385 FILE *f = fopen(path, "r");
388 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
392 if(!config_read_file(mesh, f, config)) {
393 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
402 /// Write the main configuration file.
403 bool main_config_write(meshlink_handle_t *mesh, const config_t *config) {
404 if(!mesh->confbase) {
409 make_main_path(mesh, path, sizeof(path));
411 FILE *f = fopen(path, "w");
414 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
418 if(!config_write_file(mesh, f, config)) {
419 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
428 /// Read an invitation file, and immediately delete it.
429 bool invitation_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
430 if(!mesh->confbase) {
435 char used_path[PATH_MAX];
436 make_invitation_path(mesh, name, path, sizeof(path));
437 make_used_invitation_path(mesh, name, used_path, sizeof(used_path));
439 // Atomically rename the invitation file
440 if(rename(path, used_path)) {
441 if(errno == ENOENT) {
442 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
444 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
450 FILE *f = fopen(used_path, "r");
453 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
457 // Check the timestamp
460 if(fstat(fileno(f), &st)) {
461 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
467 if(time(NULL) > st.st_mtime + mesh->invitation_timeout) {
468 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
474 if(!config_read_file(mesh, f, config)) {
475 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
486 /// Write an invitation file.
487 bool invitation_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
488 if(!mesh->confbase) {
493 make_invitation_path(mesh, name, path, sizeof(path));
495 FILE *f = fopen(path, "w");
498 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
502 if(!config_write_file(mesh, f, config)) {
503 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
512 /// Purge old invitation files
513 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
514 if(!mesh->confbase) {
519 make_invitation_path(mesh, "", path, sizeof(path));
521 DIR *dir = opendir(path);
524 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
525 meshlink_errno = MESHLINK_ESTORAGE;
533 while((ent = readdir(dir))) {
534 if(strlen(ent->d_name) != 24) {
538 char invname[PATH_MAX];
541 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
542 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
546 if(!stat(invname, &st)) {
547 if(mesh->invitation_key && deadline < st.st_mtime) {
553 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
559 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
561 meshlink_errno = MESHLINK_ESTORAGE;