Clarify that scripts are called synchronously.
[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 #ifdef HAVE_MINGW
159         int uid = 0;
160 #else
161         uid_t uid = getuid();
162 #endif
163
164         // Check that tinc.conf is readable.
165
166         if(access(tinc_conf, R_OK)) {
167                 fprintf(stderr, "ERROR: cannot read %s: %s\n", tinc_conf, strerror(errno));
168                 if(errno == ENOENT) {
169                         fprintf(stderr, "No tinc configuration found. Create a new one with:\n\n");
170                         print_tinc_cmd(argv0, "init");
171                 } else if(errno == EACCES) {
172                         if(uid != 0)
173                                 fprintf(stderr, "You are currently not running tinc as root. Use sudo?\n");
174                         else
175                                 fprintf(stderr, "Check the permissions of each component of the path %s.\n", tinc_conf);
176                 }
177                 return 1;
178         }
179
180         char *name = get_my_name(true);
181         if(!name) {
182                 fprintf(stderr, "ERROR: tinc cannot run without a valid Name.\n");
183                 return 1;
184         }
185
186         // Check for private keys.
187         // TODO: use RSAPrivateKeyFile and Ed25519PrivateKeyFile variables if present.
188
189         struct stat st;
190         char fname[PATH_MAX];
191         char dname[PATH_MAX];
192
193 #ifndef DISABLE_LEGACY
194         rsa_t *rsa_priv = NULL;
195         snprintf(fname, sizeof fname, "%s/rsa_key.priv", confbase);
196
197         if(stat(fname, &st)) {
198                 if(errno != ENOENT) {
199                         // 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.
200                         fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
201                         fprintf(stderr, "Please correct this error.\n");
202                         return 1;
203                 }
204         } else {
205                 FILE *f = fopen(fname, "r");
206                 if(!f) {
207                         fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
208                         return 1;
209                 }
210                 rsa_priv = rsa_read_pem_private_key(f);
211                 fclose(f);
212                 if(!rsa_priv) {
213                         fprintf(stderr, "ERROR: No key or unusable key found in %s.\n", fname);
214                         fprintf(stderr, "You can generate a new RSA key with:\n\n");
215                         print_tinc_cmd(argv0, "generate-rsa-keys");
216                         return 1;
217                 }
218
219                 if(st.st_mode & 077) {
220                         fprintf(stderr, "WARNING: unsafe file permissions on %s.\n", fname);
221                         if(st.st_uid != uid) {
222                                 fprintf(stderr, "You are not running %s as the same uid as %s.\n", argv0, fname);
223                         } else if(ask_fix()) {
224                                 if(chmod(fname, st.st_mode & ~077))
225                                         fprintf(stderr, "ERROR: could not change permissions of %s: %s\n", fname, strerror(errno));
226                                 else
227                                         fprintf(stderr, "Fixed permissions of %s.\n", fname);
228                         }
229                 }
230         }
231 #endif
232
233         ecdsa_t *ecdsa_priv = NULL;
234         snprintf(fname, sizeof fname, "%s/ed25519_key.priv", confbase);
235
236         if(stat(fname, &st)) {
237                 if(errno != ENOENT) {
238                         // 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.
239                         fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
240                         fprintf(stderr, "Please correct this error.\n");
241                         return 1;
242                 }
243         } else {
244                 FILE *f = fopen(fname, "r");
245                 if(!f) {
246                         fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
247                         return 1;
248                 }
249                 ecdsa_priv = ecdsa_read_pem_private_key(f);
250                 fclose(f);
251                 if(!ecdsa_priv) {
252                         fprintf(stderr, "ERROR: No key or unusable key found in %s.\n", fname);
253                         fprintf(stderr, "You can generate a new Ed25519 key with:\n\n");
254                         print_tinc_cmd(argv0, "generate-ed25519-keys");
255                         return 1;
256                 }
257
258                 if(st.st_mode & 077) {
259                         fprintf(stderr, "WARNING: unsafe file permissions on %s.\n", fname);
260                         if(st.st_uid != uid) {
261                                 fprintf(stderr, "You are not running %s as the same uid as %s.\n", argv0, fname);
262                         } else if(ask_fix()) {
263                                 if(chmod(fname, st.st_mode & ~077))
264                                         fprintf(stderr, "ERROR: could not change permissions of %s: %s\n", fname, strerror(errno));
265                                 else
266                                         fprintf(stderr, "Fixed permissions of %s.\n", fname);
267                         }
268                 }
269         }
270
271 #ifdef DISABLE_LEGACY
272         if(!ecdsa_priv) {
273                 fprintf(stderr, "ERROR: No Ed25519 private key found.\n");
274 #else
275         if(!rsa_priv && !ecdsa_priv) {
276                 fprintf(stderr, "ERROR: Neither RSA or Ed25519 private key found.\n");
277 #endif
278                 fprintf(stderr, "You can generate new keys with:\n\n");
279                 print_tinc_cmd(argv0, "generate-keys");
280                 return 1;
281         }
282
283         // Check for public keys.
284         // TODO: use RSAPublicKeyFile and Ed25519PublicKeyFile variables if present.
285
286         snprintf(fname, sizeof fname, "%s/hosts/%s", confbase, name);
287         if(access(fname, R_OK))
288                 fprintf(stderr, "WARNING: cannot read %s\n", fname);
289
290         FILE *f;
291
292 #ifndef DISABLE_LEGACY
293         rsa_t *rsa_pub = NULL;
294
295         f = fopen(fname, "r");
296         if(f)
297                 rsa_pub = rsa_read_pem_public_key(f);
298         fclose(f);
299
300         if(rsa_priv) {
301                 if(!rsa_pub) {
302                         fprintf(stderr, "WARNING: No (usable) public RSA key found.\n");
303                         if(ask_fix()) {
304                                 FILE *f = fopen(fname, "a");
305                                 if(f) {
306                                         if(rsa_write_pem_public_key(rsa_priv, f))
307                                                 fprintf(stderr, "Wrote RSA public key to %s.\n", fname);
308                                         else
309                                                 fprintf(stderr, "ERROR: could not write RSA public key to %s.\n", fname);
310                                         fclose(f);
311                                 } else {
312                                         fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
313                                 }
314                         }
315                 } else {
316                         // TODO: suggest remedies
317                         size_t len = rsa_size(rsa_priv);
318                         if(len != rsa_size(rsa_pub)) {
319                                 fprintf(stderr, "ERROR: public and private RSA keys do not match.\n");
320                                 return 1;
321                         }
322                         char buf1[len], buf2[len], buf3[len];
323                         randomize(buf1, sizeof buf1);
324                         buf1[0] &= 0x7f;
325                         memset(buf2, 0, sizeof buf2);
326                         memset(buf3, 0, sizeof buf2);
327                         if(!rsa_public_encrypt(rsa_pub, buf1, sizeof buf1, buf2)) {
328                                 fprintf(stderr, "ERROR: public RSA key does not work.\n");
329                                 return 1;
330                         }
331                         if(!rsa_private_decrypt(rsa_priv, buf2, sizeof buf2, buf3)) {
332                                 fprintf(stderr, "ERROR: private RSA key does not work.\n");
333                                 return 1;
334                         }
335                         if(memcmp(buf1, buf3, sizeof buf1)) {
336                                 fprintf(stderr, "ERROR: public and private RSA keys do not match.\n");
337                                 return 1;
338                         }
339                 }
340         } else {
341                 if(rsa_pub)
342                         fprintf(stderr, "WARNING: A public RSA key was found but no private key is known.\n");
343         }
344 #endif
345         //
346         // TODO: this should read the Ed25519PublicKey config variable instead.
347         ecdsa_t *ecdsa_pub = NULL;
348
349         f = fopen(fname, "r");
350         if(f)
351                 ecdsa_pub = ecdsa_read_pem_public_key(f);
352         fclose(f);
353
354         if(ecdsa_priv) {
355                 if(!ecdsa_pub) {
356                         fprintf(stderr, "WARNING: No (usable) public Ed25519 key found.\n");
357                         if(ask_fix()) {
358                                 FILE *f = fopen(fname, "a");
359                                 if(f) {
360                                         if(ecdsa_write_pem_public_key(ecdsa_priv, f))
361                                                 fprintf(stderr, "Wrote Ed25519 public key to %s.\n", fname);
362                                         else
363                                                 fprintf(stderr, "ERROR: could not write Ed25519 public key to %s.\n", fname);
364                                         fclose(f);
365                                 } else {
366                                         fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
367                                 }
368                         }
369                 } else {
370                         // TODO: suggest remedies
371                         char *key1 = ecdsa_get_base64_public_key(ecdsa_pub);
372                         if(!key1) {
373                                 fprintf(stderr, "ERROR: public Ed25519 key does not work.\n");
374                                 return 1;
375                         }
376                         char *key2 = ecdsa_get_base64_public_key(ecdsa_priv);
377                         if(!key2) {
378                                 free(key1);
379                                 fprintf(stderr, "ERROR: private Ed25519 key does not work.\n");
380                                 return 1;
381                         }
382                         int result = strcmp(key1, key2);
383                         free(key1);
384                         free(key2);
385                         if(result) {
386                                 fprintf(stderr, "ERROR: public and private Ed25519 keys do not match.\n");
387                                 return 1;
388                         }
389                 }
390         } else {
391                 if(ecdsa_pub)
392                         fprintf(stderr, "WARNING: A public Ed25519 key was found but no private key is known.\n");
393         }
394
395         // Check whether scripts are executable
396
397         struct dirent *ent;
398         DIR *dir = opendir(confbase);
399         if(!dir) {
400                 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", confbase, strerror(errno));
401                 return 1;
402         }
403
404         while((ent = readdir(dir))) {
405                 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down"))
406                         continue;
407
408                 strncpy(fname, ent->d_name, sizeof fname);
409                 char *dash = strrchr(fname, '-');
410                 if(!dash)
411                         continue;
412                 *dash = 0;
413
414                 if(strcmp(fname, "tinc") && strcmp(fname, "host") && strcmp(fname, "subnet")) {
415                         static bool explained = false;
416                         fprintf(stderr, "WARNING: Unknown script %s" SLASH "%s found.\n", confbase, ent->d_name);
417                         if(!explained) {
418                                 fprintf(stderr, "The only scripts in %s executed by tinc are:\n", confbase);
419                                 fprintf(stderr, "tinc-up, tinc-down, host-up, host-down, subnet-up and subnet-down.\n");
420                                 explained = true;
421                         }
422                         continue;
423                 }
424
425                 snprintf(fname, sizeof fname, "%s" SLASH "%s", confbase, ent->d_name);
426                 if(access(fname, R_OK | X_OK)) {
427                         if(errno != EACCES) {
428                                 fprintf(stderr, "ERROR: cannot access %s: %s\n", fname, strerror(errno));
429                                 continue;
430                         }
431                         fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
432                         if(ask_fix()) {
433                                 if(chmod(fname, 0755))
434                                         fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
435                         }
436                 }
437         }
438         closedir(dir);
439
440         snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
441         dir = opendir(dname);
442         if(!dir) {
443                 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", dname, strerror(errno));
444                 return 1;
445         }
446
447         while((ent = readdir(dir))) {
448                 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down"))
449                         continue;
450
451                 strncpy(fname, ent->d_name, sizeof fname);
452                 char *dash = strrchr(fname, '-');
453                 if(!dash)
454                         continue;
455                 *dash = 0;
456
457                 snprintf(fname, sizeof fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
458                 if(access(fname, R_OK | X_OK)) {
459                         if(errno != EACCES) {
460                                 fprintf(stderr, "ERROR: cannot access %s: %s\n", fname, strerror(errno));
461                                 continue;
462                         }
463                         fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
464                         if(ask_fix()) {
465                                 if(chmod(fname, 0755))
466                                         fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
467                         }
468                 }
469         }
470         closedir(dir);
471         
472         // Check for obsolete / unsafe / unknown configuration variables.
473
474         check_conffile(tinc_conf, true);
475
476         dir = opendir(dname);
477         if(dir) {
478                 while((ent = readdir(dir))) {
479                         if(!check_id(ent->d_name))
480                                 continue;
481
482                         snprintf(fname, sizeof fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
483                         check_conffile(fname, false);
484                 }
485                 closedir(dir);
486         }
487
488         return 0;
489 }
490