]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - src/misc-progs/openvpnctrl.c
Merge branch 'next' into fifteen
[people/teissler/ipfire-2.x.git] / src / misc-progs / openvpnctrl.c
index 95027577eeb47110bccd1cb9881af9f42f78d68a..257778892a1868bdb302614de2a1fcc970b3d619 100644 (file)
@@ -8,6 +8,7 @@
 #include <netinet/in.h>
 #include <fcntl.h>
 #include "setuid.h"
+#include "netutil.h"
 #include "libsmooth.h"
 
 #define noovpndebug
@@ -163,7 +164,6 @@ connection *getConnections() {
 int readPidFile(const char *pidfile) {
        FILE *fp = fopen(pidfile, "r");
        if (fp == NULL) {
-               fprintf(stderr, "PID file not found: '%s'\n", pidfile);
                exit(1);
        }
 
@@ -175,7 +175,6 @@ int readPidFile(const char *pidfile) {
 }
 
 void ovpnInit(void) {
-       
        // Read OpenVPN configuration
        kv = initkeyvalues();
        if (!readkeyvalues(kv, CONFIG_ROOT "/ovpn/settings")) {
@@ -184,17 +183,14 @@ void ovpnInit(void) {
        }
 
        if (!findkey(kv, "ENABLED", enablered)) {
-               fprintf(stderr, "Cannot read ENABLED\n");
                exit(1);
        }
 
        if (!findkey(kv, "ENABLED_BLUE", enableblue)){
-               fprintf(stderr, "Cannot read ENABLED_BLUE\n");
                exit(1);
        }
 
        if (!findkey(kv, "ENABLED_ORANGE", enableorange)){
-               fprintf(stderr, "Cannot read ENABLED_ORANGE\n");
                exit(1);
        }
        freekeyvalues(kv);
@@ -220,24 +216,22 @@ void ovpnInit(void) {
        }
 
        kv=initkeyvalues();
-       if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))
-       {
+       if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")) {
                fprintf(stderr, "Cannot read ethernet settings\n");
                exit(1);
        }
        
-       if (strcmp(enableblue, "on")==0){
-               if (!findkey(kv, "BLUE_DEV", blueif)){
-                       fprintf(stderr, "Cannot read BLUE_DEV\n");
+       if (strcmp(enableblue, "on") == 0) {
+               if (!findkey(kv, "BLUE_DEV", blueif)) {
                        exit(1);
                }
        }
-       if (strcmp(enableorange, "on")==0){
-               if (!findkey(kv, "ORANGE_DEV", orangeif)){
-                       fprintf(stderr, "Cannot read ORNAGE_DEV\n");
+
+       if (strcmp(enableorange, "on") == 0) {
+               if (!findkey(kv, "ORANGE_DEV", orangeif)) {
                        exit(1);
                }
-       }               
+       }
        freekeyvalues(kv);
 }
 
@@ -259,7 +253,7 @@ void setChainRules(char *chain, char *interface, char *protocol, char *port)
 void flushChain(char *chain) {
        char str[STRING_SIZE];
 
-       sprintf(str, "/sbin/iptables -F %sINPUT", chain);
+       sprintf(str, "/sbin/iptables -F %s", chain);
        executeCommand(str);
 }
 
@@ -270,6 +264,13 @@ void flushChainNAT(char *chain) {
        executeCommand(str);
 }
 
+void flushChainINPUT(char *chain) {
+       char str[STRING_SIZE];
+
+       snprintf(str, STRING_SIZE, "%sINPUT", chain);
+       flushChain(str);
+}
+
 void deleteChainReference(char *chain) {
        char str[STRING_SIZE];
 
@@ -289,9 +290,9 @@ void deleteAllChains(void) {
        deleteChainReference(OVPNRED);
        deleteChainReference(OVPNBLUE);
        deleteChainReference(OVPNORANGE);
-       flushChain(OVPNRED);
-       flushChain(OVPNBLUE);
-       flushChain(OVPNORANGE);
+       flushChainINPUT(OVPNRED);
+       flushChainINPUT(OVPNBLUE);
+       flushChainINPUT(OVPNORANGE);
        deleteChain(OVPNRED);
        deleteChain(OVPNBLUE);
        deleteChain(OVPNORANGE);
@@ -316,7 +317,7 @@ void createChain(char *chain) {
 void createAllChains(void) {
        // create chain and chain references
        if (!strcmp(enableorange, "on")) {
-               if (strlen(orangeif)) {
+               if (strlen(orangeif) > 0) {
                        createChain(OVPNORANGE);
                        createChainReference(OVPNORANGE);
                } else {
@@ -326,7 +327,7 @@ void createAllChains(void) {
        }
 
        if (!strcmp(enableblue, "on")) {
-               if (strlen(blueif)) {
+               if (strlen(blueif) > 0) {
                        createChain(OVPNBLUE);
                        createChainReference(OVPNBLUE);
                } else {
@@ -336,7 +337,7 @@ void createAllChains(void) {
        }
 
        if (!strcmp(enablered, "on")) {
-               if (strlen(redif)) {
+               if (strlen(redif) > 0) {
                        createChain(OVPNRED);
                        createChainReference(OVPNRED);
                } else {
@@ -454,14 +455,13 @@ void setFirewallRules(void) {
 
        if (!findkey(kv, "VPN_IP", dovpnip)){
                fprintf(stderr, "Cannot read VPN_IP\n");
-//             exit(1); step further as we don't need an ip
        }
        freekeyvalues(kv);
 
        // Flush all chains.
-       flushChain(OVPNRED);
-       flushChain(OVPNBLUE);
-       flushChain(OVPNORANGE);
+       flushChainINPUT(OVPNRED);
+       flushChainINPUT(OVPNBLUE);
+       flushChainINPUT(OVPNORANGE);
        flushChain(OVPNBLOCK);
        flushChainNAT(OVPNNAT);
 
@@ -523,7 +523,7 @@ void stopDaemon(void) {
 void startDaemon(void) {
        char command[STRING_SIZE];
        
-       if (!((strcmp(enablered, "on")==0) || (strcmp(enableblue, "on")==0) || (strcmp(enableorange, "on")==0))){
+       if (!((strcmp(enablered, "on") == 0) || (strcmp(enableblue, "on") == 0) || (strcmp(enableorange, "on") == 0))) {
                fprintf(stderr, "OpenVPN is not enabled on any interface\n");
                exit(1);
        } else {