Add the "fsck" command to the CLI.
[tinc] / src / fsck.c
1 /*
2     fsck.c -- Check the configuration files for problems
3     Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org>
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
22 #include "crypto.h"
23 #include "ecdsa.h"
24 #include "ecdsagen.h"
25 #include "fsck.h"
26 #include "names.h"
27 #ifndef DISABLE_LEGACY
28 #include "rsa.h"
29 #include "rsagen.h"
30 #endif
31 #include "tincctl.h"
32 #include "utils.h"
33
34 static bool ask_fix(void) {
35         if(force)
36                 return true;
37         if(!tty)
38                 return false;
39 again:
40         fprintf(stderr, "Fix y/n? ");
41         char buf[1024];
42         if(!fgets(buf, sizeof buf, stdin)) {
43                 tty = false;
44                 return false;
45         }
46         if(buf[0] == 'y' || buf[0] == 'Y')
47                 return true;
48         if(buf[0] == 'n' || buf[0] == 'N')
49                 return false;
50         goto again;
51 }
52
53 static void print_tinc_cmd(const char *argv0, const char *format, ...) {
54         if(confbasegiven)
55                 fprintf(stderr, "%s -c %s ", argv0, confbase);
56         else if(netname)
57                 fprintf(stderr, "%s -n %s ", argv0, netname);
58         else
59                 fprintf(stderr, "%s ", argv0);
60         va_list va;
61         va_start(va, format);
62         vfprintf(stderr, format, va);
63         va_end(va);
64         fputc('\n', stderr);
65 }
66
67 static int strtailcmp(const char *str, const char *tail) {
68         size_t slen = strlen(str);
69         size_t tlen = strlen(tail);
70         if(tlen > slen)
71                 return -1;
72         return memcmp(str + slen - tlen, tail, tlen);
73 }
74
75 static void check_conffile(const char *fname, bool server) {
76         FILE *f = fopen(fname, "r");
77         if(!f) {
78                 fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
79                 return;
80         }
81
82         char line[2048];
83         int lineno = 0;
84         bool skip = false;
85         const int maxvariables = 50;
86         int count[maxvariables];
87         memset(count, 0, sizeof count);
88
89         while(fgets(line, sizeof line, f)) {
90                 if(skip) {
91                         if(!strncmp(line, "-----END", 8))
92                                 skip = false;
93                         continue;
94                 } else {
95                         if(!strncmp(line, "-----BEGIN", 10)) {
96                                 skip = true;
97                                 continue;
98                         }
99                 }
100
101                 int len;
102                 char *variable, *value, *eol;
103                 variable = value = line;
104
105                 lineno++;
106
107                 eol = line + strlen(line);
108                 while(strchr("\t \r\n", *--eol))
109                         *eol = '\0';
110
111                 if(!line[0] || line[0] == '#')
112                         continue;
113
114                 len = strcspn(value, "\t =");
115                 value += len;
116                 value += strspn(value, "\t ");
117                 if(*value == '=') {
118                         value++;
119                         value += strspn(value, "\t ");
120                 }
121                 variable[len] = '\0';
122
123                 bool found = false;
124
125                 for(int i = 0; variables[i].name; i++) {
126                         if(strcasecmp(variables[i].name, variable))
127                                 continue;
128
129                         found = true;
130
131                         if(variables[i].type & VAR_OBSOLETE) {
132                                 fprintf(stderr, "WARNING: obsolete variable %s in %s line %d\n", variable, fname, lineno);
133                         }
134
135                         if(i < maxvariables)
136                                 count[i]++;
137                 }
138
139                 if(!found)
140                         fprintf(stderr, "WARNING: unknown variable %s in %s line %d\n", variable, fname, lineno);
141
142                 if(!*value)
143                         fprintf(stderr, "ERROR: no value for variable %s in %s line %d\n", variable, fname, lineno);
144         }
145
146         for(int i = 0; variables[i].name && i < maxvariables; i++) {
147                 if(count[i] > 1 && !(variables[i].type & VAR_MULTIPLE))
148                         fprintf(stderr, "WARNING: multiple instances of variable %s in %s\n", variables[i].name, fname);
149         }
150
151         if(ferror(f))
152                 fprintf(stderr, "ERROR: while reading %s: %s\n", fname, strerror(errno));
153
154         fclose(f);
155 }
156
157 int fsck(const char *argv0) {
158         uid_t uid = getuid();
159
160         // Check that tinc.conf is readable.
161
162         if(access(tinc_conf, R_OK)) {
163                 fprintf(stderr, "ERROR: cannot read %s: %s\n", tinc_conf, strerror(errno));
164                 if(errno == ENOENT) {
165                         fprintf(stderr, "No tinc configuration found. Create a new one with:\n\n");
166                         print_tinc_cmd(argv0, "init");
167                 } else if(errno == EACCES) {
168                         if(uid != 0)
169                                 fprintf(stderr, "You are currently not running tinc as root. Use sudo?\n");
170                         else
171                                 fprintf(stderr, "Check the permissions of each component of the path %s.\n", tinc_conf);
172                 }
173                 return 1;
174         }
175
176         char *name = get_my_name(true);
177         if(!name) {
178                 fprintf(stderr, "ERROR: tinc cannot run without a valid Name.\n");
179                 return 1;
180         }
181
182         // Check for private keys.
183         // TODO: use RSAPrivateKeyFile and Ed25519PrivateKeyFile variables if present.
184
185         struct stat st;
186         char fname[PATH_MAX];
187         char dname[PATH_MAX];
188
189 #ifndef DISABLE_LEGACY
190         rsa_t *rsa_priv = NULL;
191         snprintf(fname, sizeof fname, "%s/rsa_key.priv", confbase);
192
193         if(stat(fname, &st)) {
194                 if(errno != ENOENT) {
195                         // Something is seriously wrong here. If we can access the directory with tinc.conf in it, we should certainly be able to stat() an existing file.
196                         fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
197                         fprintf(stderr, "Please correct this error.\n");
198                         return 1;
199                 }
200         } else {
201                 FILE *f = fopen(fname, "r");
202                 if(!f) {
203                         fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
204                         return 1;
205                 }
206                 rsa_priv = rsa_read_pem_private_key(f);
207                 fclose(f);
208                 if(!rsa_priv) {
209                         fprintf(stderr, "ERROR: No key or unusable key found in %s.\n", fname);
210                         fprintf(stderr, "You can generate a new RSA key with:\n\n");
211                         print_tinc_cmd(argv0, "generate-rsa-keys");
212                         return 1;
213                 }
214
215                 if(st.st_mode & 077) {
216                         fprintf(stderr, "WARNING: unsafe file permissions on %s.\n", fname);
217                         if(st.st_uid != uid) {
218                                 fprintf(stderr, "You are not running %s as the same uid as %s.\n", argv0, fname);
219                         } else if(ask_fix()) {
220                                 if(chmod(fname, st.st_mode & ~077))
221                                         fprintf(stderr, "ERROR: could not change permissions of %s: %s\n", fname, strerror(errno));
222                                 else
223                                         fprintf(stderr, "Fixed permissions of %s.\n", fname);
224                         }
225                 }
226         }
227 #endif
228
229         ecdsa_t *ecdsa_priv = NULL;
230         snprintf(fname, sizeof fname, "%s/ed25519_key.priv", confbase);
231
232         if(stat(fname, &st)) {
233                 if(errno != ENOENT) {
234                         // Something is seriously wrong here. If we can access the directory with tinc.conf in it, we should certainly be able to stat() an existing file.
235                         fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
236                         fprintf(stderr, "Please correct this error.\n");
237                         return 1;
238                 }
239         } else {
240                 FILE *f = fopen(fname, "r");
241                 if(!f) {
242                         fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
243                         return 1;
244                 }
245                 ecdsa_priv = ecdsa_read_pem_private_key(f);
246                 fclose(f);
247                 if(!ecdsa_priv) {
248                         fprintf(stderr, "ERROR: No key or unusable key found in %s.\n", fname);
249                         fprintf(stderr, "You can generate a new Ed25519 key with:\n\n");
250                         print_tinc_cmd(argv0, "generate-ed25519-keys");
251                         return 1;
252                 }
253
254                 if(st.st_mode & 077) {
255                         fprintf(stderr, "WARNING: unsafe file permissions on %s.\n", fname);
256                         if(st.st_uid != uid) {
257                                 fprintf(stderr, "You are not running %s as the same uid as %s.\n", argv0, fname);
258                         } else if(ask_fix()) {
259                                 if(chmod(fname, st.st_mode & ~077))
260                                         fprintf(stderr, "ERROR: could not change permissions of %s: %s\n", fname, strerror(errno));
261                                 else
262                                         fprintf(stderr, "Fixed permissions of %s.\n", fname);
263                         }
264                 }
265         }
266
267 #ifdef DISABLE_LEGACY
268         if(!ecdsa_priv) {
269                 fprintf(stderr, "ERROR: No Ed25519 private key found.\n");
270 #else
271         if(!rsa_priv && !ecdsa_priv) {
272                 fprintf(stderr, "ERROR: Neither RSA or Ed25519 private key found.\n");
273 #endif
274                 fprintf(stderr, "You can generate new keys with:\n\n");
275                 print_tinc_cmd(argv0, "generate-keys");
276                 return 1;
277         }
278
279         // Check for public keys.
280         // TODO: use RSAPublicKeyFile and Ed25519PublicKeyFile variables if present.
281
282         snprintf(fname, sizeof fname, "%s/hosts/%s", confbase, name);
283         if(access(fname, R_OK))
284                 fprintf(stderr, "WARNING: cannot read %s\n", fname);
285
286         FILE *f;
287
288 #ifndef DISABLE_LEGACY
289         rsa_t *rsa_pub = NULL;
290
291         f = fopen(fname, "r");
292         if(f)
293                 rsa_pub = rsa_read_pem_public_key(f);
294         fclose(f);
295
296         if(rsa_priv) {
297                 if(!rsa_pub) {
298                         fprintf(stderr, "WARNING: No (usable) public RSA key found.\n");
299                         if(ask_fix()) {
300                                 FILE *f = fopen(fname, "a");
301                                 if(f) {
302                                         if(rsa_write_pem_public_key(rsa_priv, f))
303                                                 fprintf(stderr, "Wrote RSA public key to %s.\n", fname);
304                                         else
305                                                 fprintf(stderr, "ERROR: could not write RSA public key to %s.\n", fname);
306                                         fclose(f);
307                                 } else {
308                                         fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
309                                 }
310                         }
311                 } else {
312                         // TODO: suggest remedies
313                         size_t len = rsa_size(rsa_priv);
314                         if(len != rsa_size(rsa_pub)) {
315                                 fprintf(stderr, "ERROR: public and private RSA keys do not match.\n");
316                                 return 1;
317                         }
318                         char buf1[len], buf2[len], buf3[len];
319                         randomize(buf1, sizeof buf1);
320                         buf1[0] &= 0x7f;
321                         memset(buf2, 0, sizeof buf2);
322                         memset(buf3, 0, sizeof buf2);
323                         if(!rsa_public_encrypt(rsa_pub, buf1, sizeof buf1, buf2)) {
324                                 fprintf(stderr, "ERROR: public RSA key does not work.\n");
325                                 return 1;
326                         }
327                         if(!rsa_private_decrypt(rsa_priv, buf2, sizeof buf2, buf3)) {
328                                 fprintf(stderr, "ERROR: private RSA key does not work.\n");
329                                 return 1;
330                         }
331                         if(memcmp(buf1, buf3, sizeof buf1)) {
332                                 fprintf(stderr, "ERROR: public and private RSA keys do not match.\n");
333                                 return 1;
334                         }
335                 }
336         } else {
337                 if(rsa_pub)
338                         fprintf(stderr, "WARNING: A public RSA key was found but no private key is known.\n");
339         }
340 #endif
341         //
342         // TODO: this should read the Ed25519PublicKey config variable instead.
343         ecdsa_t *ecdsa_pub = NULL;
344
345         f = fopen(fname, "r");
346         if(f)
347                 ecdsa_pub = ecdsa_read_pem_public_key(f);
348         fclose(f);
349
350         if(ecdsa_priv) {
351                 if(!ecdsa_pub) {
352                         fprintf(stderr, "WARNING: No (usable) public Ed25519 key found.\n");
353                         if(ask_fix()) {
354                                 FILE *f = fopen(fname, "a");
355                                 if(f) {
356                                         if(ecdsa_write_pem_public_key(ecdsa_priv, f))
357                                                 fprintf(stderr, "Wrote Ed25519 public key to %s.\n", fname);
358                                         else
359                                                 fprintf(stderr, "ERROR: could not write Ed25519 public key to %s.\n", fname);
360                                         fclose(f);
361                                 } else {
362                                         fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
363                                 }
364                         }
365                 } else {
366                         // TODO: suggest remedies
367                         char *key1 = ecdsa_get_base64_public_key(ecdsa_pub);
368                         if(!key1) {
369                                 fprintf(stderr, "ERROR: public Ed25519 key does not work.\n");
370                                 return 1;
371                         }
372                         char *key2 = ecdsa_get_base64_public_key(ecdsa_priv);
373                         if(!key2) {
374                                 free(key1);
375                                 fprintf(stderr, "ERROR: private Ed25519 key does not work.\n");
376                                 return 1;
377                         }
378                         int result = strcmp(key1, key2);
379                         free(key1);
380                         free(key2);
381                         if(result) {
382                                 fprintf(stderr, "ERROR: public and private Ed25519 keys do not match.\n");
383                                 return 1;
384                         }
385                 }
386         } else {
387                 if(ecdsa_pub)
388                         fprintf(stderr, "WARNING: A public Ed25519 key was found but no private key is known.\n");
389         }
390
391         // Check whether scripts are executable
392
393         struct dirent *ent;
394         DIR *dir = opendir(confbase);
395         if(!dir) {
396                 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", confbase, strerror(errno));
397                 return 1;
398         }
399
400         while((ent = readdir(dir))) {
401                 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down"))
402                         continue;
403
404                 strncpy(fname, ent->d_name, sizeof fname);
405                 char *dash = strrchr(fname, '-');
406                 if(!dash)
407                         continue;
408                 *dash = 0;
409
410                 if(strcmp(fname, "tinc") && strcmp(fname, "host") && strcmp(fname, "subnet")) {
411                         static bool explained = false;
412                         fprintf(stderr, "WARNING: Unknown script %s" SLASH "%s found.\n", confbase, ent->d_name);
413                         if(!explained) {
414                                 fprintf(stderr, "The only scripts in %s executed by tinc are:\n", confbase);
415                                 fprintf(stderr, "tinc-up, tinc-down, host-up, host-down, subnet-up and subnet-down.\n");
416                                 explained = true;
417                         }
418                         continue;
419                 }
420
421                 snprintf(fname, sizeof fname, "%s" SLASH "%s", confbase, ent->d_name);
422                 if(access(fname, R_OK | X_OK)) {
423                         if(errno != EACCES) {
424                                 fprintf(stderr, "ERROR: cannot access %s: %s\n", fname, strerror(errno));
425                                 continue;
426                         }
427                         fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
428                         if(ask_fix()) {
429                                 if(chmod(fname, 0755))
430                                         fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
431                         }
432                 }
433         }
434         closedir(dir);
435
436         snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
437         dir = opendir(dname);
438         if(!dir) {
439                 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", dname, strerror(errno));
440                 return 1;
441         }
442
443         while((ent = readdir(dir))) {
444                 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down"))
445                         continue;
446
447                 strncpy(fname, ent->d_name, sizeof fname);
448                 char *dash = strrchr(fname, '-');
449                 if(!dash)
450                         continue;
451                 *dash = 0;
452
453                 snprintf(fname, sizeof fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
454                 if(access(fname, R_OK | X_OK)) {
455                         if(errno != EACCES) {
456                                 fprintf(stderr, "ERROR: cannot access %s: %s\n", fname, strerror(errno));
457                                 continue;
458                         }
459                         fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
460                         if(ask_fix()) {
461                                 if(chmod(fname, 0755))
462                                         fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
463                         }
464                 }
465         }
466         closedir(dir);
467         
468         // Check for obsolete / unsafe / unknown configuration variables.
469
470         check_conffile(tinc_conf, true);
471
472         dir = opendir(dname);
473         if(dir) {
474                 while((ent = readdir(dir))) {
475                         if(!check_id(ent->d_name))
476                                 continue;
477
478                         snprintf(fname, sizeof fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
479                         check_conffile(fname, false);
480                 }
481                 closedir(dir);
482         }
483
484         return 0;
485 }
486