X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Finvitation.c;h=cff9e7270c4606447d83c02af3423ce5068d700c;hb=0289162552cd85375605044c696e2a3294e7aa9a;hp=f2f4d76c148195dd7e6165bc387acea20d4cc1a6;hpb=6debc6c79ba385d35f646e0958f84ace5b8f4b4d;p=tinc diff --git a/src/invitation.c b/src/invitation.c index f2f4d76c..cff9e727 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -1,6 +1,6 @@ /* invitation.c -- Create and accept invitations - Copyright (C) 2013-2017 Guus Sliepen + Copyright (C) 2013-2022 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -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); } -char *get_my_hostname() { +static bool get_my_hostname(char **out_address, char **out_port) { char *hostname = NULL; char *port = NULL; char *hostport = NULL; @@ -114,6 +118,19 @@ char *get_my_hostname() { 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 @@ char *get_my_hostname() { 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,33 @@ 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); + } } 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"); @@ -457,11 +503,13 @@ int cmd_invite(int argc, char *argv[]) { randomize(cookie, 18); // Create a filename that doesn't reveal the cookie itself - uint8_t buf[18 + strlen(fingerprint)]; + const size_t buflen = 18 + strlen(fingerprint); + uint8_t *buf = alloca(buflen); + char cookiehash[64]; memcpy(buf, cookie, 18); - memcpy(buf + 18, fingerprint, sizeof(buf) - 18); - sha512(buf, sizeof(buf), cookiehash); + memcpy(buf + 18, fingerprint, buflen - 18); + sha512(buf, buflen, cookiehash); b64encode_tinc_urlsafe(cookiehash, cookiehash, 18); free(fingerprint); @@ -484,7 +532,12 @@ 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)) { + return 1; + } // Fill in the details. fprintf(f, "Name = %s\n", argv[1]); @@ -519,10 +572,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); + + if(!appended) { + fprintf(stderr, "Could not append my config to invitation file: %s.\n", strerror(errno)); + free(address); + return 1; + } // Create an URL from the local address, key hash and cookie char *url; @@ -545,15 +602,13 @@ int cmd_invite(int argc, char *argv[]) { } static int sock; -static char cookie[18]; +static char cookie[18], hash[18]; static sptps_t sptps; static char *data; static size_t datalen; static bool success = false; -static char cookie[18], hash[18]; - -static char *get_line(const char **data) { +static char *get_line(char *line, size_t linelen, const char **data) { if(!data || !*data) { return NULL; } @@ -563,11 +618,10 @@ static char *get_line(const char **data) { return NULL; } - static char line[1024]; const char *end = strchr(*data, '\n'); size_t len = end ? (size_t)(end - *data) : strlen(*data); - if(len >= sizeof(line)) { + if(len >= linelen) { fprintf(stderr, "Maximum line length exceeded!\n"); return NULL; } @@ -589,7 +643,9 @@ static char *get_line(const char **data) { } static char *get_value(const char *data, const char *var) { - char *line = get_line(&data); + static char buf[1024]; + + char *line = get_line(buf, sizeof(buf), &data); if(!line) { return NULL; @@ -656,29 +712,28 @@ static char *grep(const char *data, const char *var) { } static bool finalize_join(void) { - const char *temp_name = get_value(data, "Name"); + const char *name = get_value(data, "Name"); - if(!temp_name) { + if(!name) { fprintf(stderr, "No Name found in invitation!\n"); return false; } - size_t len = strlen(temp_name); - char name[len + 1]; - memcpy(name, temp_name, len); - name[len] = 0; - if(!check_id(name)) { fprintf(stderr, "Invalid Name found in invitation!\n"); return false; } 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; + } } } @@ -774,7 +829,9 @@ make_names: const char *p = data; char *l, *value; - while((l = get_line(&p))) { + static char line[1024]; + + while((l = get_line(line, sizeof(line), &p))) { // Ignore comments if(*l == '#') { continue; @@ -881,7 +938,7 @@ make_names: return false; } - while((l = get_line(&p))) { + while((l = get_line(line, sizeof(line), &p))) { if(!strcmp(l, "#---------------------------------------------------------------#")) { continue; } @@ -998,7 +1055,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); @@ -1030,7 +1087,7 @@ ask_netname: if(response == 'e') { char *command; -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS const char *editor = getenv("VISUAL"); if(!editor) { @@ -1087,7 +1144,7 @@ ask_netname: static bool invitation_send(void *handle, uint8_t type, const void *vdata, size_t len) { (void)handle; (void)type; - const uint8_t *data = vdata; + const char *data = vdata; while(len) { ssize_t result = send(sock, data, len, 0); @@ -1227,7 +1284,8 @@ int cmd_join(int argc, char *argv[]) { } if(!port || !*port) { - port = "655"; + static char default_port[] = "655"; + port = default_port; } if(!b64decode_tinc(slash, hash, 24) || !b64decode_tinc(slash + 24, cookie, 24)) { @@ -1359,7 +1417,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