2 invitation.c -- Create and accept invitations
3 Copyright (C) 2013-2017 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.
22 #include "control_common.h"
27 #include "invitation.h"
38 #include "ed25519/sha512.h"
40 int addressfamily = AF_UNSPEC;
42 static void scan_for_hostname(const char *filename, char **hostname, char **port) {
43 if(!filename || (*hostname && *port)) {
47 FILE *f = fopen(filename, "r");
53 while(fgets(line, sizeof(line), f)) {
59 p += strcspn(p, "\t =");
65 q = p + strspn(p, "\t ");
68 q += 1 + strspn(q + 1, "\t ");
72 p = q + strcspn(q, "\t ");
78 p += strspn(p, "\t ");
79 p[strcspn(p, "\t ")] = 0;
81 if(!*port && !strcasecmp(line, "Port")) {
83 } else if(!*hostname && !strcasecmp(line, "Address")) {
84 *hostname = xstrdup(q);
92 if(*hostname && *port) {
100 char *get_my_hostname() {
101 char *hostname = NULL;
103 char *hostport = NULL;
104 char *name = get_my_name(false);
105 char filename[PATH_MAX] = {0};
107 // Use first Address statement in own host config file
109 snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, name);
110 scan_for_hostname(filename, &hostname, &port);
111 scan_for_hostname(tinc_conf, &hostname, &port);
121 // If that doesn't work, guess externally visible hostname
122 fprintf(stderr, "Trying to discover externally visible hostname...\n");
123 struct addrinfo *ai = str2addrinfo("tinc-vpn.org", "80", SOCK_STREAM);
124 struct addrinfo *aip = ai;
125 static const char request[] = "GET http://tinc-vpn.org/host.cgi HTTP/1.0\r\n\r\n";
128 int s = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
131 if(connect(s, aip->ai_addr, aip->ai_addrlen)) {
138 send(s, request, sizeof(request) - 1, 0);
139 ssize_t len = recv(s, line, sizeof(line) - 1, MSG_WAITALL);
144 if(line[len - 1] == '\n') {
148 char *p = strrchr(line, '\n');
151 hostname = xstrdup(p + 1);
170 // Check that the hostname is reasonable
172 for(char *p = hostname; *p; p++) {
173 if(isalnum((uint8_t) *p) || *p == '-' || *p == '.' || *p == ':') {
177 // If not, forget it.
186 fprintf(stderr, "Could not determine the external address or hostname. Please set Address manually.\n");
195 fprintf(stderr, "Please enter your host's external address or hostname");
198 fprintf(stderr, " [%s]", hostname);
201 fprintf(stderr, ": ");
203 if(!fgets(line, sizeof(line), stdin)) {
204 fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
218 for(char *p = line; *p; p++) {
219 if(isalnum((uint8_t) *p) || *p == '-' || *p == '.') {
223 fprintf(stderr, "Invalid address or hostname.\n");
228 hostname = xstrdup(line);
233 FILE *f = fopen(filename, "a");
236 fprintf(f, "\nAddress = %s\n", hostname);
239 fprintf(stderr, "Could not append Address to %s: %s\n", filename, strerror(errno));
246 if(strchr(hostname, ':')) {
247 xasprintf(&hostport, "[%s]:%s", hostname, port);
249 xasprintf(&hostport, "%s:%s", hostname, port);
252 if(strchr(hostname, ':')) {
253 xasprintf(&hostport, "[%s]", hostname);
255 hostport = xstrdup(hostname);
264 static bool fcopy(FILE *out, const char *filename) {
265 FILE *in = fopen(filename, "r");
268 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
275 while((len = fread(buf, 1, sizeof(buf), in))) {
276 fwrite(buf, len, 1, out);
283 int cmd_invite(int argc, char *argv[]) {
285 fprintf(stderr, "Not enough arguments!\n");
289 // Check validity of the new node's name
290 if(!check_id(argv[1])) {
291 fprintf(stderr, "Invalid name for node.\n");
296 myname = get_my_name(true);
302 // Ensure no host configuration file with that name exists
303 char filename[PATH_MAX];
304 snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, argv[1]);
306 if(!access(filename, F_OK)) {
307 fprintf(stderr, "A host config file for %s already exists!\n", argv[1]);
311 // If a daemon is running, ensure no other nodes know about this name
312 if(connect_tincd(false)) {
314 sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
316 while(recvline(fd, line, sizeof(line))) {
320 if(sscanf(line, "%d %d %4095s", &code, &req, node) != 3) {
324 if(!strcmp(node, argv[1])) {
330 fprintf(stderr, "A node with name %s is already known!\n", argv[1]);
335 snprintf(filename, sizeof(filename), "%s" SLASH "invitations", confbase);
337 if(mkdir(filename, 0700) && errno != EEXIST) {
338 fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno));
342 // Count the number of valid invitations, clean up old ones
343 DIR *dir = opendir(filename);
346 fprintf(stderr, "Could not read directory %s: %s\n", filename, strerror(errno));
353 time_t deadline = time(NULL) - 604800; // 1 week in the past
355 while((ent = readdir(dir))) {
356 if(strlen(ent->d_name) != 24) {
360 char invname[PATH_MAX];
363 if((size_t)snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name) >= sizeof(invname)) {
364 fprintf(stderr, "Filename too long: %s" SLASH "%s\n", filename, ent->d_name);
368 if(!stat(invname, &st)) {
369 if(deadline < st.st_mtime) {
375 fprintf(stderr, "Could not stat %s: %s\n", invname, strerror(errno));
383 fprintf(stderr, "Error while reading directory %s: %s\n", filename, strerror(errno));
388 snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "ed25519_key.priv", confbase);
390 // Remove the key if there are no outstanding invitations.
395 // Create a new key if necessary.
396 FILE *f = fopen(filename, "r");
399 if(errno != ENOENT) {
400 fprintf(stderr, "Could not read %s: %s\n", filename, strerror(errno));
404 key = ecdsa_generate();
410 f = fopen(filename, "w");
413 fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno));
418 chmod(filename, 0600);
420 if(!ecdsa_write_pem_private_key(key, f)) {
421 fprintf(stderr, "Could not write ECDSA private key\n");
429 if(connect_tincd(true)) {
430 sendline(fd, "%d %d", CONTROL, REQ_RELOAD);
432 fprintf(stderr, "Could not signal the tinc daemon. Please restart or reload it manually.\n");
435 key = ecdsa_read_pem_private_key(f);
439 fprintf(stderr, "Could not read private key from %s\n", filename);
447 // Create a hash of the key.
449 char *fingerprint = ecdsa_get_base64_public_key(key);
450 sha512(fingerprint, strlen(fingerprint), hash);
451 b64encode_urlsafe(hash, hash, 18);
455 // Create a random cookie for this invitation.
457 randomize(cookie, 18);
459 // Create a filename that doesn't reveal the cookie itself
460 uint8_t buf[18 + strlen(fingerprint)];
462 memcpy(buf, cookie, 18);
463 memcpy(buf + 18, fingerprint, sizeof(buf) - 18);
464 sha512(buf, sizeof(buf), cookiehash);
465 b64encode_urlsafe(cookiehash, cookiehash, 18);
469 b64encode_urlsafe(cookie, cookie, 18);
471 // Create a file containing the details of the invitation.
472 snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookiehash);
473 int ifd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
476 fprintf(stderr, "Could not create invitation file %s: %s\n", filename, strerror(errno));
480 f = fdopen(ifd, "w");
486 // Get the local address
487 char *address = get_my_hostname();
489 // Fill in the details.
490 fprintf(f, "Name = %s\n", argv[1]);
492 if(check_netname(netname, true)) {
493 fprintf(f, "NetName = %s\n", netname);
496 fprintf(f, "ConnectTo = %s\n", myname);
498 // Copy Broadcast and Mode
499 FILE *tc = fopen(tinc_conf, "r");
504 while(fgets(buf, sizeof(buf), tc)) {
505 if((!strncasecmp(buf, "Mode", 4) && strchr(" \t=", buf[4]))
506 || (!strncasecmp(buf, "Broadcast", 9) && strchr(" \t=", buf[9]))) {
509 // Make sure there is a newline character.
510 if(!strchr(buf, '\n')) {
519 fprintf(f, "#---------------------------------------------------------------#\n");
520 fprintf(f, "Name = %s\n", myname);
522 char filename2[PATH_MAX];
523 snprintf(filename2, sizeof(filename2), "%s" SLASH "hosts" SLASH "%s", confbase, myname);
527 // Create an URL from the local address, key hash and cookie
529 xasprintf(&url, "%s/%s%s", address, hash, cookie);
531 // Call the inviation-created script
533 environment_init(&env);
534 environment_add(&env, "NODE=%s", argv[1]);
535 environment_add(&env, "INVITATION_FILE=%s", filename);
536 environment_add(&env, "INVITATION_URL=%s", url);
537 execute_script("invitation-created", &env);
538 environment_exit(&env);
548 static char cookie[18];
549 static sptps_t sptps;
551 static size_t datalen;
552 static bool success = false;
554 static char cookie[18], hash[18];
556 static char *get_line(const char **data) {
557 if(!data || !*data) {
566 static char line[1024];
567 const char *end = strchr(*data, '\n');
568 size_t len = end ? (size_t)(end - *data) : strlen(*data);
570 if(len >= sizeof(line)) {
571 fprintf(stderr, "Maximum line length exceeded!\n");
575 if(len && !isprint((uint8_t) **data)) {
579 memcpy(line, *data, len);
591 static char *get_value(const char *data, const char *var) {
592 char *line = get_line(&data);
598 char *sep = line + strcspn(line, " \t=");
599 char *val = sep + strspn(sep, " \t");
602 val += 1 + strspn(val + 1, " \t");
607 if(strcasecmp(line, var)) {
614 static char *grep(const char *data, const char *var) {
615 static char value[1024];
617 const char *p = data;
618 size_t varlen = strlen(var);
620 // Skip all lines not starting with var
621 while(strncasecmp(p, var, varlen) || !strchr(" \t=", p[varlen])) {
636 p += strspn(p, " \t");
639 p += 1 + strspn(p + 1, " \t");
642 const char *e = strchr(p, '\n');
648 if((size_t)(e - p) >= sizeof(value)) {
649 fprintf(stderr, "Maximum line length exceeded!\n");
653 memcpy(value, p, e - p);
658 static bool finalize_join(void) {
659 const char *temp_name = get_value(data, "Name");
662 fprintf(stderr, "No Name found in invitation!\n");
666 size_t len = strlen(temp_name);
668 memcpy(name, temp_name, len);
671 if(!check_id(name)) {
672 fprintf(stderr, "Invalid Name found in invitation!\n");
677 netname = grep(data, "NetName");
679 if(netname && !check_netname(netname, true)) {
680 fprintf(stderr, "Unsafe NetName found in invitation!\n");
685 bool ask_netname = false;
686 char temp_netname[32];
700 xasprintf(&tinc_conf, "%s" SLASH "tinc.conf", confbase);
701 xasprintf(&hosts_dir, "%s" SLASH "hosts", confbase);
703 if(!access(tinc_conf, F_OK)) {
704 fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
710 // Generate a random netname, ask for a better one later.
712 snprintf(temp_netname, sizeof(temp_netname), "join_%x", rand());
713 netname = temp_netname;
717 if(mkdir(confbase, 0777) && errno != EEXIST) {
718 fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
722 if(mkdir(hosts_dir, 0777) && errno != EEXIST) {
723 fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
727 FILE *f = fopen(tinc_conf, "w");
730 fprintf(stderr, "Could not create file %s: %s\n", tinc_conf, strerror(errno));
734 fprintf(f, "Name = %s\n", name);
736 char filename[PATH_MAX];
737 snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, name);
738 FILE *fh = fopen(filename, "w");
741 fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
746 snprintf(filename, sizeof(filename), "%s" SLASH "invitation-data", confbase);
747 FILE *finv = fopen(filename, "w");
749 if(!finv || fwrite(data, datalen, 1, finv) != 1) {
750 fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
759 snprintf(filename, sizeof(filename), "%s" SLASH "tinc-up.invitation", confbase);
760 FILE *fup = fopen(filename, "w");
763 fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
769 ifconfig_header(fup);
771 // Filter first chunk on approved keywords, split between tinc.conf and hosts/Name
772 // Generate a tinc-up script from Ifconfig and Route keywords.
773 // Other chunks go unfiltered to their respective host config files
774 const char *p = data;
777 while((l = get_line(&p))) {
783 // Split line into variable and value
784 size_t len = strcspn(l, "\t =");
786 value += strspn(value, "\t ");
790 value += strspn(value, "\t ");
795 // Ignore lines with empty variable names
801 if(!strcasecmp(l, "Name")) {
802 if(strcmp(value, name)) {
807 } else if(!strcasecmp(l, "NetName")) {
811 // Check the list of known variables
815 for(i = 0; variables[i].name; i++) {
816 if(strcasecmp(l, variables[i].name)) {
824 // Handle Ifconfig and Route statements
826 if(!strcasecmp(l, "Ifconfig")) {
827 if(!strcasecmp(value, "dhcp")) {
829 } else if(!strcasecmp(value, "dhcp6")) {
831 } else if(!strcasecmp(value, "slaac")) {
834 ifconfig_address(fup, value);
838 } else if(!strcasecmp(l, "Route")) {
839 ifconfig_route(fup, value);
844 // Ignore unknown and unsafe variables
846 fprintf(stderr, "Ignoring unknown variable '%s' in invitation.\n", l);
848 } else if(!(variables[i].type & VAR_SAFE)) {
850 fprintf(stderr, "Warning: unsafe variable '%s' in invitation.\n", l);
852 fprintf(stderr, "Ignoring unsafe variable '%s' in invitation.\n", l);
857 // Copy the safe variable to the right config file
858 fprintf((variables[i].type & VAR_HOST) ? fh : f, "%s = %s\n", l, value);
862 bool valid_tinc_up = ifconfig_footer(fup);
865 while(l && !strcasecmp(l, "Name")) {
866 if(!check_id(value)) {
867 fprintf(stderr, "Invalid Name found in invitation.\n");
871 if(!strcmp(value, name)) {
872 fprintf(stderr, "Secondary chunk would overwrite our own host config file.\n");
876 snprintf(filename, sizeof(filename), "%s" SLASH "%s", hosts_dir, value);
877 f = fopen(filename, "w");
880 fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
884 while((l = get_line(&p))) {
885 if(!strcmp(l, "#---------------------------------------------------------------#")) {
889 size_t len = strcspn(l, "\t =");
891 if(len == 4 && !strncasecmp(l, "Name", 4)) {
893 value += strspn(value, "\t ");
897 value += strspn(value, "\t ");
911 // Generate our key and send a copy to the server
912 ecdsa_t *key = ecdsa_generate();
918 char *b64key = ecdsa_get_base64_public_key(key);
924 snprintf(filename, sizeof(filename), "%s" SLASH "ed25519_key.priv", confbase);
925 f = fopenmask(filename, "w", 0600);
931 if(!ecdsa_write_pem_private_key(key, f)) {
932 fprintf(stderr, "Error writing private key!\n");
940 fprintf(fh, "Ed25519PublicKey = %s\n", b64key);
942 sptps_send_record(&sptps, 1, b64key, strlen(b64key));
946 #ifndef DISABLE_LEGACY
947 rsa_t *rsa = rsa_generate(2048, 0x1001);
948 snprintf(filename, sizeof(filename), "%s" SLASH "rsa_key.priv", confbase);
949 f = fopenmask(filename, "w", 0600);
951 if(!f || !rsa_write_pem_private_key(rsa, f)) {
952 fprintf(stderr, "Could not write private RSA key\n");
953 } else if(!rsa_write_pem_public_key(rsa, fh)) {
954 fprintf(stderr, "Could not write public RSA key\n");
968 if(ask_netname && tty) {
969 fprintf(stderr, "Enter a new netname: ");
971 if(!fgets(line, sizeof(line), stdin)) {
972 fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
976 if(!*line || *line == '\n') {
980 line[strlen(line) - 1] = 0;
982 char newbase[PATH_MAX];
984 if((size_t)snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line) >= sizeof(newbase)) {
985 fprintf(stderr, "Filename too long: " CONFDIR SLASH "tinc" SLASH "%s\n", line);
989 if(rename(confbase, newbase)) {
990 fprintf(stderr, "Error trying to rename %s to %s: %s\n", confbase, newbase, strerror(errno));
998 char filename2[PATH_MAX];
999 snprintf(filename, sizeof(filename), "%s" SLASH "tinc-up.invitation", confbase);
1002 snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up.bat", confbase);
1004 snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up", confbase);
1009 FILE *fup = fopen(filename, "r");
1012 fprintf(stderr, "\nPlease review the following tinc-up script:\n\n");
1016 while(fgets(buf, sizeof(buf), fup)) {
1025 fprintf(stderr, "\nDo you want to use this script [y]es/[n]o/[e]dit? ");
1026 response = tolower(getchar());
1027 } while(!strchr("yne", response));
1029 fprintf(stderr, "\n");
1031 if(response == 'e') {
1034 const char *editor = getenv("VISUAL");
1037 editor = getenv("EDITOR");
1044 xasprintf(&command, "\"%s\" \"%s\"", editor, filename);
1046 xasprintf(&command, "edit \"%s\"", filename);
1049 if(system(command)) {
1058 if(response == 'y') {
1059 rename(filename, filename2);
1060 chmod(filename2, 0755);
1061 fprintf(stderr, "tinc-up enabled.\n");
1063 fprintf(stderr, "tinc-up has been left disabled.\n");
1068 rename(filename, filename2);
1069 chmod(filename2, 0755);
1070 fprintf(stderr, "tinc-up enabled.\n");
1072 fprintf(stderr, "A tinc-up script was generated, but has been left disabled.\n");
1076 // A placeholder was generated.
1077 rename(filename, filename2);
1078 chmod(filename2, 0755);
1081 fprintf(stderr, "Configuration stored in: %s\n", confbase);
1087 static bool invitation_send(void *handle, uint8_t type, const void *vdata, size_t len) {
1090 const uint8_t *data = vdata;
1093 ssize_t result = send(sock, data, len, 0);
1095 if(result == -1 && sockwouldblock(sockerrno)) {
1097 } else if(result <= 0) {
1108 static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint16_t len) {
1112 case SPTPS_HANDSHAKE:
1113 return sptps_send_record(&sptps, 0, cookie, sizeof(cookie));
1116 data = xrealloc(data, datalen + len + 1);
1117 memcpy(data + datalen, msg, len);
1123 return finalize_join();
1126 fprintf(stderr, "Invitation successfully accepted.\n");
1127 shutdown(sock, SHUT_RDWR);
1138 int cmd_join(int argc, char *argv[]) {
1144 fprintf(stderr, "Too many arguments!\n");
1148 // Make sure confbase exists and is accessible.
1149 if(!confbase_given && mkdir(confdir, 0755) && errno != EEXIST) {
1150 fprintf(stderr, "Could not create directory %s: %s\n", confdir, strerror(errno));
1154 if(mkdir(confbase, 0777) && errno != EEXIST) {
1155 fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
1159 if(access(confbase, R_OK | W_OK | X_OK)) {
1160 fprintf(stderr, "No permission to write in directory %s: %s\n", confbase, strerror(errno));
1164 // If a netname or explicit configuration directory is specified, check for an existing tinc.conf.
1165 if((netname || confbasegiven) && !access(tinc_conf, F_OK)) {
1166 fprintf(stderr, "Configuration file %s already exists!\n", tinc_conf);
1170 // Either read the invitation from the command line or from stdin.
1174 invitation = argv[1];
1177 fprintf(stderr, "Enter invitation URL: ");
1182 if(!fgets(line, sizeof(line), stdin)) {
1183 fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
1190 // Parse the invitation URL.
1193 char *slash = strchr(invitation, '/');
1201 if(strlen(slash) != 48) {
1205 char *address = invitation;
1208 if(*address == '[') {
1210 char *bracket = strchr(address, ']');
1218 if(bracket[1] == ':') {
1222 port = strchr(address, ':');
1229 if(!port || !*port) {
1233 if(!b64decode(slash, hash, 24) || !b64decode(slash + 24, cookie, 24)) {
1237 // Generate a throw-away key for the invitation.
1238 ecdsa_t *key = ecdsa_generate();
1244 char *b64key = ecdsa_get_base64_public_key(key);
1246 // Connect to the tinc daemon mentioned in the URL.
1247 struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
1254 struct addrinfo *aip = NULL;
1269 sock = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
1272 fprintf(stderr, "Could not open socket: %s\n", strerror(errno));
1276 if(connect(sock, aip->ai_addr, aip->ai_addrlen)) {
1277 char *addrstr, *portstr;
1278 sockaddr2str((sockaddr_t *)aip->ai_addr, &addrstr, &portstr);
1279 fprintf(stderr, "Could not connect to %s port %s: %s\n", addrstr, portstr, strerror(errno));
1286 fprintf(stderr, "Connected to %s port %s...\n", address, port);
1288 // Tell him we have an invitation, and give him our throw-away key.
1289 ssize_t len = snprintf(line, sizeof(line), "0 ?%s %d.%d\n", b64key, PROT_MAJOR, PROT_MINOR);
1291 if(len <= 0 || (size_t)len >= sizeof(line)) {
1295 if(!sendline(sock, "0 ?%s %d.%d", b64key, PROT_MAJOR, 1)) {
1296 fprintf(stderr, "Error sending request to %s port %s: %s\n", address, port, strerror(errno));
1301 char hisname[4096] = "";
1302 int code, hismajor, hisminor = 0;
1304 if(!recvline(sock, line, sizeof(line)) || sscanf(line, "%d %4095s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(sock, line, sizeof(line)) || !rstrip(line) || sscanf(line, "%d ", &code) != 1 || code != ACK || strlen(line) < 3) {
1305 fprintf(stderr, "Cannot read greeting from peer\n");
1317 // Check if the hash of the key he gave us matches the hash in the URL.
1318 char *fingerprint = line + 2;
1321 if(sha512(fingerprint, strlen(fingerprint), hishash)) {
1322 fprintf(stderr, "Could not create digest\n%s\n", line + 2);
1326 if(memcmp(hishash, hash, 18)) {
1327 fprintf(stderr, "Peer has an invalid key!\n%s\n", line + 2);
1332 ecdsa_t *hiskey = ecdsa_set_base64_public_key(fingerprint);
1338 // Start an SPTPS session
1339 if(!sptps_start(&sptps, NULL, true, false, key, hiskey, "tinc invitation", 15, invitation_send, invitation_receive)) {
1343 // Feed rest of input buffer to SPTPS
1344 if(!sptps_receive_data(&sptps, buffer, blen)) {
1348 while((len = recv(sock, line, sizeof(line), 0))) {
1350 if(sockwouldblock(sockerrno)) {
1356 // If socket has been shut down, recv() on Windows returns -1 and sets sockerrno
1357 // to WSAESHUTDOWN, while on UNIX-like operating systems recv() returns 0, so we
1358 // have to do an explicit check here.
1359 if(sockshutdown(sockerrno)) {
1364 fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, sockstrerror(sockerrno));
1371 size_t done = sptps_receive_data(&sptps, p, len);
1377 len -= (ssize_t) done;
1388 fprintf(stderr, "Connection closed by peer, invitation cancelled.\n");
1395 fprintf(stderr, "Invalid invitation URL.\n");