Fix warnings from cppcheck.
authorGuus Sliepen <guus@tinc-vpn.org>
Sun, 7 Oct 2012 15:53:23 +0000 (17:53 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sun, 7 Oct 2012 15:53:23 +0000 (17:53 +0200)
src/conf.c
src/multicast_device.c
src/net.h
src/net_setup.c
src/process.c
src/tincctl.c

index c1302d2..b30feed 100644 (file)
@@ -411,8 +411,7 @@ bool append_config_file(const char *name, const char *key, const char *value) {
                fclose(fp);
        }
 
-
        free(fname);
 
-       return fp;
+       return fp != NULL;
 }
index e5e9a3f..51d5c29 100644 (file)
@@ -38,7 +38,7 @@ static struct addrinfo *ai = NULL;
 static mac_t ignore_src = {{0}};
 
 static bool setup_device(void) {
-       char *host;
+       char *host = NULL;
        char *port;
        char *space;
        int ttl = 1;
@@ -49,14 +49,14 @@ static bool setup_device(void) {
 
        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;
@@ -70,12 +70,12 @@ static bool setup_device(void) {
 
        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
@@ -86,9 +86,8 @@ static bool setup_device(void) {
        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) {
@@ -101,8 +100,7 @@ static bool setup_device(void) {
                        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);
@@ -122,8 +120,7 @@ static bool setup_device(void) {
                        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);
@@ -136,13 +133,23 @@ static bool setup_device(void) {
        
                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) {
index 7bcb85b..d7b868a 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -148,6 +148,9 @@ typedef enum proxytype_t {
 } 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"
index f07bdda..fd595a9 100644 (file)
@@ -52,20 +52,23 @@ char *proxyuser;
 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 */
@@ -78,15 +81,13 @@ bool node_read_ecdsa_public_key(node_t *n) {
 
        /* 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;
        }
 
@@ -95,7 +96,8 @@ bool node_read_ecdsa_public_key(node_t *n) {
 
 exit:
        exit_configuration(&config_tree);
-       free(fname);
+       free(hcfname);
+       free(pubname);
        return result;
 }
 
@@ -386,11 +388,26 @@ char *get_name(void) {
 
 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, ' ')))
@@ -465,32 +482,32 @@ bool setup_myself_reloadable(void) {
        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;
@@ -505,18 +522,18 @@ bool setup_myself_reloadable(void) {
 
        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)
index 362b678..a3cae33 100644 (file)
@@ -226,58 +226,40 @@ bool detach(void) {
 
 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];
index a771b0e..4dc1ff2 100644 (file)
@@ -355,7 +355,7 @@ static FILE *ask_and_open(const char *filename, const char *what, const char *mo
 static bool ecdsa_keygen(bool ask) {
        ecdsa_t key;
        FILE *f;
-       char *filename;
+       char *pubname, *privname;
 
        fprintf(stderr, "Generating ECDSA keypair:\n");
 
@@ -365,8 +365,9 @@ static bool ecdsa_keygen(bool ask) {
        } 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;
@@ -379,14 +380,14 @@ static bool ecdsa_keygen(bool ask) {
        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;
@@ -396,7 +397,6 @@ static bool ecdsa_keygen(bool ask) {
        free(pubkey);
 
        fclose(f);
-       free(filename);
 
        return true;
 }
@@ -408,7 +408,7 @@ static bool ecdsa_keygen(bool ask) {
 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);
 
@@ -418,8 +418,9 @@ static bool rsa_keygen(int bits, bool ask) {
        } 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;
@@ -432,14 +433,14 @@ static bool rsa_keygen(int bits, bool ask) {
        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;
@@ -447,7 +448,6 @@ static bool rsa_keygen(int bits, bool ask) {
        rsa_write_pem_public_key(&key, f);
 
        fclose(f);
-       free(filename);
 
        return true;
 }
@@ -833,12 +833,15 @@ static int cmd_start(int argc, char *argv[]) {
        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);
@@ -1226,7 +1229,7 @@ static struct {
        {"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},
@@ -1245,6 +1248,8 @@ static struct {
        {"ProcessPriority", VAR_SERVER},
        {"Proxy", VAR_SERVER},
        {"ReplayWindow", VAR_SERVER},
+       {"ScriptsExtension", VAR_SERVER},
+       {"ScriptsInterpreter", VAR_SERVER},
        {"StrictSubnets", VAR_SERVER},
        {"TunnelServer", VAR_SERVER},
        {"UDPRcvBuf", VAR_SERVER},
@@ -2133,6 +2138,8 @@ static int cmd_shell(int argc, char *argv[]) {
                }
        }
 
+       free(nargv);
+
        if(tty)
                printf("\n");
        return result;