Wipe (some) secrets from memory after use
[tinc] / src / invitation.c
index d1e1e61..5db1e98 100644 (file)
@@ -34,6 +34,8 @@
 #include "tincctl.h"
 #include "utils.h"
 #include "xalloc.h"
+#include "random.h"
+#include "pidfile.h"
 
 #include "ed25519/sha512.h"
 
@@ -79,8 +81,10 @@ static void scan_for_hostname(const char *filename, char **hostname, char **port
                p[strcspn(p, "\t ")] = 0;
 
                if(!*port && !strcasecmp(line, "Port")) {
+                       free(*port);
                        *port = xstrdup(q);
                } else if(!*hostname && !strcasecmp(line, "Address")) {
+                       free(*hostname);
                        *hostname = xstrdup(q);
 
                        if(*p) {
@@ -97,7 +101,7 @@ static void scan_for_hostname(const char *filename, char **hostname, char **port
        fclose(f);
 }
 
-static char *get_my_hostname(void) {
+static bool get_my_hostname(char **out_address, char **out_port) {
        char *hostname = NULL;
        char *port = NULL;
        char *hostport = NULL;
@@ -114,6 +118,19 @@ static char *get_my_hostname(void) {
        free(name);
        name = NULL;
 
+       if(!port || (is_decimal(port) && atoi(port) == 0)) {
+               pidfile_t *pidfile = read_pidfile();
+
+               if(pidfile) {
+                       free(port);
+                       port = xstrdup(pidfile->port);
+                       free(pidfile);
+               } else {
+                       fprintf(stderr, "tincd is using a dynamic port and is not running. Please start tincd or set the Port option to a non-zero value.\n");
+                       goto exit;
+               }
+       }
+
        if(hostname) {
                goto done;
        }
@@ -185,7 +202,7 @@ static char *get_my_hostname(void) {
                if(!hostname) {
                        fprintf(stderr, "Could not determine the external address or hostname. Please set Address manually.\n");
                        free(port);
-                       return NULL;
+                       return false;
                }
 
                goto save;
@@ -204,7 +221,7 @@ again:
                fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
                free(hostname);
                free(port);
-               return NULL;
+               return false;
        }
 
        if(!rstrip(line)) {
@@ -256,12 +273,25 @@ done:
                }
        }
 
+exit:
        free(hostname);
+
+       if(hostport && port) {
+               *out_address = hostport;
+               *out_port = port;
+               return true;
+       }
+
+       free(hostport);
        free(port);
-       return hostport;
+       return false;
 }
 
-static bool fcopy(FILE *out, const char *filename) {
+// Copy host configuration file, replacing Port with the value passed here. Host
+// configs may contain this clause: `Port = 0`, which means 'ask the operating
+// system to allocate any available port'. This obviously won't do for invitation
+// files, so replace it with an actual port we've obtained previously.
+static bool copy_config_replacing_port(FILE *out, const char *filename, const char *port) {
        FILE *in = fopen(filename, "r");
 
        if(!in) {
@@ -269,17 +299,34 @@ static bool fcopy(FILE *out, const char *filename) {
                return false;
        }
 
-       char buf[1024];
-       size_t len;
+       char line[1024];
 
-       while((len = fread(buf, 1, sizeof(buf), in))) {
-               fwrite(buf, len, 1, out);
+       while(fgets(line, sizeof(line), in)) {
+               const char *var_beg = line + strspn(line, "\t ");
+               const char *var_end = var_beg + strcspn(var_beg, "\t ");
+
+               // Check the name of the variable we've read. If it's Port, replace it with
+               // a port we'll use in invitation URL. Otherwise, just copy the line.
+               if(var_end > var_beg && !strncasecmp(var_beg, "Port", var_end - var_beg)) {
+                       fprintf(out, "Port = %s\n", port);
+               } else {
+                       fprintf(out, "%s", line);
+               }
        }
 
+       memzero(line, sizeof(line));
        fclose(in);
        return true;
 }
 
+static bool append_host_config(FILE *f, const char *nodename, const char *port) {
+       char path[PATH_MAX];
+       snprintf(path, sizeof(path), "%s" SLASH "hosts" SLASH "%s", confbase, nodename);
+       bool success = copy_config_replacing_port(f, path, port);
+       fclose(f);
+       return success;
+}
+
 int cmd_invite(int argc, char *argv[]) {
        if(argc < 2) {
                fprintf(stderr, "Not enough arguments!\n");
@@ -475,6 +522,7 @@ int cmd_invite(int argc, char *argv[]) {
        int ifd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
 
        if(!ifd) {
+               memzero(cookie, sizeof(cookie));
                fprintf(stderr, "Could not create invitation file %s: %s\n", filename, strerror(errno));
                return 1;
        }
@@ -486,7 +534,20 @@ int cmd_invite(int argc, char *argv[]) {
        }
 
        // Get the local address
-       char *address = get_my_hostname();
+       char *address = NULL;
+       char *port = NULL;
+
+       if(!get_my_hostname(&address, &port)) {
+               memzero(cookie, sizeof(cookie));
+               return 1;
+       }
+
+       // Create an URL from the local address, key hash and cookie
+       char *url;
+       xasprintf(&url, "%s/%s%s", address, hash, cookie);
+
+       memzero(cookie, sizeof(cookie));
+       free(address);
 
        // Fill in the details.
        fprintf(f, "Name = %s\n", argv[1]);
@@ -521,14 +582,14 @@ int cmd_invite(int argc, char *argv[]) {
        fprintf(f, "#---------------------------------------------------------------#\n");
        fprintf(f, "Name = %s\n", myname);
 
-       char filename2[PATH_MAX];
-       snprintf(filename2, sizeof(filename2), "%s" SLASH "hosts" SLASH "%s", confbase, myname);
-       fcopy(f, filename2);
-       fclose(f);
+       bool appended = append_host_config(f, myname, port);
+       free(port);
 
-       // Create an URL from the local address, key hash and cookie
-       char *url;
-       xasprintf(&url, "%s/%s%s", address, hash, cookie);
+       if(!appended) {
+               fprintf(stderr, "Could not append my config to invitation file: %s.\n", strerror(errno));
+               free_string(url);
+               return 1;
+       }
 
        // Call the inviation-created script
        environment_t env;
@@ -540,8 +601,7 @@ int cmd_invite(int argc, char *argv[]) {
        environment_exit(&env);
 
        puts(url);
-       free(url);
-       free(address);
+       free_string(url);
 
        return 0;
 }
@@ -670,11 +730,15 @@ static bool finalize_join(void) {
        }
 
        if(!netname) {
-               netname = xstrdup(grep(data, "NetName"));
+               const char *net = grep(data, "NetName");
 
-               if(netname && !check_netname(netname, true)) {
-                       fprintf(stderr, "Unsafe NetName found in invitation!\n");
-                       return false;
+               if(net) {
+                       netname = xstrdup(net);
+
+                       if(!check_netname(netname, true)) {
+                               fprintf(stderr, "Unsafe NetName found in invitation!\n");
+                               return false;
+                       }
                }
        }
 
@@ -913,9 +977,9 @@ make_names:
                return false;
        }
 
-       char *b64key = ecdsa_get_base64_public_key(key);
+       char *b64_pubkey = ecdsa_get_base64_public_key(key);
 
-       if(!b64key) {
+       if(!b64_pubkey) {
                return false;
        }
 
@@ -935,10 +999,10 @@ make_names:
 
        fclose(f);
 
-       fprintf(fh, "Ed25519PublicKey = %s\n", b64key);
+       fprintf(fh, "Ed25519PublicKey = %s\n", b64_pubkey);
 
-       sptps_send_record(&sptps, 1, b64key, strlen(b64key));
-       free(b64key);
+       sptps_send_record(&sptps, 1, b64_pubkey, strlen(b64_pubkey));
+       free(b64_pubkey);
        ecdsa_free(key);
 
 #ifndef DISABLE_LEGACY
@@ -996,7 +1060,7 @@ ask_netname:
        char filename2[PATH_MAX];
        snprintf(filename, sizeof(filename), "%s" SLASH "tinc-up.invitation", confbase);
 
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
        snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up.bat", confbase);
 #else
        snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up", confbase);
@@ -1028,7 +1092,7 @@ ask_netname:
 
                                if(response == 'e') {
                                        char *command;
-#ifndef HAVE_MINGW
+#ifndef HAVE_WINDOWS
                                        const char *editor = getenv("VISUAL");
 
                                        if(!editor) {
@@ -1240,13 +1304,13 @@ int cmd_join(int argc, char *argv[]) {
                return 1;
        }
 
-       char *b64key = ecdsa_get_base64_public_key(key);
+       char *b64_pubkey = ecdsa_get_base64_public_key(key);
 
        // Connect to the tinc daemon mentioned in the URL.
        struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
 
        if(!ai) {
-               free(b64key);
+               free(b64_pubkey);
                ecdsa_free(key);
                return 1;
        }
@@ -1261,7 +1325,7 @@ next:
 
                if(!aip) {
                        freeaddrinfo(ai);
-                       free(b64key);
+                       free(b64_pubkey);
                        ecdsa_free(key);
                        return 1;
                }
@@ -1287,13 +1351,13 @@ next:
        fprintf(stderr, "Connected to %s port %s...\n", address, port);
 
        // Tell him we have an invitation, and give him our throw-away key.
-       ssize_t len = snprintf(line, sizeof(line), "0 ?%s %d.%d\n", b64key, PROT_MAJOR, PROT_MINOR);
+       ssize_t len = snprintf(line, sizeof(line), "0 ?%s %d.%d\n", b64_pubkey, PROT_MAJOR, PROT_MINOR);
 
        if(len <= 0 || (size_t)len >= sizeof(line)) {
                abort();
        }
 
-       if(!sendline(sock, "0 ?%s %d.%d", b64key, PROT_MAJOR, 1)) {
+       if(!sendline(sock, "0 ?%s %d.%d", b64_pubkey, PROT_MAJOR, 1)) {
                fprintf(stderr, "Error sending request to %s port %s: %s\n", address, port, strerror(errno));
                closesocket(sock);
                goto next;
@@ -1312,8 +1376,8 @@ next:
        ai = NULL;
        aip = NULL;
 
-       free(b64key);
-       b64key = NULL;
+       free(b64_pubkey);
+       b64_pubkey = NULL;
 
        // Check if the hash of the key he gave us matches the hash in the URL.
        char *fingerprint = line + 2;
@@ -1358,7 +1422,7 @@ next:
                                continue;
                        }
 
-#if HAVE_MINGW
+#if HAVE_WINDOWS
 
                        // If socket has been shut down, recv() on Windows returns -1 and sets sockerrno
                        // to WSAESHUTDOWN, while on UNIX-like operating systems recv() returns 0, so we