scan_for_hostname(tinc_conf, &hostname, &port);
}
+ free(name);
+ name = NULL;
+
if(hostname) {
goto done;
}
if(s >= 0) {
send(s, request, sizeof(request) - 1, 0);
- int 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;
// 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;
}
if(!tty) {
if(!hostname) {
fprintf(stderr, "Could not determine the external address or hostname. Please set Address manually.\n");
+ free(port);
return NULL;
}
if(!fgets(line, sizeof(line), stdin)) {
fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
free(hostname);
+ free(port);
return NULL;
}
}
for(char *p = line; *p; p++) {
- if(isalnum(*p) || *p == '-' || *p == '.') {
+ if(isalnum((uint8_t) *p) || *p == '-' || *p == '.') {
continue;
}
return 1;
}
+ free(myname);
myname = get_my_name(true);
if(!myname) {
char invname[PATH_MAX];
struct stat st;
- if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name) >= sizeof(invname)) {
+ if((size_t)snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name) >= sizeof(invname)) {
fprintf(stderr, "Filename too long: %s" SLASH "%s\n", filename, ent->d_name);
continue;
}
if(!f) {
fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno));
+ free(key);
return 1;
}
if(!ecdsa_write_pem_private_key(key, f)) {
fprintf(stderr, "Could not write ECDSA private key\n");
fclose(f);
+ free(key);
return 1;
}
sha512(fingerprint, strlen(fingerprint), hash);
b64encode_urlsafe(hash, hash, 18);
+ 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
- char buf[18 + strlen(fingerprint)];
+ uint8_t buf[18 + strlen(fingerprint)];
char cookiehash[64];
memcpy(buf, cookie, 18);
memcpy(buf + 18, fingerprint, sizeof(buf) - 18);
sha512(buf, sizeof(buf), cookiehash);
b64encode_urlsafe(cookiehash, cookiehash, 18);
+ free(fingerprint);
+
b64encode_urlsafe(cookie, cookie, 18);
// Create a file containing the details of the invitation.
static char line[1024];
const char *end = strchr(*data, '\n');
- size_t len = end ? end - *data : strlen(*data);
+ size_t len = end ? (size_t)(end - *data) : strlen(*data);
if(len >= sizeof(line)) {
fprintf(stderr, "Maximum line length exceeded!\n");
return NULL;
}
- if(len && !isprint(**data)) {
+ if(len && !isprint((uint8_t) **data)) {
abort();
}
static char value[1024];
const char *p = data;
- int varlen = strlen(var);
+ size_t varlen = strlen(var);
// Skip all lines not starting with var
while(strncasecmp(p, var, varlen) || !strchr(" \t=", p[varlen])) {
return xstrdup(p);
}
- if(e - p >= sizeof(value)) {
+ if((size_t)(e - p) >= sizeof(value)) {
fprintf(stderr, "Maximum line length exceeded!\n");
return NULL;
}
}
static bool finalize_join(void) {
- char *name = xstrdup(get_value(data, "Name"));
+ const char *temp_name = get_value(data, "Name");
- if(!name) {
+ if(!temp_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;
}
// Split line into variable and value
- int len = strcspn(l, "\t =");
+ size_t len = strcspn(l, "\t =");
value = l + len;
value += strspn(value, "\t ");
fprintf(stderr, "Ignoring unknown variable '%s' in invitation.\n", l);
continue;
} else if(!(variables[i].type & VAR_SAFE)) {
- fprintf(stderr, "Ignoring unsafe variable '%s' in invitation.\n", l);
- continue;
+ if(force) {
+ fprintf(stderr, "Warning: unsafe variable '%s' in invitation.\n", l);
+ } else {
+ fprintf(stderr, "Ignoring unsafe variable '%s' in invitation.\n", l);
+ continue;
+ }
}
// Copy the safe variable to the right config file
continue;
}
- int len = strcspn(l, "\t =");
+ size_t len = strcspn(l, "\t =");
if(len == 4 && !strncasecmp(l, "Name", 4)) {
value = l + len;
char newbase[PATH_MAX];
- if(snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line) >= sizeof(newbase)) {
+ if((size_t)snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line) >= sizeof(newbase)) {
fprintf(stderr, "Filename too long: " CONFDIR SLASH "tinc" SLASH "%s\n", line);
goto ask_netname;
}
char filename2[PATH_MAX];
snprintf(filename, sizeof(filename), "%s" SLASH "tinc-up.invitation", confbase);
+
+#ifdef HAVE_MINGW
+ snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up.bat", confbase);
+#else
snprintf(filename2, sizeof(filename2), "%s" SLASH "tinc-up", confbase);
+#endif
if(valid_tinc_up) {
if(tty) {
if(response == 'e') {
char *command;
#ifndef HAVE_MINGW
- xasprintf(&command, "\"%s\" \"%s\"", getenv("VISUAL") ? : getenv("EDITOR") ? : "vi", filename);
+ const char *editor = getenv("VISUAL");
+
+ if(!editor) {
+ editor = getenv("EDITOR");
+ }
+
+ if(!editor) {
+ editor = "vi";
+ }
+
+ xasprintf(&command, "\"%s\" \"%s\"", editor, filename);
#else
xasprintf(&command, "edit \"%s\"", filename);
#endif
}
}
} 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.
}
-static bool invitation_send(void *handle, uint8_t type, const void *data, size_t len) {
+static bool invitation_send(void *handle, uint8_t type, const void *vdata, size_t len) {
+ (void)handle;
+ (void)type;
+ const uint8_t *data = vdata;
+
while(len) {
- int result = send(sock, data, len, 0);
+ ssize_t result = send(sock, data, len, 0);
- if(result == -1 && errno == EINTR) {
+ if(result == -1 && sockwouldblock(sockerrno)) {
continue;
} else if(result <= 0) {
return false;
}
static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint16_t len) {
+ (void)handle;
+
switch(type) {
case SPTPS_HANDSHAKE:
return sptps_send_record(&sptps, 0, cookie, sizeof(cookie));
struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
if(!ai) {
+ free(b64key);
return 1;
}
if(!aip) {
freeaddrinfo(ai);
+ free(b64key);
return 1;
}
}
fprintf(stderr, "Connected to %s port %s...\n", address, port);
// Tell him we have an invitation, and give him our throw-away key.
- int 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 || len >= sizeof(line)) {
+ if(len <= 0 || (size_t)len >= sizeof(line)) {
abort();
}
}
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;
while((len = recv(sock, line, sizeof(line), 0))) {
if(len < 0) {
- if(errno == EINTR) {
+ if(sockwouldblock(sockerrno)) {
continue;
}
- fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, strerror(errno));
+#if HAVE_MINGW
+
+ // 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
+ // have to do an explicit check here.
+ if(sockshutdown(sockerrno)) {
+ break;
+ }
+
+#endif
+ fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, sockstrerror(sockerrno));
return 1;
}
char *p = line;
while(len) {
- int done = sptps_receive_data(&sptps, p, len);
+ size_t done = sptps_receive_data(&sptps, p, len);
if(!done) {
return 1;
}
- len -= done;
+ len -= (ssize_t) done;
p += done;
}
}