fclose(fp);
}
-
free(fname);
- return fp;
+ return fp != NULL;
}
static mac_t ignore_src = {{0}};
static bool setup_device(void) {
- char *host;
+ char *host = NULL;
char *port;
char *space;
int ttl = 1;
if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
logger(DEBUG_ALWAYS, LOG_ERR, "Device variable required for %s", device_info);
- return false;
+ goto error;
}
host = xstrdup(device);
space = strchr(host, ' ');
if(!space) {
logger(DEBUG_ALWAYS, LOG_ERR, "Port number required for %s", device_info);
- return false;
+ goto error;
}
*space++ = 0;
ai = str2addrinfo(host, port, SOCK_DGRAM);
if(!ai)
- return false;
+ goto error;
device_fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP);
if(device_fd < 0) {
logger(DEBUG_ALWAYS, LOG_ERR, "Creating socket failed: %s", sockstrerror(sockerrno));
- return false;
+ goto error;
}
#ifdef FD_CLOEXEC
setsockopt(device_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one);
if(bind(device_fd, ai->ai_addr, ai->ai_addrlen)) {
- closesocket(device_fd);
logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s %s: %s", host, port, sockstrerror(sockerrno));
- return false;
+ goto error;
}
switch(ai->ai_family) {
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if(setsockopt(device_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof mreq)) {
logger(DEBUG_ALWAYS, LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
- closesocket(device_fd);
- return false;
+ goto error;
}
#ifdef IP_MULTICAST_LOOP
setsockopt(device_fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const void *)&one, sizeof one);
mreq.ipv6mr_interface = in6.sin6_scope_id;
if(setsockopt(device_fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (void *)&mreq, sizeof mreq)) {
logger(DEBUG_ALWAYS, LOG_ERR, "Cannot join multicast group %s %s: %s", host, port, sockstrerror(sockerrno));
- closesocket(device_fd);
- return false;
+ goto error;
}
#ifdef IPV6_MULTICAST_LOOP
setsockopt(device_fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const void *)&one, sizeof one);
default:
logger(DEBUG_ALWAYS, LOG_ERR, "Multicast for address family %hx unsupported", ai->ai_family);
- closesocket(device_fd);
- return false;
+ goto error;
}
+ freeaddrinfo(ai);
+
logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
return true;
+
+error:
+ if(device_fd >= 0)
+ closesocket(device_fd);
+ if(ai)
+ freeaddrinfo(ai);
+ free(host);
+
+ return false;
}
static void close_device(void) {
} proxytype_t;
extern proxytype_t proxytype;
+extern char *scriptinterpreter;
+extern char *scriptextension;
+
/* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */
#include "connection.h"
#include "node.h"
char *proxypass;
proxytype_t proxytype;
+char *scriptinterpreter;
+char *scriptextension;
+
bool node_read_ecdsa_public_key(node_t *n) {
if(ecdsa_active(&n->ecdsa))
return true;
splay_tree_t *config_tree;
FILE *fp;
- char *fname;
+ char *pubname = NULL, *hcfname = NULL;
char *p;
bool result = false;
- xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
+ xasprintf(&hcfname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
init_configuration(&config_tree);
- if(!read_config_file(config_tree, fname))
+ if(!read_config_file(config_tree, hcfname))
goto exit;
/* First, check for simple ECDSAPublicKey statement */
/* Else, check for ECDSAPublicKeyFile statement and read it */
- free(fname);
+ if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &pubname))
+ xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
- if(!get_config_string(lookup_config(config_tree, "ECDSAPublicKeyFile"), &fname))
- xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name);
-
- fp = fopen(fname, "r");
+ fp = fopen(pubname, "r");
if(!fp) {
- logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", fname, strerror(errno));
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA public key file `%s': %s", pubname, strerror(errno));
goto exit;
}
exit:
exit_configuration(&config_tree);
- free(fname);
+ free(hcfname);
+ free(pubname);
return result;
}
bool setup_myself_reloadable(void) {
char *proxy = NULL;
- char *mode = NULL;
+ char *rmode = NULL;
+ char *fmode = NULL;
+ char *bmode = NULL;
char *afname = NULL;
char *space;
bool choice;
+ free(scriptinterpreter);
+ scriptinterpreter = NULL;
+ get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter);
+
+
+ free(scriptextension);
+ if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension))
+#ifdef HAVE_MINGW
+ scriptextension = xstrdup(".bat");
+#else
+ scriptextension = xstrdup("");
+#endif
+
get_config_string(lookup_config(config_tree, "Proxy"), &proxy);
if(proxy) {
if((space = strchr(proxy, ' ')))
get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly);
get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery);
- if(get_config_string(lookup_config(config_tree, "Mode"), &mode)) {
- if(!strcasecmp(mode, "router"))
+ if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) {
+ if(!strcasecmp(rmode, "router"))
routing_mode = RMODE_ROUTER;
- else if(!strcasecmp(mode, "switch"))
+ else if(!strcasecmp(rmode, "switch"))
routing_mode = RMODE_SWITCH;
- else if(!strcasecmp(mode, "hub"))
+ else if(!strcasecmp(rmode, "hub"))
routing_mode = RMODE_HUB;
else {
logger(DEBUG_ALWAYS, LOG_ERR, "Invalid routing mode!");
return false;
}
- free(mode);
+ free(rmode);
}
- if(get_config_string(lookup_config(config_tree, "Forwarding"), &mode)) {
- if(!strcasecmp(mode, "off"))
+ if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) {
+ if(!strcasecmp(fmode, "off"))
forwarding_mode = FMODE_OFF;
- else if(!strcasecmp(mode, "internal"))
+ else if(!strcasecmp(fmode, "internal"))
forwarding_mode = FMODE_INTERNAL;
- else if(!strcasecmp(mode, "kernel"))
+ else if(!strcasecmp(fmode, "kernel"))
forwarding_mode = FMODE_KERNEL;
else {
logger(DEBUG_ALWAYS, LOG_ERR, "Invalid forwarding mode!");
return false;
}
- free(mode);
+ free(fmode);
}
choice = true;
get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance);
get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl);
- if(get_config_string(lookup_config(config_tree, "Broadcast"), &mode)) {
- if(!strcasecmp(mode, "no"))
+ if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) {
+ if(!strcasecmp(bmode, "no"))
broadcast_mode = BMODE_NONE;
- else if(!strcasecmp(mode, "yes") || !strcasecmp(mode, "mst"))
+ else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst"))
broadcast_mode = BMODE_MST;
- else if(!strcasecmp(mode, "direct"))
+ else if(!strcasecmp(bmode, "direct"))
broadcast_mode = BMODE_DIRECT;
else {
logger(DEBUG_ALWAYS, LOG_ERR, "Invalid broadcast mode!");
return false;
}
- free(mode);
+ free(bmode);
}
#if !defined(SOL_IP) || !defined(IP_TOS)
bool execute_script(const char *name, char **envp) {
#ifdef HAVE_SYSTEM
- int status, len;
char *scriptname;
- int i;
- char *interpreter = NULL;
+ char *command;
-#ifndef HAVE_MINGW
- len = xasprintf(&scriptname, "\"%s" SLASH "%s\"", confbase, name);
-#else
- len = xasprintf(&scriptname, "\"%s" SLASH "%s.bat\"", confbase, name);
-#endif
- if(len < 0)
- return false;
+ xasprintf(&scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
- scriptname[len - 1] = '\0';
-
-#ifndef HAVE_TUNEMU
/* First check if there is a script */
- if(access(scriptname + 1, F_OK)) {
+ if(access(scriptname, F_OK)) {
free(scriptname);
return true;
}
-#endif
-
- // Custom scripts interpreter
- if(get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &interpreter)) {
- // Force custom scripts interpreter allowing execution of scripts on android without execution flag (such as on /sdcard)
- free(scriptname);
- len = xasprintf(&scriptname, "%s \"%s/%s\"", interpreter, confbase, name);
- free(interpreter);
- if(len < 0)
- return false;
- }
logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
-
#ifdef HAVE_PUTENV
/* Set environment */
- for(i = 0; envp[i]; i++)
+ for(int i = 0; envp[i]; i++)
putenv(envp[i]);
#endif
- scriptname[len - 1] = '\"';
- status = system(scriptname);
+ if(scriptinterpreter)
+ xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
+ else
+ xasprintf(&command, "\"%s\"", scriptname);
+
+ int status = system(command);
+ free(command);
free(scriptname);
/* Unset environment */
- for(i = 0; envp[i]; i++) {
+ for(int i = 0; envp[i]; i++) {
char *e = strchr(envp[i], '=');
if(e) {
char p[e - envp[i] + 1];
static bool ecdsa_keygen(bool ask) {
ecdsa_t key;
FILE *f;
- char *filename;
+ char *pubname, *privname;
fprintf(stderr, "Generating ECDSA keypair:\n");
} else
fprintf(stderr, "Done.\n");
- xasprintf(&filename, "%s" SLASH "ecdsa_key.priv", confbase);
- f = ask_and_open(filename, "private ECDSA key", "a", ask);
+ xasprintf(&privname, "%s" SLASH "ecdsa_key.priv", confbase);
+ f = ask_and_open(privname, "private ECDSA key", "a", ask);
+ free(privname);
if(!f)
return false;
ecdsa_write_pem_private_key(&key, f);
fclose(f);
- free(filename);
if(name)
- xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
+ xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
else
- xasprintf(&filename, "%s" SLASH "ecdsa_key.pub", confbase);
+ xasprintf(&pubname, "%s" SLASH "ecdsa_key.pub", confbase);
- f = ask_and_open(filename, "public ECDSA key", "a", ask);
+ f = ask_and_open(pubname, "public ECDSA key", "a", ask);
+ free(pubname);
if(!f)
return false;
free(pubkey);
fclose(f);
- free(filename);
return true;
}
static bool rsa_keygen(int bits, bool ask) {
rsa_t key;
FILE *f;
- char *filename;
+ char *pubname, *privname;
fprintf(stderr, "Generating %d bits keys:\n", bits);
} else
fprintf(stderr, "Done.\n");
- xasprintf(&filename, "%s" SLASH "rsa_key.priv", confbase);
- f = ask_and_open(filename, "private RSA key", "a", ask);
+ xasprintf(&privname, "%s" SLASH "rsa_key.priv", confbase);
+ f = ask_and_open(privname, "private RSA key", "a", ask);
+ free(privname);
if(!f)
return false;
rsa_write_pem_private_key(&key, f);
fclose(f);
- free(filename);
if(name)
- xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, name);
+ xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, name);
else
- xasprintf(&filename, "%s" SLASH "rsa_key.pub", confbase);
+ xasprintf(&pubname, "%s" SLASH "rsa_key.pub", confbase);
- f = ask_and_open(filename, "public RSA key", "a", ask);
+ f = ask_and_open(pubname, "public RSA key", "a", ask);
+ free(pubname);
if(!f)
return false;
rsa_write_pem_public_key(&key, f);
fclose(f);
- free(filename);
return true;
}
pid_t pid = fork();
if(pid == -1) {
fprintf(stderr, "Could not fork: %s\n", strerror(errno));
+ free(nargv);
return 1;
}
if(!pid)
exit(execvp(c, nargv));
+ free(nargv);
+
int status = -1;
if(waitpid(pid, &status, 0) != pid || !WIFEXITED(status) || WEXITSTATUS(status)) {
fprintf(stderr, "Error starting %s\n", c);
{"ECDSAPrivateKeyFile", VAR_SERVER},
{"ExperimentalProtocol", VAR_SERVER},
{"Forwarding", VAR_SERVER},
- {"GraphDumpFile", VAR_SERVER},
+ {"GraphDumpFile", VAR_SERVER | VAR_OBSOLETE},
{"Hostnames", VAR_SERVER},
{"IffOneQueue", VAR_SERVER},
{"Interface", VAR_SERVER},
{"ProcessPriority", VAR_SERVER},
{"Proxy", VAR_SERVER},
{"ReplayWindow", VAR_SERVER},
+ {"ScriptsExtension", VAR_SERVER},
+ {"ScriptsInterpreter", VAR_SERVER},
{"StrictSubnets", VAR_SERVER},
{"TunnelServer", VAR_SERVER},
{"UDPRcvBuf", VAR_SERVER},
}
}
+ free(nargv);
+
if(tty)
printf("\n");
return result;