char OVPNRED[STRING_SIZE] = "OVPN";
char OVPNBLUE[STRING_SIZE] = "OVPN_BLUE_";
char OVPNORANGE[STRING_SIZE] = "OVPN_ORANGE_";
-char WRAPPERVERSION[STRING_SIZE] = "ipfire-2.1.2";
+char WRAPPERVERSION[STRING_SIZE] = "ipfire-2.2.2";
struct connection_struct {
char name[STRING_SIZE];
char type[STRING_SIZE];
char proto[STRING_SIZE];
+ char status[STRING_SIZE];
int port;
struct connection_struct *next;
};
printf(" kills/stops OpenVPN\n");
printf(" -r --restart\n");
printf(" restarts OpenVPN (implicitly creates chains and firewall rules)\n");
+ printf(" -sn2n --start-net-2-net\n");
+ printf(" starts all net2net connections\n");
+ printf(" you may pass a connection name to the switch to only start a specific one\n");
+ printf(" -kn2n --kill-net-2-net\n");
+ printf(" kills all net2net connections\n");
+ printf(" you may pass a connection name to the switch to only start a specific one\n");
printf(" -d --display\n");
printf(" displays OpenVPN status to syslog\n");
printf(" -fwr --firewall-rules\n");
}
char line[STRING_SIZE] = "";
- char *result;
+ char result[STRING_SIZE] = "";
+ char *resultptr;
int count;
connection *conn_first = NULL;
connection *conn_last = NULL;
conn_last = conn_curr;
count = 0;
- result = strtok(line, ",");
- while (result) {
- if (count == 2) {
+ char *lineptr = &line;
+ while (1) {
+ if (*lineptr == NULL)
+ break;
+
+ resultptr = result;
+ while (*lineptr != NULL) {
+ if (*lineptr == ',') {
+ lineptr++;
+ break;
+ }
+ *resultptr++ = *lineptr++;
+ }
+ *resultptr = '\0';
+
+ if (count == 1) {
+ strcpy(conn_curr->status, result);
+ } else if (count == 2) {
strcpy(conn_curr->name, result);
} else if (count == 4) {
strcpy(conn_curr->type, result);
- } else if (count == 12) {
+ } else if (count == 29) {
strcpy(conn_curr->proto, result);
- } else if (count == 13) {
+ } else if (count == 30) {
conn_curr->port = atoi(result);
}
- result = strtok(NULL, ",");
count++;
}
}
}
}
-void startNet2Net(char *name) {
+int startNet2Net(char *name) {
connection *conn = NULL;
connection *conn_iter;
if (conn == NULL) {
fprintf(stderr, "Connection not found.\n");
- exit(1);
+ return 1;
+ }
+
+ if (strcmp(conn->status, "on") != 0) {
+ fprintf(stderr, "Connection '%s' is not enabled.\n", conn->name);
+ return 1;
}
+ fprintf(stderr, "Starting connection %s...\n", conn->name);
+
char configfile[STRING_SIZE];
snprintf(configfile, STRING_SIZE - 1, CONFIG_ROOT "/ovpn/n2nconf/%s/%s.conf",
conn->name, conn->name);
if (fp == NULL) {
fprintf(stderr, "Could not find configuration file for connection '%s' at '%s'.\n",
conn->name, configfile);
- exit(2);
+ return 2;
}
fclose(fp);
setFirewallRules();
char command[STRING_SIZE];
- sprintf(command, "/usr/sbin/openvpn --config %s", configfile);
+ snprintf(command, STRING_SIZE-1, "/sbin/modprobe tun");
+ executeCommand(command);
+ snprintf(command, STRING_SIZE-1, "/usr/sbin/openvpn --config %s", configfile);
executeCommand(command);
+
+ return 0;
}
-void killNet2Net(char *name) {
+int killNet2Net(char *name) {
connection *conn = NULL;
connection *conn_iter;
if (conn == NULL) {
fprintf(stderr, "Connection not found.\n");
- exit(1);
+ return 1;
}
char pidfile[STRING_SIZE];
int pid = readPidFile(pidfile);
if (!pid > 0) {
- exit(1);
+ fprintf(stderr, "Could not read pid file of connection %s.", conn->name);
+ return 1;
}
- fprintf(stderr, "Killing PID %d.\n", pid);
+ fprintf(stderr, "Killing connection %s (PID %d)...\n", conn->name, pid);
kill(pid, SIGTERM);
char command[STRING_SIZE];
snprintf(command, STRING_SIZE - 1, "/bin/rm -f %s", pidfile);
executeCommand(command);
- exit(0);
+ return 0;
+}
+
+void startAllNet2Net() {
+ int exitcode = 0, _exitcode = 0;
+
+ connection *conn = getConnections();
+
+ while(conn) {
+ /* Skip all connections that are not of type "net" or disabled. */
+ if ((strcmp(conn->type, "net") != 0) || (strcmp(conn->status, "on") != 0)) {
+ conn = conn->next;
+ continue;
+ }
+
+ _exitcode = startNet2Net(conn->name);
+ conn = conn->next;
+
+ if (_exitcode > exitcode) {
+ exitcode = _exitcode;
+ }
+ }
+
+ exit(exitcode);
+}
+
+void killAllNet2Net() {
+ int exitcode = 0, _exitcode = 0;
+
+ connection *conn = getConnections();
+
+ while(conn) {
+ /* Skip all connections that are not of type "net". */
+ if (strcmp(conn->type, "net") != 0) {
+ conn = conn->next;
+ continue;
+ }
+
+ _exitcode = killNet2Net(conn->name);
+ conn = conn->next;
+
+ if (_exitcode > exitcode) {
+ exitcode = _exitcode;
+ }
+ }
+
+ exit(exitcode);
}
void displayopenvpn(void) {
startDaemon();
return 0;
}
+ else if( (strcmp(argv[1], "-sn2n") == 0) || (strcmp(argv[1], "--start-net-2-net") == 0) ) {
+ startAllNet2Net();
+ return 0;
+ }
+ else if( (strcmp(argv[1], "-kn2n") == 0) || (strcmp(argv[1], "--kill-net-2-net") == 0) ) {
+ killAllNet2Net();
+ return 0;
+ }
else if( (strcmp(argv[1], "-sdo") == 0) || (strcmp(argv[1], "--start-daemon-only") == 0) ) {
startDaemon();
return 0;