#include "setuid.h"
#include "libsmooth.h"
-#define noovpndebug
+#define ovpndebug
// global vars
struct keyvalue *kv = NULL;
struct connection_struct {
char name[STRING_SIZE];
+ char type[STRING_SIZE];
char proto[STRING_SIZE];
int port;
struct connection_struct *next;
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) {
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
}
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);
}
}
}
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"))
{
}
freekeyvalues(kv);
- // read connection configuration
- connection *conn = getConnections();
-
// Flush all chains.
flushChain(OVPNRED);
flushChain(OVPNBLUE);
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;
}
}
char command[STRING_SIZE];
int pid = readPidFile("/var/run/openvpn.pid");
- if (pid == NULL) {
+ if (!pid > 0) {
exit(1);
}
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;
}
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;
}
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);
}
usage();
if(argc == 3) {
+ ovpnInit();
+
if( (strcmp(argv[1], "-sn2n") == 0) || (strcmp(argv[1], "--start-net-2-net") == 0) ) {
startNet2Net(argv[2]);
return 0;