]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - src/misc-progs/openvpnctrl.c
openvpnctrl: Fix initialization of the program.
[people/teissler/ipfire-2.x.git] / src / misc-progs / openvpnctrl.c
index 68bbe2fff5e874c33180f2bb0a05f42aceb183ee..23924d4f96bfecdf5b75569e2a9657d6b3299954 100644 (file)
@@ -8,7 +8,7 @@
 #include "setuid.h"
 #include "libsmooth.h"
 
-#define noovpndebug
+#define ovpndebug
 
 // global vars
        struct keyvalue *kv = NULL;
@@ -29,6 +29,7 @@ char WRAPPERVERSION[STRING_SIZE] = "ipfire-2.1.2";
 
 struct connection_struct {
        char name[STRING_SIZE];
+       char type[STRING_SIZE];
        char proto[STRING_SIZE];
        int port;
        struct connection_struct *next;
@@ -106,6 +107,8 @@ connection *getConnections() {
                while (result) {
                        if (count == 2) {
                                strcpy(conn_curr->name, result);
+                       } else if (count == 4) {
+                               strcpy(conn_curr->type, result);
                        } else if (count == 12) {
                                strcpy(conn_curr->proto, result);
                        } else if (count == 13) {
@@ -122,6 +125,20 @@ connection *getConnections() {
        return conn_first;
 }
 
+int readPidFile(const char *pidfile) {
+       FILE *fp = fopen(pidfile, "r");
+       if (fp == NULL) {
+               fprintf(stderr, "PID file not found: '%s'\n", pidfile);
+               exit(1);
+       }
+
+       int pid = 0;
+       fscanf(fp, "%d", &pid);
+       fclose(fp);
+
+       return pid;
+}
+
 void ovpnInit(void) {
        
        // Read OpenVPN configuration
@@ -268,39 +285,34 @@ void createChain(char *chain) {
 }
 
 void createAllChains(void) {
-       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 {
-               // create chain and chain references
-               if (!strcmp(enableorange, "on")) {
-                       if (strlen(orangeif)) {
-                               createChain(OVPNORANGE);
-                               createChainReference(OVPNORANGE);
-                       } else {
-                               fprintf(stderr, "OpenVPN enabled on orange but no orange interface found\n");
-                               //exit(1);
-                       }
+       // create chain and chain references
+       if (!strcmp(enableorange, "on")) {
+               if (strlen(orangeif)) {
+                       createChain(OVPNORANGE);
+                       createChainReference(OVPNORANGE);
+               } else {
+                       fprintf(stderr, "OpenVPN enabled on orange but no orange interface found\n");
+                       //exit(1);
                }
-       
-               if (!strcmp(enableblue, "on")) {
-                       if (strlen(blueif)) {
-                               createChain(OVPNBLUE);
-                               createChainReference(OVPNBLUE);
-                       } else {
-                               fprintf(stderr, "OpenVPN enabled on blue but no blue interface found\n");
-                               //exit(1);
-                       }
+       }
+
+       if (!strcmp(enableblue, "on")) {
+               if (strlen(blueif)) {
+                       createChain(OVPNBLUE);
+                       createChainReference(OVPNBLUE);
+               } else {
+                       fprintf(stderr, "OpenVPN enabled on blue but no blue interface found\n");
+                       //exit(1);
                }
-       
-               if (!strcmp(enablered, "on")) {
-                       if (strlen(redif)) {
-                               createChain(OVPNRED);
-                               createChainReference(OVPNRED);
-                       } else {
-                               fprintf(stderr, "OpenVPN enabled on red but no red interface found\n");
-                               //exit(1);
-                       }
+       }
+
+       if (!strcmp(enablered, "on")) {
+               if (strlen(redif)) {
+                       createChain(OVPNRED);
+                       createChainReference(OVPNRED);
+               } else {
+                       fprintf(stderr, "OpenVPN enabled on red but no red interface found\n");
+                       //exit(1);
                }
        }
 }
@@ -310,12 +322,6 @@ void setFirewallRules(void) {
        char dport[STRING_SIZE] = "";
        char dovpnip[STRING_SIZE] = "";
 
-       /* check if it makes sence to proceed further */
-       if (!((strcmp(enablered, "on")==0) || (strcmp(enableblue, "on")==0) || (strcmp(enableorange, "on")==0))){
-               fprintf(stderr, "Config error, at least one device must be enabled\n");
-               exit(1);
-       }
-
        kv = initkeyvalues();
        if (!readkeyvalues(kv, CONFIG_ROOT "/ovpn/settings"))
        {
@@ -340,9 +346,6 @@ void setFirewallRules(void) {
        }
        freekeyvalues(kv);
 
-       // read connection configuration
-       connection *conn = getConnections();
-
        // Flush all chains.
        flushChain(OVPNRED);
        flushChain(OVPNBLUE);
@@ -356,11 +359,18 @@ void setFirewallRules(void) {
        if (!strcmp(enableorange, "on") && strlen(orangeif))
                setChainRules(OVPNORANGE, orangeif, protocol, dport);
 
+       // read connection configuration
+       connection *conn = getConnections();
+
        // set firewall rules for n2n connections
-       char *port;
-       while (conn) {
-               sprintf(port, "%d", conn->port);
-               setChainRules(OVPNRED, redif, conn->proto, port);
+       char command[STRING_SIZE];
+       while (conn != NULL) {
+               if (strcmp(conn->type, "net") == 0) {
+                       sprintf(command, "/sbin/iptables -A %sINPUT -i %s -p %s --dport %d -j ACCEPT",
+                               OVPNRED, redif, conn->proto, conn->port);
+                       executeCommand(command);
+               }
+
                conn = conn->next;
        }
 }
@@ -369,7 +379,7 @@ void stopDaemon(void) {
        char command[STRING_SIZE];
 
        int pid = readPidFile("/var/run/openvpn.pid");
-       if (pid == NULL) {
+       if (!pid > 0) {
                exit(1);
        }
 
@@ -401,7 +411,7 @@ void startNet2Net(char *name) {
        conn_iter = getConnections();
 
        while (conn_iter) {
-               if (strcmp(conn_iter->name, name) == 0) {
+               if ((strcmp(conn_iter->type, "net") == 0) && (strcmp(conn_iter->name, name) == 0)) {
                        conn = conn_iter;
                        break;
                }
@@ -433,20 +443,6 @@ void startNet2Net(char *name) {
        executeCommand(command);
 }
 
-int readPidFile(const char *pidfile) {
-       FILE *fp = fopen(pidfile, "r");
-       if (fp == NULL) {
-               fprintf(stderr, "PID file not found: '%s'\n", pidfile);
-               exit(1);
-       }
-
-       int pid = NULL;
-       fscanf(fp, "%d", &pid);
-       fclose(fp);
-
-       return pid;
-}
-
 void killNet2Net(char *name) {
        connection *conn = NULL;
        connection *conn_iter;
@@ -467,16 +463,20 @@ void killNet2Net(char *name) {
        }
 
        char pidfile[STRING_SIZE];
-       snprintf(&pidfile, STRING_SIZE - 1, "/var/run/%sn2n.pid", conn->name);
+       snprintf(pidfile, STRING_SIZE - 1, "/var/run/%sn2n.pid", conn->name);
 
        int pid = readPidFile(pidfile);
-       if (pid == NULL) {
+       if (!pid > 0) {
                exit(1);
        }
 
        fprintf(stderr, "Killing PID %d.\n", pid);
        kill(pid, SIGTERM);
 
+       char command[STRING_SIZE];
+       snprintf(command, STRING_SIZE - 1, "/bin/rm -f %s", pidfile);
+       executeCommand(command);
+
        exit(0);
 }
 
@@ -494,6 +494,8 @@ int main(int argc, char *argv[]) {
            usage();
 
        if(argc == 3) {
+               ovpnInit();
+
                if( (strcmp(argv[1], "-sn2n") == 0) || (strcmp(argv[1], "--start-net-2-net") == 0) ) {
                        startNet2Net(argv[2]);
                        return 0;