#include "tincctl.h"
#include "top.h"
-#ifdef HAVE_MINGW
-#define SCRIPTEXTENSION ".bat"
-#else
-#define SCRIPTEXTENSION ""
-#endif
-
static char **orig_argv;
static int orig_argc;
bool tty = true;
bool confbasegiven = false;
bool netnamegiven = false;
-
-#ifdef HAVE_MINGW
-static struct WSAData wsa_state;
-#endif
+char *scriptinterpreter = NULL;
+char *scriptextension = "";
static struct option const long_options[] = {
{"config", required_argument, NULL, 'c'},
return true;
}
+/* Open a file with the desired permissions, minus the umask.
+ Also, if we want to create an executable file, we call fchmod()
+ to set the executable bits. */
+
+FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
+ mode_t mask = umask(0);
+ perms &= ~mask;
+ umask(~perms);
+ FILE *f = fopen(filename, mode);
+#ifdef HAVE_FCHMOD
+ if((perms & 0444) && f)
+ fchmod(fileno(f), perms);
+#endif
+ umask(mask);
+ return f;
+}
+
static void disable_old_keys(const char *filename, const char *what) {
char tmpfile[PATH_MAX] = "";
char buf[1024];
snprintf(tmpfile, sizeof tmpfile, "%s.tmp", filename);
- w = fopen(tmpfile, "w");
-
-#ifdef HAVE_FCHMOD
- /* Let the temporary file have the same permissions as the original. */
-
- if(w) {
- struct stat st = {.st_mode = 0600};
- fstat(fileno(r), &st);
- fchmod(fileno(w), st.st_mode);
- }
-#endif
+ struct stat st = {.st_mode = 0600};
+ fstat(fileno(r), &st);
+ w = fopenmask(tmpfile, "w", st.st_mode);
while(fgets(buf, sizeof buf, r)) {
if(!block && !strncmp(buf, "-----BEGIN ", 11)) {
unlink(tmpfile);
}
-static FILE *ask_and_open(const char *filename, const char *what, const char *mode, bool ask) {
+static FILE *ask_and_open(const char *filename, const char *what, const char *mode, bool ask, mode_t perms) {
FILE *r;
char *directory;
char buf[PATH_MAX];
/* Check stdin and stdout */
if(ask && tty) {
/* Ask for a file and/or directory name. */
- fprintf(stdout, "Please enter a file to save %s to [%s]: ", what, filename);
- fflush(stdout);
+ fprintf(stderr, "Please enter a file to save %s to [%s]: ", what, filename);
if(fgets(buf, sizeof buf, stdin) == NULL) {
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
/* Open it first to keep the inode busy */
- r = fopen(filename, mode);
+ r = fopenmask(filename, mode, perms);
if(!r) {
fprintf(stderr, "Error opening file `%s': %s\n", filename, strerror(errno));
fprintf(stderr, "Done.\n");
xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase);
- f = ask_and_open(privname, "private ECDSA key", "a", ask);
+ f = ask_and_open(privname, "private ECDSA key", "a", ask, 0600);
free(privname);
if(!f)
return false;
-#ifdef HAVE_FCHMOD
- /* Make it unreadable for others. */
- fchmod(fileno(f), 0600);
-#endif
-
if(!ecdsa_write_pem_private_key(key, f)) {
fprintf(stderr, "Error writing private key!\n");
ecdsa_free(key);
else
xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase);
- f = ask_and_open(pubname, "public ECDSA key", "a", ask);
+ f = ask_and_open(pubname, "public ECDSA key", "a", ask, 0666);
free(pubname);
if(!f)
fprintf(stderr, "Done.\n");
xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
- f = ask_and_open(privname, "private RSA key", "a", ask);
+ f = ask_and_open(privname, "private RSA key", "a", ask, 0600);
free(privname);
if(!f)
return false;
-#ifdef HAVE_FCHMOD
- /* Make it unreadable for others. */
- fchmod(fileno(f), 0600);
-#endif
-
if(!rsa_write_pem_private_key(key, f)) {
fprintf(stderr, "Error writing private key!\n");
fclose(f);
else
xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
- f = ask_and_open(pubname, "public RSA key", "a", ask);
+ f = ask_and_open(pubname, "public RSA key", "a", ask, 0666);
free(pubname);
if(!f)
fclose(f);
-#ifdef HAVE_MINGW
- if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
- if(verbose)
- fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
- return false;
- }
-#endif
-
#ifndef HAVE_MINGW
struct sockaddr_un sa;
sa.sun_family = AF_UNIX;
{"IffOneQueue", VAR_SERVER},
{"Interface", VAR_SERVER},
{"KeyExpire", VAR_SERVER},
+ {"ListenAddress", VAR_SERVER | VAR_MULTIPLE},
{"LocalDiscovery", VAR_SERVER},
{"MACExpire", VAR_SERVER},
{"MaxConnectionBurst", VAR_SERVER},
if(action < 0 && !removed) {
remove(tmpfile);
fprintf(stderr, "No configuration variables deleted.\n");
- return *value;
+ return *value != 0;
}
// Replace the configuration file with the new one
fprintf(stderr, "Warning: could not bind to port 655. ");
- srand(time(NULL));
-
for(int i = 0; i < 100; i++) {
int port = 0x1000 + (rand() & 0x7fff);
if(try_bind(port)) {
} else if(argc < 2) {
if(tty) {
char buf[1024];
- fprintf(stdout, "Enter the Name you want your tinc node to have: ");
- fflush(stdout);
+ fprintf(stderr, "Enter the Name you want your tinc node to have: ");
if(!fgets(buf, sizeof buf, stdin)) {
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
return 1;
return 1;
}
- if(mkdir(confdir, 0755) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", CONFDIR, strerror(errno));
+ if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
+ fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
return 1;
}
- if(mkdir(confbase, 0755) && errno != EEXIST) {
+ if(mkdir(confbase, 0777) && errno != EEXIST) {
fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
return 1;
}
- if(mkdir(hosts_dir, 0755) && errno != EEXIST) {
+ if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
return 1;
}
char *filename;
xasprintf(&filename, "%s" SLASH "tinc-up", confbase);
if(access(filename, F_OK)) {
- FILE *f = fopen(filename, "w");
+ FILE *f = fopenmask(filename, "w", 0777);
if(!f) {
fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
return 1;
}
- mode_t mask = umask(0);
- umask(mask);
- fchmod(fileno(f), 0755 & ~mask);
- fprintf(f, "#!/bin/sh\n\necho 'Unconfigured tinc-up script, please edit!'\n\n#ifconfig $INTERFACE <your vpn IP address> netmask <netmask of whole VPN>\n");
+ fprintf(f, "#!/bin/sh\n\necho 'Unconfigured tinc-up script, please edit '$0'!'\n\n#ifconfig $INTERFACE <your vpn IP address> netmask <netmask of whole VPN>\n");
fclose(f);
}
#endif
return 0;
}
+#ifdef HAVE_MINGW
+ static struct WSAData wsa_state;
+
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ fprintf(stderr, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+ return false;
+ }
+#endif
+
+ srand(time(NULL));
crypto_init();
tty = isatty(0) && isatty(1);