2 fsck.c -- Check the configuration files for problems
3 Copyright (C) 2014 Guus Sliepen <guus@tinc-vpn.org>
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.
27 #ifndef DISABLE_LEGACY
34 static bool ask_fix(void) {
44 fprintf(stderr, "Fix y/n? ");
47 if(!fgets(buf, sizeof(buf), stdin)) {
52 if(buf[0] == 'y' || buf[0] == 'Y') {
56 if(buf[0] == 'n' || buf[0] == 'N') {
63 static void print_tinc_cmd(const char *argv0, const char *format, ...) {
65 fprintf(stderr, "%s -c %s ", argv0, confbase);
67 fprintf(stderr, "%s -n %s ", argv0, netname);
69 fprintf(stderr, "%s ", argv0);
74 vfprintf(stderr, format, va);
79 static int strtailcmp(const char *str, const char *tail) {
80 size_t slen = strlen(str);
81 size_t tlen = strlen(tail);
87 return memcmp(str + slen - tlen, tail, tlen);
90 static void check_conffile(const char *fname, bool server) {
93 FILE *f = fopen(fname, "r");
96 fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
103 const int maxvariables = 50;
104 int count[maxvariables];
105 memset(count, 0, sizeof(count));
107 while(fgets(line, sizeof(line), f)) {
109 if(!strncmp(line, "-----END", 8)) {
115 if(!strncmp(line, "-----BEGIN", 10)) {
122 char *variable, *value, *eol;
123 variable = value = line;
127 eol = line + strlen(line);
129 while(strchr("\t \r\n", *--eol)) {
133 if(!line[0] || line[0] == '#') {
137 len = strcspn(value, "\t =");
139 value += strspn(value, "\t ");
143 value += strspn(value, "\t ");
146 variable[len] = '\0';
150 for(int i = 0; variables[i].name; i++) {
151 if(strcasecmp(variables[i].name, variable)) {
157 if(variables[i].type & VAR_OBSOLETE) {
158 fprintf(stderr, "WARNING: obsolete variable %s in %s line %d\n", variable, fname, lineno);
161 if(i < maxvariables) {
167 fprintf(stderr, "WARNING: unknown variable %s in %s line %d\n", variable, fname, lineno);
171 fprintf(stderr, "ERROR: no value for variable %s in %s line %d\n", variable, fname, lineno);
175 for(int i = 0; variables[i].name && i < maxvariables; i++) {
176 if(count[i] > 1 && !(variables[i].type & VAR_MULTIPLE)) {
177 fprintf(stderr, "WARNING: multiple instances of variable %s in %s\n", variables[i].name, fname);
182 fprintf(stderr, "ERROR: while reading %s: %s\n", fname, strerror(errno));
188 int fsck(const char *argv0) {
192 uid_t uid = getuid();
195 // Check that tinc.conf is readable.
197 if(access(tinc_conf, R_OK)) {
198 fprintf(stderr, "ERROR: cannot read %s: %s\n", tinc_conf, strerror(errno));
200 if(errno == ENOENT) {
201 fprintf(stderr, "No tinc configuration found. Create a new one with:\n\n");
202 print_tinc_cmd(argv0, "init");
203 } else if(errno == EACCES) {
205 fprintf(stderr, "You are currently not running tinc as root. Use sudo?\n");
207 fprintf(stderr, "Check the permissions of each component of the path %s.\n", tinc_conf);
214 char *name = get_my_name(true);
217 fprintf(stderr, "ERROR: tinc cannot run without a valid Name.\n");
221 // Check for private keys.
222 // TODO: use RSAPrivateKeyFile and Ed25519PrivateKeyFile variables if present.
225 char fname[PATH_MAX];
226 char dname[PATH_MAX];
228 #ifndef DISABLE_LEGACY
229 rsa_t *rsa_priv = NULL;
230 snprintf(fname, sizeof(fname), "%s/rsa_key.priv", confbase);
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");
240 FILE *f = fopen(fname, "r");
243 fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
247 rsa_priv = rsa_read_pem_private_key(f);
251 fprintf(stderr, "ERROR: No key or unusable key found in %s.\n", fname);
252 fprintf(stderr, "You can generate a new RSA key with:\n\n");
253 print_tinc_cmd(argv0, "generate-rsa-keys");
257 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
259 if(st.st_mode & 077) {
260 fprintf(stderr, "WARNING: unsafe file permissions on %s.\n", fname);
262 if(st.st_uid != uid) {
263 fprintf(stderr, "You are not running %s as the same uid as %s.\n", argv0, fname);
264 } else if(ask_fix()) {
265 if(chmod(fname, st.st_mode & ~077)) {
266 fprintf(stderr, "ERROR: could not change permissions of %s: %s\n", fname, strerror(errno));
268 fprintf(stderr, "Fixed permissions of %s.\n", fname);
278 ecdsa_t *ecdsa_priv = NULL;
279 snprintf(fname, sizeof(fname), "%s/ed25519_key.priv", confbase);
281 if(stat(fname, &st)) {
282 if(errno != ENOENT) {
283 // 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.
284 fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
285 fprintf(stderr, "Please correct this error.\n");
289 FILE *f = fopen(fname, "r");
292 fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
296 ecdsa_priv = ecdsa_read_pem_private_key(f);
300 fprintf(stderr, "ERROR: No key or unusable key found in %s.\n", fname);
301 fprintf(stderr, "You can generate a new Ed25519 key with:\n\n");
302 print_tinc_cmd(argv0, "generate-ed25519-keys");
306 #if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
308 if(st.st_mode & 077) {
309 fprintf(stderr, "WARNING: unsafe file permissions on %s.\n", fname);
311 if(st.st_uid != uid) {
312 fprintf(stderr, "You are not running %s as the same uid as %s.\n", argv0, fname);
313 } else if(ask_fix()) {
314 if(chmod(fname, st.st_mode & ~077)) {
315 fprintf(stderr, "ERROR: could not change permissions of %s: %s\n", fname, strerror(errno));
317 fprintf(stderr, "Fixed permissions of %s.\n", fname);
325 #ifdef DISABLE_LEGACY
328 fprintf(stderr, "ERROR: No Ed25519 private key found.\n");
331 if(!rsa_priv && !ecdsa_priv) {
332 fprintf(stderr, "ERROR: Neither RSA or Ed25519 private key found.\n");
334 fprintf(stderr, "You can generate new keys with:\n\n");
335 print_tinc_cmd(argv0, "generate-keys");
339 // Check for public keys.
340 // TODO: use RSAPublicKeyFile variable if present.
342 snprintf(fname, sizeof(fname), "%s/hosts/%s", confbase, name);
344 if(access(fname, R_OK)) {
345 fprintf(stderr, "WARNING: cannot read %s\n", fname);
350 #ifndef DISABLE_LEGACY
351 rsa_t *rsa_pub = NULL;
353 f = fopen(fname, "r");
356 rsa_pub = rsa_read_pem_public_key(f);
362 fprintf(stderr, "WARNING: No (usable) public RSA key found.\n");
365 FILE *f = fopen(fname, "a");
368 if(rsa_write_pem_public_key(rsa_priv, f)) {
369 fprintf(stderr, "Wrote RSA public key to %s.\n", fname);
371 fprintf(stderr, "ERROR: could not write RSA public key to %s.\n", fname);
376 fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
380 // TODO: suggest remedies
381 size_t len = rsa_size(rsa_priv);
383 if(len != rsa_size(rsa_pub)) {
384 fprintf(stderr, "ERROR: public and private RSA keys do not match.\n");
388 char buf1[len], buf2[len], buf3[len];
389 randomize(buf1, sizeof(buf1));
391 memset(buf2, 0, sizeof(buf2));
392 memset(buf3, 0, sizeof(buf2));
394 if(!rsa_public_encrypt(rsa_pub, buf1, sizeof(buf1), buf2)) {
395 fprintf(stderr, "ERROR: public RSA key does not work.\n");
399 if(!rsa_private_decrypt(rsa_priv, buf2, sizeof(buf2), buf3)) {
400 fprintf(stderr, "ERROR: private RSA key does not work.\n");
404 if(memcmp(buf1, buf3, sizeof(buf1))) {
405 fprintf(stderr, "ERROR: public and private RSA keys do not match.\n");
411 fprintf(stderr, "WARNING: A public RSA key was found but no private key is known.\n");
417 ecdsa_t *ecdsa_pub = NULL;
419 f = fopen(fname, "r");
422 ecdsa_pub = get_pubkey(f);
426 ecdsa_pub = ecdsa_read_pem_public_key(f);
434 fprintf(stderr, "WARNING: No (usable) public Ed25519 key found.\n");
437 FILE *f = fopen(fname, "a");
440 if(ecdsa_write_pem_public_key(ecdsa_priv, f)) {
441 fprintf(stderr, "Wrote Ed25519 public key to %s.\n", fname);
443 fprintf(stderr, "ERROR: could not write Ed25519 public key to %s.\n", fname);
448 fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
452 // TODO: suggest remedies
453 char *key1 = ecdsa_get_base64_public_key(ecdsa_pub);
456 fprintf(stderr, "ERROR: public Ed25519 key does not work.\n");
460 char *key2 = ecdsa_get_base64_public_key(ecdsa_priv);
464 fprintf(stderr, "ERROR: private Ed25519 key does not work.\n");
468 int result = strcmp(key1, key2);
473 fprintf(stderr, "ERROR: public and private Ed25519 keys do not match.\n");
479 fprintf(stderr, "WARNING: A public Ed25519 key was found but no private key is known.\n");
483 // Check whether scripts are executable
486 DIR *dir = opendir(confbase);
489 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", confbase, strerror(errno));
493 while((ent = readdir(dir))) {
494 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down")) {
498 strncpy(fname, ent->d_name, sizeof(fname));
499 char *dash = strrchr(fname, '-');
507 if(strcmp(fname, "tinc") && strcmp(fname, "host") && strcmp(fname, "subnet")) {
508 static bool explained = false;
509 fprintf(stderr, "WARNING: Unknown script %s" SLASH "%s found.\n", confbase, ent->d_name);
512 fprintf(stderr, "The only scripts in %s executed by tinc are:\n", confbase);
513 fprintf(stderr, "tinc-up, tinc-down, host-up, host-down, subnet-up and subnet-down.\n");
520 snprintf(fname, sizeof(fname), "%s" SLASH "%s", confbase, ent->d_name);
522 if(access(fname, R_OK | X_OK)) {
523 if(errno != EACCES) {
524 fprintf(stderr, "ERROR: cannot access %s: %s\n", fname, strerror(errno));
528 fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
531 if(chmod(fname, 0755)) {
532 fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
540 snprintf(dname, sizeof(dname), "%s" SLASH "hosts", confbase);
541 dir = opendir(dname);
544 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", dname, strerror(errno));
548 while((ent = readdir(dir))) {
549 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down")) {
553 strncpy(fname, ent->d_name, sizeof(fname));
554 char *dash = strrchr(fname, '-');
562 snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
564 if(access(fname, R_OK | X_OK)) {
565 if(errno != EACCES) {
566 fprintf(stderr, "ERROR: cannot access %s: %s\n", fname, strerror(errno));
570 fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
573 if(chmod(fname, 0755)) {
574 fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
582 // Check for obsolete / unsafe / unknown configuration variables.
584 check_conffile(tinc_conf, true);
586 dir = opendir(dname);
589 while((ent = readdir(dir))) {
590 if(!check_id(ent->d_name)) {
594 snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
595 check_conffile(fname, false);