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) {
40 fprintf(stderr, "Fix y/n? ");
42 if(!fgets(buf, sizeof buf, stdin)) {
46 if(buf[0] == 'y' || buf[0] == 'Y')
48 if(buf[0] == 'n' || buf[0] == 'N')
53 static void print_tinc_cmd(const char *argv0, const char *format, ...) {
55 fprintf(stderr, "%s -c %s ", argv0, confbase);
57 fprintf(stderr, "%s -n %s ", argv0, netname);
59 fprintf(stderr, "%s ", argv0);
62 vfprintf(stderr, format, va);
67 static int strtailcmp(const char *str, const char *tail) {
68 size_t slen = strlen(str);
69 size_t tlen = strlen(tail);
72 return memcmp(str + slen - tlen, tail, tlen);
75 static void check_conffile(const char *fname, bool server) {
76 FILE *f = fopen(fname, "r");
78 fprintf(stderr, "ERROR: cannot read %s: %s\n", fname, strerror(errno));
85 const int maxvariables = 50;
86 int count[maxvariables];
87 memset(count, 0, sizeof count);
89 while(fgets(line, sizeof line, f)) {
91 if(!strncmp(line, "-----END", 8))
95 if(!strncmp(line, "-----BEGIN", 10)) {
102 char *variable, *value, *eol;
103 variable = value = line;
107 eol = line + strlen(line);
108 while(strchr("\t \r\n", *--eol))
111 if(!line[0] || line[0] == '#')
114 len = strcspn(value, "\t =");
116 value += strspn(value, "\t ");
119 value += strspn(value, "\t ");
121 variable[len] = '\0';
125 for(int i = 0; variables[i].name; i++) {
126 if(strcasecmp(variables[i].name, variable))
131 if(variables[i].type & VAR_OBSOLETE) {
132 fprintf(stderr, "WARNING: obsolete variable %s in %s line %d\n", variable, fname, lineno);
140 fprintf(stderr, "WARNING: unknown variable %s in %s line %d\n", variable, fname, lineno);
143 fprintf(stderr, "ERROR: no value for variable %s in %s line %d\n", variable, fname, lineno);
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);
152 fprintf(stderr, "ERROR: while reading %s: %s\n", fname, strerror(errno));
157 int fsck(const char *argv0) {
161 uid_t uid = getuid();
164 // Check that tinc.conf is readable.
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) {
173 fprintf(stderr, "You are currently not running tinc as root. Use sudo?\n");
175 fprintf(stderr, "Check the permissions of each component of the path %s.\n", tinc_conf);
180 char *name = get_my_name(true);
182 fprintf(stderr, "ERROR: tinc cannot run without a valid Name.\n");
186 // Check for private keys.
187 // TODO: use RSAPrivateKeyFile and Ed25519PrivateKeyFile variables if present.
190 char fname[PATH_MAX];
191 char dname[PATH_MAX];
193 #ifndef DISABLE_LEGACY
194 rsa_t *rsa_priv = NULL;
195 snprintf(fname, sizeof fname, "%s/rsa_key.priv", confbase);
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");
205 FILE *f = fopen(fname, "r");
207 fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
210 rsa_priv = rsa_read_pem_private_key(f);
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");
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));
227 fprintf(stderr, "Fixed permissions of %s.\n", fname);
233 ecdsa_t *ecdsa_priv = NULL;
234 snprintf(fname, sizeof fname, "%s/ed25519_key.priv", confbase);
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");
244 FILE *f = fopen(fname, "r");
246 fprintf(stderr, "ERROR: could not open %s: %s\n", fname, strerror(errno));
249 ecdsa_priv = ecdsa_read_pem_private_key(f);
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");
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));
266 fprintf(stderr, "Fixed permissions of %s.\n", fname);
271 #ifdef DISABLE_LEGACY
273 fprintf(stderr, "ERROR: No Ed25519 private key found.\n");
275 if(!rsa_priv && !ecdsa_priv) {
276 fprintf(stderr, "ERROR: Neither RSA or Ed25519 private key found.\n");
278 fprintf(stderr, "You can generate new keys with:\n\n");
279 print_tinc_cmd(argv0, "generate-keys");
283 // Check for public keys.
284 // TODO: use RSAPublicKeyFile and Ed25519PublicKeyFile variables if present.
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);
292 #ifndef DISABLE_LEGACY
293 rsa_t *rsa_pub = NULL;
295 f = fopen(fname, "r");
297 rsa_pub = rsa_read_pem_public_key(f);
302 fprintf(stderr, "WARNING: No (usable) public RSA key found.\n");
304 FILE *f = fopen(fname, "a");
306 if(rsa_write_pem_public_key(rsa_priv, f))
307 fprintf(stderr, "Wrote RSA public key to %s.\n", fname);
309 fprintf(stderr, "ERROR: could not write RSA public key to %s.\n", fname);
312 fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
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");
322 char buf1[len], buf2[len], buf3[len];
323 randomize(buf1, sizeof buf1);
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");
331 if(!rsa_private_decrypt(rsa_priv, buf2, sizeof buf2, buf3)) {
332 fprintf(stderr, "ERROR: private RSA key does not work.\n");
335 if(memcmp(buf1, buf3, sizeof buf1)) {
336 fprintf(stderr, "ERROR: public and private RSA keys do not match.\n");
342 fprintf(stderr, "WARNING: A public RSA key was found but no private key is known.\n");
346 // TODO: this should read the Ed25519PublicKey config variable instead.
347 ecdsa_t *ecdsa_pub = NULL;
349 f = fopen(fname, "r");
351 ecdsa_pub = ecdsa_read_pem_public_key(f);
356 fprintf(stderr, "WARNING: No (usable) public Ed25519 key found.\n");
358 FILE *f = fopen(fname, "a");
360 if(ecdsa_write_pem_public_key(ecdsa_priv, f))
361 fprintf(stderr, "Wrote Ed25519 public key to %s.\n", fname);
363 fprintf(stderr, "ERROR: could not write Ed25519 public key to %s.\n", fname);
366 fprintf(stderr, "ERROR: could not append to %s: %s\n", fname, strerror(errno));
370 // TODO: suggest remedies
371 char *key1 = ecdsa_get_base64_public_key(ecdsa_pub);
373 fprintf(stderr, "ERROR: public Ed25519 key does not work.\n");
376 char *key2 = ecdsa_get_base64_public_key(ecdsa_priv);
379 fprintf(stderr, "ERROR: private Ed25519 key does not work.\n");
382 int result = strcmp(key1, key2);
386 fprintf(stderr, "ERROR: public and private Ed25519 keys do not match.\n");
392 fprintf(stderr, "WARNING: A public Ed25519 key was found but no private key is known.\n");
395 // Check whether scripts are executable
398 DIR *dir = opendir(confbase);
400 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", confbase, strerror(errno));
404 while((ent = readdir(dir))) {
405 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down"))
408 strncpy(fname, ent->d_name, sizeof fname);
409 char *dash = strrchr(fname, '-');
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);
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");
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));
431 fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
433 if(chmod(fname, 0755))
434 fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
440 snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase);
441 dir = opendir(dname);
443 fprintf(stderr, "ERROR: cannot read directory %s: %s\n", dname, strerror(errno));
447 while((ent = readdir(dir))) {
448 if(strtailcmp(ent->d_name, "-up") && strtailcmp(ent->d_name, "-down"))
451 strncpy(fname, ent->d_name, sizeof fname);
452 char *dash = strrchr(fname, '-');
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));
463 fprintf(stderr, "WARNING: cannot read and execute %s: %s\n", fname, strerror(errno));
465 if(chmod(fname, 0755))
466 fprintf(stderr, "ERROR: cannot change permissions on %s: %s\n", fname, strerror(errno));
472 // Check for obsolete / unsafe / unknown configuration variables.
474 check_conffile(tinc_conf, true);
476 dir = opendir(dname);
478 while((ent = readdir(dir))) {
479 if(!check_id(ent->d_name))
482 snprintf(fname, sizeof fname, "%s" SLASH "hosts" SLASH "%s", confbase, ent->d_name);
483 check_conffile(fname, false);