]> git.meshlink.io Git - meshlink/blob - src/conf.c
Add support for encrypted storage.
[meshlink] / src / conf.c
1 /*
2     econf.c -- configuration code
3     Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21 #include <assert.h>
22
23 #include "conf.h"
24 #include "crypto.h"
25 #include "logger.h"
26 #include "meshlink_internal.h"
27 #include "xalloc.h"
28 #include "packmsg.h"
29
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);
33 }
34
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);
38 }
39
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);
43 }
44
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);
48 }
49
50 /// Remove a directory recursively
51 static void deltree(const char *dirname) {
52         DIR *d = opendir(dirname);
53
54         if(d) {
55                 struct dirent *ent;
56
57                 while((ent = readdir(d))) {
58                         if(ent->d_name[0] == '.') {
59                                 continue;
60                         }
61
62                         char filename[PATH_MAX];
63                         snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
64
65                         if(unlink(filename)) {
66                                 deltree(filename);
67                         }
68                 }
69
70                 closedir(d);
71         }
72
73         rmdir(dirname);
74 }
75
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));
80                 return false;
81         }
82
83         char path[PATH_MAX];
84
85         // Remove meshlink.conf
86         snprintf(path, sizeof(path), "%s" SLASH "meshlink.conf", mesh->confbase);
87         unlink(path);
88
89         // Remove any host config files
90         snprintf(path, sizeof(path), "%s" SLASH "hosts", mesh->confbase);
91         deltree(path);
92
93         if(mkdir(path, 0700) && errno != EEXIST) {
94                 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
95                 return false;
96         }
97
98         // Remove any invitation files
99         snprintf(path, sizeof(path), "%s" SLASH "invitations", mesh->confbase);
100         deltree(path);
101
102         if(mkdir(path, 0700) && errno != EEXIST) {
103                 logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", path, strerror(errno));
104                 return false;
105         }
106
107         return true;
108 }
109
110 /// Wipe an existing configuration directory
111 bool config_destroy(const char *confbase) {
112         char path[PATH_MAX];
113
114         // Remove meshlink.conf
115         snprintf(path, sizeof(path), "%s" SLASH "meshlink.conf", confbase);
116
117         if(unlink(path)) {
118                 if(errno == ENOENT) {
119                         meshlink_errno = MESHLINK_ENOENT;
120                         return false;
121                 } else {
122                         logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", path, strerror(errno));
123                         meshlink_errno = MESHLINK_ESTORAGE;
124                         return false;
125                 }
126         }
127
128         deltree(confbase);
129         return true;
130 }
131
132 /// Check the presence of the main configuration file.
133 bool main_config_exists(meshlink_handle_t *mesh) {
134         char path[PATH_MAX];
135         make_main_path(mesh, path, sizeof(path));
136
137         return access(path, F_OK) == 0;
138 }
139
140 /// Lock the main configuration file.
141 bool main_config_lock(meshlink_handle_t *mesh) {
142         char path[PATH_MAX];
143         make_main_path(mesh, path, sizeof(path));
144
145         mesh->conffile = fopen(path, "r");
146
147         if(!mesh->conffile) {
148                 logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", path, strerror(errno));
149                 meshlink_errno = MESHLINK_ESTORAGE;
150                 return false;
151         }
152
153 #ifdef FD_CLOEXEC
154         fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
155 #endif
156
157 #ifdef HAVE_MINGW
158         // TODO: use _locking()?
159 #else
160
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;
166                 return false;
167         }
168
169 #endif
170
171         return true;
172 }
173
174 /// Unlock the main configuration file.
175 void main_config_unlock(meshlink_handle_t *mesh) {
176         if(mesh->conffile) {
177                 fclose(mesh->conffile);
178                 mesh->conffile = NULL;
179         }
180 }
181
182 /// Read a configuration file from a FILE handle.
183 bool config_read_file(meshlink_handle_t *mesh, FILE *f, config_t *config) {
184         (void)mesh;
185         long len;
186
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;
190                 fclose(f);
191                 return false;
192         }
193
194         uint8_t *buf = xmalloc(len);
195
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;
199                 fclose(f);
200                 return false;
201         }
202
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);
208
209                 if(len > 12 && chacha_poly1305_decrypt_iv96(ctx, buf, buf + 12, len - 12, decrypted, &decrypted_len)) {
210                         free(buf);
211                         config->buf = decrypted;
212                         config->len = decrypted_len;
213                         return true;
214                 } else {
215                         logger(mesh, MESHLINK_ERROR, "Cannot decrypt config file\n");
216                         meshlink_errno = MESHLINK_ESTORAGE;
217                         free(decrypted);
218                         free(buf);
219                         return false;
220                 }
221         }
222
223         config->buf = buf;
224         config->len = len;
225
226         return true;
227 }
228
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);
234                 uint8_t seqbuf[12];
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;
239
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;
242                 } else {
243                         logger(mesh, MESHLINK_ERROR, "Cannot encrypt config file\n");
244                         meshlink_errno = MESHLINK_ESTORAGE;
245                 }
246
247                 chacha_poly1305_exit(ctx);
248                 return success;
249         }
250
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;
254                 return false;
255         }
256
257         return true;
258 }
259
260 /// Free resources of a loaded configuration file.
261 void config_free(config_t *config) {
262         free((uint8_t *)config->buf);
263         config->buf = NULL;
264         config->len = 0;
265 }
266
267 /// Check the presence of a host configuration file.
268 bool config_exists(meshlink_handle_t *mesh, const char *name) {
269         char path[PATH_MAX];
270         make_host_path(mesh, name, path, sizeof(path));
271
272         return access(path, F_OK) == 0;
273 }
274
275 /// Read a host configuration file.
276 bool config_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
277         char path[PATH_MAX];
278         make_host_path(mesh, name, path, sizeof(path));
279
280         FILE *f = fopen(path, "r");
281
282         if(!f) {
283                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
284                 return false;
285         }
286
287         if(!config_read_file(mesh, f, config)) {
288                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
289                 fclose(f);
290                 return false;
291         }
292
293         fclose(f);
294         return true;
295 }
296
297 /// Write a host configuration file.
298 bool config_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
299         char path[PATH_MAX];
300         make_host_path(mesh, name, path, sizeof(path));
301
302         FILE *f = fopen(path, "w");
303
304         if(!f) {
305                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
306                 return false;
307         }
308
309         if(!config_write_file(mesh, f, config)) {
310                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
311                 fclose(f);
312                 return false;
313         }
314
315         fclose(f);
316         return true;
317 }
318
319 /// Read the main configuration file.
320 bool main_config_read(meshlink_handle_t *mesh, config_t *config) {
321         char path[PATH_MAX];
322         make_main_path(mesh, path, sizeof(path));
323
324         FILE *f = fopen(path, "r");
325
326         if(!f) {
327                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
328                 return false;
329         }
330
331         if(!config_read_file(mesh, f, config)) {
332                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
333                 fclose(f);
334                 return false;
335         }
336
337         fclose(f);
338         return true;
339 }
340
341 /// Write the main configuration file.
342 bool main_config_write(meshlink_handle_t *mesh, const config_t *config) {
343         char path[PATH_MAX];
344         make_main_path(mesh, path, sizeof(path));
345
346         FILE *f = fopen(path, "w");
347
348         if(!f) {
349                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
350                 return false;
351         }
352
353         if(!config_write_file(mesh, f, config)) {
354                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
355                 fclose(f);
356                 return false;
357         }
358
359         fclose(f);
360         return true;
361 }
362
363 /// Read an invitation file, and immediately delete it.
364 bool invitation_read(meshlink_handle_t *mesh, const char *name, config_t *config) {
365         char path[PATH_MAX];
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));
369
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);
374                 } else {
375                         logger(mesh, MESHLINK_ERROR, "Error trying to rename invitation %s\n", name);
376                 }
377
378                 return false;
379         }
380
381         FILE *f = fopen(used_path, "r");
382
383         if(!f) {
384                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
385                 return false;
386         }
387
388         // Check the timestamp
389         struct stat st;
390
391         if(fstat(fileno(f), &st)) {
392                 logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", name);
393                 fclose(f);
394                 unlink(used_path);
395                 return false;
396         }
397
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);
400                 fclose(f);
401                 unlink(used_path);
402                 return false;
403         }
404
405         if(!config_read_file(mesh, f, config)) {
406                 logger(mesh, MESHLINK_ERROR, "Failed to read `%s': %s", path, strerror(errno));
407                 fclose(f);
408                 unlink(used_path);
409                 return false;
410         }
411
412         fclose(f);
413         unlink(used_path);
414         return true;
415 }
416
417 /// Write an invitation file.
418 bool invitation_write(meshlink_handle_t *mesh, const char *name, const config_t *config) {
419         char path[PATH_MAX];
420         make_invitation_path(mesh, name, path, sizeof(path));
421
422         FILE *f = fopen(path, "w");
423
424         if(!f) {
425                 logger(mesh, MESHLINK_ERROR, "Failed to open `%s': %s", path, strerror(errno));
426                 return false;
427         }
428
429         if(!config_write_file(mesh, f, config)) {
430                 logger(mesh, MESHLINK_ERROR, "Failed to write `%s': %s", path, strerror(errno));
431                 fclose(f);
432                 return false;
433         }
434
435         fclose(f);
436         return true;
437 }
438
439 /// Purge old invitation files
440 size_t invitation_purge_old(meshlink_handle_t *mesh, time_t deadline) {
441         char path[PATH_MAX];
442         make_invitation_path(mesh, "", path, sizeof(path));
443
444         DIR *dir = opendir(path);
445
446         if(!dir) {
447                 logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", path, strerror(errno));
448                 meshlink_errno = MESHLINK_ESTORAGE;
449                 return 0;
450         }
451
452         errno = 0;
453         size_t count = 0;
454         struct dirent *ent;
455
456         while((ent = readdir(dir))) {
457                 if(strlen(ent->d_name) != 24) {
458                         continue;
459                 }
460
461                 char invname[PATH_MAX];
462                 struct stat st;
463
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);
466                         continue;
467                 }
468
469                 if(!stat(invname, &st)) {
470                         if(mesh->invitation_key && deadline < st.st_mtime) {
471                                 count++;
472                         } else {
473                                 unlink(invname);
474                         }
475                 } else {
476                         logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
477                         errno = 0;
478                 }
479         }
480
481         if(errno) {
482                 logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", path, strerror(errno));
483                 closedir(dir);
484                 meshlink_errno = MESHLINK_ESTORAGE;
485                 return 0;
486         }
487
488         closedir(dir);
489
490         return count;
491 }