projects
/
tinc
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Replace MinGW with Windows to avoid ambiguities
[tinc]
/
src
/
invitation.c
diff --git
a/src/invitation.c
b/src/invitation.c
index
30d100d
..
22fd614
100644
(file)
--- a/
src/invitation.c
+++ b/
src/invitation.c
@@
-1,6
+1,6
@@
/*
invitation.c -- Create and accept invitations
/*
invitation.c -- Create and accept invitations
- Copyright (C) 2013-20
17
Guus Sliepen <guus@tinc-vpn.org>
+ Copyright (C) 2013-20
22
Guus Sliepen <guus@tinc-vpn.org>
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
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
@@
-97,7
+97,7
@@
static void scan_for_hostname(const char *filename, char **hostname, char **port
fclose(f);
}
fclose(f);
}
-
char *get_my_hostname(
) {
+
static char *get_my_hostname(void
) {
char *hostname = NULL;
char *port = NULL;
char *hostport = NULL;
char *hostname = NULL;
char *port = NULL;
char *hostport = NULL;
@@
-111,6
+111,9
@@
char *get_my_hostname() {
scan_for_hostname(tinc_conf, &hostname, &port);
}
scan_for_hostname(tinc_conf, &hostname, &port);
}
+ free(name);
+ name = NULL;
+
if(hostname) {
goto done;
}
if(hostname) {
goto done;
}
@@
-133,7
+136,7
@@
char *get_my_hostname() {
if(s >= 0) {
send(s, request, sizeof(request) - 1, 0);
if(s >= 0) {
send(s, request, sizeof(request) - 1, 0);
-
in
t len = recv(s, line, sizeof(line) - 1, MSG_WAITALL);
+
ssize_
t len = recv(s, line, sizeof(line) - 1, MSG_WAITALL);
if(len > 0) {
line[len] = 0;
if(len > 0) {
line[len] = 0;
@@
-167,7
+170,7
@@
char *get_my_hostname() {
// Check that the hostname is reasonable
if(hostname) {
for(char *p = hostname; *p; p++) {
// Check that the hostname is reasonable
if(hostname) {
for(char *p = hostname; *p; p++) {
- if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':') {
+ if(isalnum(
(uint8_t)
*p) || *p == '-' || *p == '.' || *p == ':') {
continue;
}
continue;
}
@@
-213,7
+216,7
@@
again:
}
for(char *p = line; *p; p++) {
}
for(char *p = line; *p; p++) {
- if(isalnum(*p) || *p == '-' || *p == '.') {
+ if(isalnum(
(uint8_t)
*p) || *p == '-' || *p == '.') {
continue;
}
continue;
}
@@
-289,6
+292,7
@@
int cmd_invite(int argc, char *argv[]) {
return 1;
}
return 1;
}
+ free(myname);
myname = get_my_name(true);
if(!myname) {
myname = get_my_name(true);
if(!myname) {
@@
-407,6
+411,7
@@
int cmd_invite(int argc, char *argv[]) {
if(!f) {
fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno));
if(!f) {
fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno));
+ ecdsa_free(key);
return 1;
}
return 1;
}
@@
-415,6
+420,7
@@
int cmd_invite(int argc, char *argv[]) {
if(!ecdsa_write_pem_private_key(key, f)) {
fprintf(stderr, "Could not write ECDSA private key\n");
fclose(f);
if(!ecdsa_write_pem_private_key(key, f)) {
fprintf(stderr, "Could not write ECDSA private key\n");
fclose(f);
+ ecdsa_free(key);
return 1;
}
return 1;
}
@@
-442,21
+448,27
@@
int cmd_invite(int argc, char *argv[]) {
char hash[64];
char *fingerprint = ecdsa_get_base64_public_key(key);
sha512(fingerprint, strlen(fingerprint), hash);
char hash[64];
char *fingerprint = ecdsa_get_base64_public_key(key);
sha512(fingerprint, strlen(fingerprint), hash);
- b64encode_urlsafe(hash, hash, 18);
+ b64encode_tinc_urlsafe(hash, hash, 18);
+
+ ecdsa_free(key);
// Create a random cookie for this invitation.
char cookie[25];
randomize(cookie, 18);
// Create a filename that doesn't reveal the cookie itself
// Create a random cookie for this invitation.
char cookie[25];
randomize(cookie, 18);
// Create a filename that doesn't reveal the cookie itself
- char buf[18 + strlen(fingerprint)];
+ const size_t buflen = 18 + strlen(fingerprint);
+ uint8_t *buf = alloca(buflen);
+
char cookiehash[64];
memcpy(buf, cookie, 18);
char cookiehash[64];
memcpy(buf, cookie, 18);
- memcpy(buf + 18, fingerprint,
sizeof(buf)
- 18);
- sha512(buf,
sizeof(buf)
, cookiehash);
- b64encode_urlsafe(cookiehash, cookiehash, 18);
+ memcpy(buf + 18, fingerprint,
buflen
- 18);
+ sha512(buf,
buflen
, cookiehash);
+ b64encode_
tinc_
urlsafe(cookiehash, cookiehash, 18);
- b64encode_urlsafe(cookie, cookie, 18);
+ free(fingerprint);
+
+ b64encode_tinc_urlsafe(cookie, cookie, 18);
// Create a file containing the details of the invitation.
snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookiehash);
// Create a file containing the details of the invitation.
snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookiehash);
@@
-535,15
+547,13
@@
int cmd_invite(int argc, char *argv[]) {
}
static int sock;
}
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 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;
}
if(!data || !*data) {
return NULL;
}
@@
-553,16
+563,15
@@
static char *get_line(const char **data) {
return NULL;
}
return NULL;
}
- static char line[1024];
const char *end = strchr(*data, '\n');
size_t len = end ? (size_t)(end - *data) : strlen(*data);
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;
}
fprintf(stderr, "Maximum line length exceeded!\n");
return NULL;
}
- if(len && !isprint(**data)) {
+ if(len && !isprint(
(uint8_t)
**data)) {
abort();
}
abort();
}
@@
-579,7
+588,9
@@
static char *get_line(const char **data) {
}
static char *get_value(const char *data, const char *var) {
}
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;
if(!line) {
return NULL;
@@
-605,7
+616,7
@@
static char *grep(const char *data, const char *var) {
static char value[1024];
const char *p = data;
static char value[1024];
const char *p = data;
-
in
t varlen = strlen(var);
+
size_
t varlen = strlen(var);
// Skip all lines not starting with var
while(strncasecmp(p, var, varlen) || !strchr(" \t=", p[varlen])) {
// Skip all lines not starting with var
while(strncasecmp(p, var, varlen) || !strchr(" \t=", p[varlen])) {
@@
-646,25
+657,20
@@
static char *grep(const char *data, const char *var) {
}
static bool finalize_join(void) {
}
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;
}
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) {
if(!check_id(name)) {
fprintf(stderr, "Invalid Name found in invitation!\n");
return false;
}
if(!netname) {
- netname =
grep(data, "NetName"
);
+ netname =
xstrdup(grep(data, "NetName")
);
if(netname && !check_netname(netname, true)) {
fprintf(stderr, "Unsafe NetName found in invitation!\n");
if(netname && !check_netname(netname, true)) {
fprintf(stderr, "Unsafe NetName found in invitation!\n");
@@
-699,7
+705,7
@@
make_names:
// Generate a random netname, ask for a better one later.
ask_netname = true;
// Generate a random netname, ask for a better one later.
ask_netname = true;
- snprintf(temp_netname, sizeof(temp_netname), "join_%x",
rand(
));
+ snprintf(temp_netname, sizeof(temp_netname), "join_%x",
prng(UINT32_MAX
));
netname = temp_netname;
goto make_names;
}
netname = temp_netname;
goto make_names;
}
@@
-764,14
+770,16
@@
make_names:
const char *p = data;
char *l, *value;
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;
}
// Split line into variable and value
// Ignore comments
if(*l == '#') {
continue;
}
// Split line into variable and value
-
in
t len = strcspn(l, "\t =");
+
size_
t len = strcspn(l, "\t =");
value = l + len;
value += strspn(value, "\t ");
value = l + len;
value += strspn(value, "\t ");
@@
-871,12
+879,12
@@
make_names:
return false;
}
return false;
}
- while((l = get_line(&p))) {
+ while((l = get_line(
line, sizeof(line),
&p))) {
if(!strcmp(l, "#---------------------------------------------------------------#")) {
continue;
}
if(!strcmp(l, "#---------------------------------------------------------------#")) {
continue;
}
-
in
t len = strcspn(l, "\t =");
+
size_
t len = strcspn(l, "\t =");
if(len == 4 && !strncasecmp(l, "Name", 4)) {
value = l + len;
if(len == 4 && !strncasecmp(l, "Name", 4)) {
value = l + len;
@@
-988,7
+996,7
@@
ask_netname:
char filename2[PATH_MAX];
snprintf(filename, sizeof(filename), "%s" SLASH "tinc-up.invitation", confbase);
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);
snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up.bat", confbase);
#else
snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up", confbase);
@@
-1020,7
+1028,7
@@
ask_netname:
if(response == 'e') {
char *command;
if(response == 'e') {
char *command;
-#ifndef HAVE_
MINGW
+#ifndef HAVE_
WINDOWS
const char *editor = getenv("VISUAL");
if(!editor) {
const char *editor = getenv("VISUAL");
if(!editor) {
@@
-1054,7
+1062,13
@@
ask_netname:
}
}
} else {
}
}
} else {
- fprintf(stderr, "A tinc-up script was generated, but has been left disabled.\n");
+ if(force) {
+ rename(filename, filename2);
+ chmod(filename2, 0755);
+ fprintf(stderr, "tinc-up enabled.\n");
+ } else {
+ fprintf(stderr, "A tinc-up script was generated, but has been left disabled.\n");
+ }
}
} else {
// A placeholder was generated.
}
} else {
// A placeholder was generated.
@@
-1074,7
+1088,7
@@
static bool invitation_send(void *handle, uint8_t type, const void *vdata, size_
const char *data = vdata;
while(len) {
const char *data = vdata;
while(len) {
-
in
t result = send(sock, data, len, 0);
+
ssize_
t result = send(sock, data, len, 0);
if(result == -1 && sockwouldblock(sockerrno)) {
continue;
if(result == -1 && sockwouldblock(sockerrno)) {
continue;
@@
-1211,10
+1225,11
@@
int cmd_join(int argc, char *argv[]) {
}
if(!port || !*port) {
}
if(!port || !*port) {
- port = "655";
+ static char default_port[] = "655";
+ port = default_port;
}
}
- if(!b64decode
(slash, hash, 24) || !b64decode
(slash + 24, cookie, 24)) {
+ if(!b64decode
_tinc(slash, hash, 24) || !b64decode_tinc
(slash + 24, cookie, 24)) {
goto invalid;
}
goto invalid;
}
@@
-1231,6
+1246,8
@@
int cmd_join(int argc, char *argv[]) {
struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
if(!ai) {
struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
if(!ai) {
+ free(b64key);
+ ecdsa_free(key);
return 1;
}
return 1;
}
@@
-1244,6
+1261,8
@@
next:
if(!aip) {
freeaddrinfo(ai);
if(!aip) {
freeaddrinfo(ai);
+ free(b64key);
+ ecdsa_free(key);
return 1;
}
}
return 1;
}
}
@@
-1268,7
+1287,7
@@
next:
fprintf(stderr, "Connected to %s port %s...\n", address, port);
// Tell him we have an invitation, and give him our throw-away key.
fprintf(stderr, "Connected to %s port %s...\n", address, port);
// Tell him we have an invitation, and give him our throw-away key.
-
in
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", b64key, PROT_MAJOR, PROT_MINOR);
if(len <= 0 || (size_t)len >= sizeof(line)) {
abort();
if(len <= 0 || (size_t)len >= sizeof(line)) {
abort();
@@
-1290,6
+1309,11
@@
next:
}
freeaddrinfo(ai);
}
freeaddrinfo(ai);
+ ai = NULL;
+ aip = NULL;
+
+ free(b64key);
+ b64key = NULL;
// Check if the hash of the key he gave us matches the hash in the URL.
char *fingerprint = line + 2;
// Check if the hash of the key he gave us matches the hash in the URL.
char *fingerprint = line + 2;
@@
-1297,11
+1321,13
@@
next:
if(sha512(fingerprint, strlen(fingerprint), hishash)) {
fprintf(stderr, "Could not create digest\n%s\n", line + 2);
if(sha512(fingerprint, strlen(fingerprint), hishash)) {
fprintf(stderr, "Could not create digest\n%s\n", line + 2);
+ ecdsa_free(key);
return 1;
}
if(memcmp(hishash, hash, 18)) {
fprintf(stderr, "Peer has an invalid key!\n%s\n", line + 2);
return 1;
}
if(memcmp(hishash, hash, 18)) {
fprintf(stderr, "Peer has an invalid key!\n%s\n", line + 2);
+ ecdsa_free(key);
return 1;
}
return 1;
}
@@
-1309,17
+1335,21
@@
next:
ecdsa_t *hiskey = ecdsa_set_base64_public_key(fingerprint);
if(!hiskey) {
ecdsa_t *hiskey = ecdsa_set_base64_public_key(fingerprint);
if(!hiskey) {
+ ecdsa_free(key);
return 1;
}
// Start an SPTPS session
if(!sptps_start(&sptps, NULL, true, false, key, hiskey, "tinc invitation", 15, invitation_send, invitation_receive)) {
return 1;
}
// Start an SPTPS session
if(!sptps_start(&sptps, NULL, true, false, key, hiskey, "tinc invitation", 15, invitation_send, invitation_receive)) {
+ ecdsa_free(hiskey);
+ ecdsa_free(key);
return 1;
}
// Feed rest of input buffer to SPTPS
if(!sptps_receive_data(&sptps, buffer, blen)) {
return 1;
}
// Feed rest of input buffer to SPTPS
if(!sptps_receive_data(&sptps, buffer, blen)) {
- return 1;
+ success = false;
+ goto exit;
}
while((len = recv(sock, line, sizeof(line), 0))) {
}
while((len = recv(sock, line, sizeof(line), 0))) {
@@
-1328,7
+1358,7
@@
next:
continue;
}
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
// 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
@@
-1339,30
+1369,33
@@
next:
#endif
fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, sockstrerror(sockerrno));
#endif
fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, sockstrerror(sockerrno));
- return 1;
+ success = false;
+ goto exit;
}
char *p = line;
while(len) {
}
char *p = line;
while(len) {
-
in
t done = sptps_receive_data(&sptps, p, len);
+
size_
t done = sptps_receive_data(&sptps, p, len);
if(!done) {
if(!done) {
- return 1;
+ success = false;
+ goto exit;
}
}
- len -= done;
+ len -=
(ssize_t)
done;
p += done;
}
}
p += done;
}
}
+exit:
sptps_stop(&sptps);
ecdsa_free(hiskey);
ecdsa_free(key);
closesocket(sock);
if(!success) {
sptps_stop(&sptps);
ecdsa_free(hiskey);
ecdsa_free(key);
closesocket(sock);
if(!success) {
- fprintf(stderr, "
Connection closed by peer, i
nvitation cancelled.\n");
+ fprintf(stderr, "
I
nvitation cancelled.\n");
return 1;
}
return 1;
}