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) {
78 if(mkdir(mesh->confbase, 0700) && errno != EEXIST) {
79 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
85 // Remove meshlink.conf
86 snprintf(path, sizeof(path), "%s" SLASH "meshlink.conf", mesh->confbase);
89 // Remove any host config files
90 snprintf(path, sizeof(path), "%s" SLASH "hosts", mesh->confbase);
93 if(mkdir(path, 0700) && errno != EEXIST) {
94 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
98 // Remove any invitation files
99 snprintf(path, sizeof(path), "%s" SLASH "invitations", mesh->confbase);
102 if(mkdir(path, 0700) && errno != EEXIST) {
103 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
110 /// Wipe an existing configuration directory
111 bool config_destroy(const char *confbase) {
114 // Remove meshlink.conf
115 snprintf(path, sizeof(path), "%s" SLASH "meshlink.conf", confbase);
118 if(errno == ENOENT) {
119 meshlink_errno = MESHLINK_ENOENT;
122 logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
123 meshlink_errno = MESHLINK_ESTORAGE;
132 /// Check the presence of the main configuration file.
133 bool main_config_exists(meshlink_handle_t *mesh) {
135 make_main_path(mesh, path, sizeof(path));
137 return access(path, F_OK) == 0;
140 /// Lock the main configuration file.
141 bool main_config_lock(meshlink_handle_t *mesh) {
143 make_main_path(mesh, path, sizeof(path));
145 mesh->conffile = fopen(path, "r");
147 if(!mesh->conffile) {
148 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
149 meshlink_errno = MESHLINK_ESTORAGE;
154 fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
158 // TODO: use _locking()?
161 if(flock(fileno(mesh->conffile), LOCK_EX | LOCK_NB) != 0) {
162 logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", path, strerror(errno));
163 fclose(mesh->conffile);
164 mesh->conffile = NULL;
165 meshlink_errno = MESHLINK_EBUSY;
174 /// Unlock the main configuration file.
175 void main_config_unlock(meshlink_handle_t *mesh) {
177 fclose(mesh->conffile);
178 mesh->conffile = NULL;
182 /// Read a configuration file from a FILE handle.
183 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config) {
187 if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) || fseek(f, 0, SEEK_SET)) {
188 logger(mesh, MESHLINK_ERROR, "Cannot get config file size: %s\n", strerror(errno));
189 meshlink_errno = MESHLINK_ESTORAGE;
194 uint8_t *buf = xmalloc(len);
196 if(fread(buf, len, 1, f) != 1) {
197 logger(mesh, MESHLINK_ERROR, "Cannot read config file: %s\n", strerror(errno));
198 meshlink_errno = MESHLINK_ESTORAGE;
203 if(mesh->config_key) {
204 uint8_t *decrypted = xmalloc(len);
205 size_t decrypted_len = len;
206 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
207 chacha_poly1305_set_key(ctx, mesh->config_key);
209 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
211 config->buf = decrypted;
212 config->len = decrypted_len;
215 logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
216 meshlink_errno = MESHLINK_ESTORAGE;
229 /// Write a configuration file to a FILE handle.
230 bool config_write_file(meshlink_handle_t *mesh, FILE *f, const config_t *config) {
231 if(mesh->config_key) {
232 uint8_t buf[config->len + 16];
233 size_t len = sizeof(buf);
235 randomize(&seqbuf, sizeof(seqbuf));
236 chacha_poly1305_ctx_t *ctx = chacha_poly1305_init();
237 chacha_poly1305_set_key(ctx, mesh->config_key);
238 bool success = false;
240 if(chacha_poly1305_encrypt_iv96(ctx, seqbuf, config->buf, config->len, buf, &len)) {
241 success = fwrite(seqbuf, sizeof(seqbuf), 1, f) == 1 && fwrite(buf, len, 1, f) == 1;
243 logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
244 meshlink_errno = MESHLINK_ESTORAGE;
247 chacha_poly1305_exit(ctx);
251 if(fwrite(config->buf, config->len, 1, f) != 1) {
252 logger(mesh, MESHLINK_ERROR, "Cannot write config file: %s", strerror(errno));
253 meshlink_errno = MESHLINK_ESTORAGE;
260 /// Free resources of a loaded configuration file.
261 void config_free(config_t *config) {
262 free((uint8_t *)config->buf);
267 /// Check the presence of a host configuration file.
268 bool config_exists(meshlink_handle_t *mesh, const char *name) {
270 make_host_path(mesh, name, path, sizeof(path));
272 return access(path, F_OK) == 0;
275 /// Read a host configuration file.
276 bool config_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
278 make_host_path(mesh, name, path, sizeof(path));
280 FILE *f = fopen(path, "r");
283 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
287 if(!config_read_file(mesh, f, config)) {
288 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
297 /// Write a host configuration file.
298 bool config_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
300 make_host_path(mesh, name, path, sizeof(path));
302 FILE *f = fopen(path, "w");
305 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
309 if(!config_write_file(mesh, f, config)) {
310 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
319 /// Read the main configuration file.
320 bool main_config_read(meshlink_handle_t *mesh, config_t *config) {
322 make_main_path(mesh, path, sizeof(path));
324 FILE *f = fopen(path, "r");
327 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
331 if(!config_read_file(mesh, f, config)) {
332 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
341 /// Write the main configuration file.
342 bool main_config_write(meshlink_handle_t *mesh, const config_t *config) {
344 make_main_path(mesh, path, sizeof(path));
346 FILE *f = fopen(path, "w");
349 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
353 if(!config_write_file(mesh, f, config)) {
354 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
363 /// Read an invitation file, and immediately delete it.
364 bool invitation_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
366 char used_path[PATH_MAX];
367 make_invitation_path(mesh, name, path, sizeof(path));
368 make_used_invitation_path(mesh, name, used_path, sizeof(used_path));
370 // Atomically rename the invitation file
371 if(rename(path, used_path)) {
372 if(errno == ENOENT) {
373 logger(mesh, MESHLINK_ERROR, "Peer tried to use non-existing invitation %s\n", name);
375 logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
381 FILE *f = fopen(used_path, "r");
384 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
388 // Check the timestamp
391 if(fstat(fileno(f), &st)) {
392 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
398 if(time(NULL) > st.st_mtime + mesh->invitation_timeout) {
399 logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name);
405 if(!config_read_file(mesh, f, config)) {
406 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
417 /// Write an invitation file.
418 bool invitation_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
420 make_invitation_path(mesh, name, path, sizeof(path));
422 FILE *f = fopen(path, "w");
425 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
429 if(!config_write_file(mesh, f, config)) {
430 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
439 /// Purge old invitation files
440 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
442 make_invitation_path(mesh, "", path, sizeof(path));
444 DIR *dir = opendir(path);
447 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
448 meshlink_errno = MESHLINK_ESTORAGE;
456 while((ent = readdir(dir))) {
457 if(strlen(ent->d_name) != 24) {
461 char invname[PATH_MAX];
464 if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", path, ent->d_name) >= PATH_MAX) {
465 logger(mesh, MESHLINK_DEBUG, "Filename too long: %s" SLASH "%s", path, ent->d_name);
469 if(!stat(invname, &st)) {
470 if(mesh->invitation_key && deadline < st.st_mtime) {
476 logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
482 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
484 meshlink_errno = MESHLINK_ESTORAGE;