gchild->left->parent = gchild;
gchild->right = child;
- if(gchild->right)
- gchild->right->parent = gchild;
+ gchild->right->parent = gchild;
*superparent = gchild;
gchild->parent = parent;
balnode = parent;
} else {
subst = node->prev;
+ if(!subst) // This only happens if node is not actually in a tree at all.
+ abort();
if(subst == left) {
balnode = subst;
if(!file) {
logger(LOG_ERR, "Unable to open graph dump file %s: %s", filename, strerror(errno));
+ free(filename);
free(tmpname);
return;
}
#ifdef HAVE_MINGW
unlink(filename);
#endif
- rename(tmpname, filename);
+ if(rename(tmpname, filename))
+ logger(LOG_ERR, "Could not rename %s to %s: %s\n", tmpname, filename, strerror(errno));
free(tmpname);
}
+
+ free(filename);
}
ifr.ifr_flags |= IFF_ONE_QUEUE;
#endif
- if(iface)
+ if(iface) {
strncpy(ifr.ifr_name, iface, IFNAMSIZ);
+ ifr.ifr_name[IFNAMSIZ - 1] = 0;
+ }
if(!ioctl(device_fd, TUNSETIFF, &ifr)) {
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
- if(iface) free(iface);
+ ifrname[IFNAMSIZ - 1] = 0;
+ free(iface);
iface = xstrdup(ifrname);
} else if(!ioctl(device_fd, (('T' << 8) | 202), &ifr)) {
logger(LOG_WARNING, "Old ioctl() request was needed for %s", device);
strncpy(ifrname, ifr.ifr_name, IFNAMSIZ);
- if(iface) free(iface);
+ ifrname[IFNAMSIZ - 1] = 0;
+ free(iface);
iface = xstrdup(ifrname);
} else
#endif
overwrite_mac = true;
device_info = "Linux ethertap device";
device_type = DEVICE_TYPE_ETHERTAP;
- if(iface)
- free(iface);
+ free(iface);
iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
}
space = strchr(host, ' ');
if(!space) {
logger(LOG_ERR, "Port number required for %s", device_info);
+ free(host);
return false;
}
device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
if(device_fd < 0) {
logger(LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
+ free(host);
return false;
}
if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
closesocket(device_fd);
logger(LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
+ free(host);
return false;
}
if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof mreq)) {
logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
closesocket(device_fd);
+ free(host);
return false;
}
#ifdef IP_MULTICAST_LOOP
if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&mreq, sizeof mreq)) {
logger(LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
closesocket(device_fd);
+ free(host);
return false;
}
#ifdef IPV6_MULTICAST_LOOP
default:
logger(LOG_ERR, "Multicast for address family %hx unsupported", ai->ai_family);
closesocket(device_fd);
+ free(host);
return false;
}
+ free(host);
logger(LOG_INFO, "%s is a %s", device, device_info);
return true;
closesocket(c->socket);
if(c->edge) {
+ if(!c->node) {
+ logger(LOG_ERR, "Connection to %s (%s) has an edge but node is NULL!", c->name, c->hostname);
+ // And that should never happen.
+ abort();
+ }
+
if(report && !tunnelserver)
send_del_edge(everyone, c->edge);
char *fname, *key, *pubkey;
if(get_config_string(lookup_config(config_tree, "PrivateKey"), &key)) {
- if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) {
- logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
- return false;
- }
myself->connection->rsa_key = RSA_new();
// RSA_blinding_on(myself->connection->rsa_key, NULL);
if(BN_hex2bn(&myself->connection->rsa_key->d, key) != strlen(key)) {
logger(LOG_ERR, "Invalid PrivateKey for myself!");
+ free(key);
+ return false;
+ }
+ free(key);
+ if(!get_config_string(lookup_config(config_tree, "PublicKey"), &pubkey)) {
+ logger(LOG_ERR, "PrivateKey used but no PublicKey found!");
return false;
}
if(BN_hex2bn(&myself->connection->rsa_key->n, pubkey) != strlen(pubkey)) {
logger(LOG_ERR, "Invalid PublicKey for myself!");
+ free(pubkey);
return false;
}
- BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
- free(key);
free(pubkey);
+ BN_hex2bn(&myself->connection->rsa_key->e, "FFFF");
return true;
}
#if !defined(HAVE_MINGW) && !defined(HAVE_CYGWIN)
struct stat s;
- if(fstat(fileno(fp), &s)) {
- logger(LOG_ERR, "Could not stat RSA private key file `%s': %s'",
- fname, strerror(errno));
- free(fname);
- return false;
+ if(!fstat(fileno(fp), &s)) {
+ if(s.st_mode & ~0100700)
+ logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
+ } else {
+ logger(LOG_WARNING, "Could not stat RSA private key file `%s': %s'", fname, strerror(errno));
}
-
- if(s.st_mode & ~0100700)
- logger(LOG_WARNING, "Warning: insecure file permissions for RSA private key file `%s'!", fname);
#endif
myself->connection->rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
if(!envname) {
if(strcmp(name + 1, "HOST")) {
fprintf(stderr, "Invalid Name: environment variable %s does not exist\n", name + 1);
+ free(name);
return false;
}
if(gethostname(hostname, sizeof hostname) || !*hostname) {
fprintf(stderr, "Could not get hostname: %s\n", strerror(errno));
+ free(name);
return false;
}
hostname[31] = 0;
sockaddr2str(&sa, NULL, &myport);
}
- get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
- if(proxy) {
+ if(get_config_string(lookup_config(config_tree, "Proxy"), &proxy)) {
if((space = strchr(proxy, ' ')))
*space++ = 0;
/* Generate packet encryption key */
- if(get_config_string
- (lookup_config(config_tree, "Cipher"), &cipher)) {
+ if(get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) {
if(!strcasecmp(cipher, "none")) {
myself->incipher = NULL;
} else {
return false;
}
}
+ free(cipher);
} else
myself->incipher = EVP_bf_cbc();
if(!myself->indigest) {
logger(LOG_ERR, "Unrecognized digest type!");
+ free(digest);
return false;
}
}
+
+ free(digest);
} else
myself->indigest = EVP_sha1();
else if(!strcasecmp(type, "vde"))
devops = vde_devops;
#endif
+ free(type);
}
if(!devops.setup())
int status;
#endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */
- if(!get_config_string (lookup_config (config_tree, "BindToInterface"), &iface))
+ if(!get_config_string(lookup_config (config_tree, "BindToInterface"), &iface))
return true;
#if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
+ free(iface);
status = setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr));
if(status) {
- logger(LOG_ERR, "Can't bind to interface %s: %s", iface,
- strerror(errno));
+ logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(errno));
return false;
}
+
+ free(iface);
#else /* if !defined(SOL_SOCKET) || !defined(SO_BINDTODEVICE) */
logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
#endif
setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option);
#endif
- if(get_config_string
- (lookup_config(config_tree, "BindToInterface"), &iface)) {
+ if(get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) {
#if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
+ ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0;
+ free(iface);
if(setsockopt(nfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr))) {
closesocket(nfd);
- logger(LOG_ERR, "Can't bind to interface %s: %s", iface,
- strerror(sockerrno));
+ logger(LOG_ERR, "Can't bind to interface %s: %s", ifr.ifr_ifrn.ifrn_name, strerror(sockerrno));
return -1;
}
+
#else
logger(LOG_WARNING, "%s not supported on this platform", "BindToInterface");
#endif
if(!proxytype) {
c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
- configure_tcp(c);
} else if(proxytype == PROXY_EXEC) {
do_outgoing_pipe(c, proxyhost);
} else {
goto begin;
ifdebug(CONNECTIONS) logger(LOG_INFO, "Using proxy at %s port %s", proxyhost, proxyport);
c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
- configure_tcp(c);
}
if(c->socket == -1) {
goto begin;
}
+ if(proxytype != PROXY_EXEC)
+ configure_tcp(c);
+
#ifdef FD_CLOEXEC
fcntl(c->socket, F_SETFD, FD_CLOEXEC);
#endif
if(fscanf(f,"%20ld", &pid) != 1)
pid = 0;
fclose(f);
- return pid;
+ return (pid_t)pid;
}
/* check_pid
return false;
}
- memset(&sa, '0', sizeof(sa));
+ memset(&sa, 0, sizeof(sa));
sa.sll_family = AF_PACKET;
sa.sll_protocol = htons(ETH_P_ALL);
sa.sll_ifindex = ifr.ifr_ifindex;
break;
case 'c': /* config file */
+ if(confbase) {
+ fprintf(stderr, "Only one configuration directory can be given.\n");
+ usage(true);
+ return false;
+ }
confbase = xstrdup(optarg);
break;
case 'n': /* net name given */
/* netname "." is special: a "top-level name" */
+ if(netname) {
+ fprintf(stderr, "Only one netname can be given.\n");
+ usage(true);
+ return false;
+ }
netname = strcmp(optarg, ".") != 0 ? xstrdup(optarg) : NULL;
break;
use_logfile = true;
if(!optarg && optind < argc && *argv[optind] != '-')
optarg = argv[optind++];
- if(optarg)
+ if(optarg) {
+ if(logfilename) {
+ fprintf(stderr, "Only one logfile can be given.\n");
+ usage(true);
+ return false;
+ }
logfilename = xstrdup(optarg);
+ }
break;
case 5: /* write PID to a file */
+ if(pidfilename) {
+ fprintf(stderr, "Only one pidfile can be given.\n");
+ usage(true);
+ return false;
+ }
pidfilename = xstrdup(optarg);
break;
static bool keygen(int bits) {
RSA *rsa_key;
FILE *f;
- char *name = get_name();
char *pubname, *privname;
fprintf(stderr, "Generating %d bits keys:\n", bits);
PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
fclose(f);
- if(name)
+ char *name = get_name();
+
+ if(name) {
xasprintf(&pubname, "%s/hosts/%s", confbase, name);
- else
+ free(name);
+ } else {
xasprintf(&pubname, "%s/rsa_key.pub", confbase);
+ }
f = ask_and_open(pubname, "public RSA key");
free(pubname);
fputc('\n', f);
PEM_write_RSAPublicKey(f, rsa_key);
fclose(f);
- free(name);
return true;
}