14 struct keyvalue
*kv
= NULL
;
15 FILE *ifacefile
= NULL
;
17 char redif
[STRING_SIZE
];
18 char blueif
[STRING_SIZE
];
19 char orangeif
[STRING_SIZE
];
20 char enablered
[STRING_SIZE
] = "off";
21 char enableblue
[STRING_SIZE
] = "off";
22 char enableorange
[STRING_SIZE
] = "off";
25 char OVPNRED
[STRING_SIZE
] = "OVPN";
26 char OVPNBLUE
[STRING_SIZE
] = "OVPN_BLUE_";
27 char OVPNORANGE
[STRING_SIZE
] = "OVPN_ORANGE_";
28 char WRAPPERVERSION
[STRING_SIZE
] = "ipfire-2.1.2";
30 struct connection_struct
{
31 char name
[STRING_SIZE
];
32 char proto
[STRING_SIZE
];
34 struct connection_struct
*next
;
37 typedef struct connection_struct connection
;
39 void exithandler(void)
50 printf("Wrapper for OpenVPN %s-debug\n", WRAPPERVERSION
);
52 printf("Wrapper for OpenVPN %s\n", WRAPPERVERSION
);
54 printf("openvpnctrl <option>\n");
55 printf(" Valid options are:\n");
56 printf(" -s --start\n");
57 printf(" starts OpenVPN (implicitly creates chains and firewall rules)\n");
58 printf(" -k --kill\n");
59 printf(" kills/stops OpenVPN\n");
60 printf(" -r --restart\n");
61 printf(" restarts OpenVPN (implicitly creates chains and firewall rules)\n");
62 printf(" -d --display\n");
63 printf(" displays OpenVPN status to syslog\n");
64 printf(" -fwr --firewall-rules\n");
65 printf(" removes current OpenVPN chains and rules and resets them according to the config\n");
66 printf(" -sdo --start-daemon-only\n");
67 printf(" starts OpenVPN daemon only\n");
68 printf(" -ccr --create-chains-and-rules\n");
69 printf(" creates chains and rules for OpenVPN\n");
70 printf(" -dcr --delete-chains-and-rules\n");
71 printf(" removes all chains for OpenVPN\n");
75 connection
*getConnections() {
78 if (!(fp
= fopen(CONFIG_ROOT
"/ovpn/ovpnconfig", "r"))) {
79 fprintf(stderr
, "Could not open openvpn n2n configuration file.\n");
83 char line
[STRING_SIZE
] = "";
86 connection
*conn_first
= NULL
;
87 connection
*conn_last
= NULL
;
88 connection
*conn_curr
;
90 while ((fgets(line
, STRING_SIZE
, fp
) != NULL
)) {
91 if (line
[strlen(line
) - 1] == '\n')
92 line
[strlen(line
) - 1] = '\0';
94 conn_curr
= (connection
*)malloc(sizeof(connection
));
95 memset(conn_curr
, 0, sizeof(connection
));
97 if (conn_first
== NULL
) {
98 conn_first
= conn_curr
;
100 conn_last
->next
= conn_curr
;
102 conn_last
= conn_curr
;
105 result
= strtok(line
, ",");
108 strcpy(conn_curr
->name
, result
);
109 } else if (count
== 12) {
110 strcpy(conn_curr
->proto
, result
);
111 } else if (count
== 13) {
112 conn_curr
->port
= atoi(result
);
115 result
= strtok(NULL
, ",");
125 void ovpnInit(void) {
127 // Read OpenVPN configuration
128 kv
= initkeyvalues();
129 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ovpn/settings")) {
130 fprintf(stderr
, "Cannot read ovpn settings\n");
134 if (!findkey(kv
, "ENABLED", enablered
)) {
135 fprintf(stderr
, "Cannot read ENABLED\n");
139 if (!findkey(kv
, "ENABLED_BLUE", enableblue
)){
140 fprintf(stderr
, "Cannot read ENABLED_BLUE\n");
144 if (!findkey(kv
, "ENABLED_ORANGE", enableorange
)){
145 fprintf(stderr
, "Cannot read ENABLED_ORANGE\n");
150 // read interface settings
152 // details for the red int
153 memset(redif
, 0, STRING_SIZE
);
154 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r")))
156 if (fgets(redif
, STRING_SIZE
, ifacefile
))
158 if (redif
[strlen(redif
) - 1] == '\n')
159 redif
[strlen(redif
) - 1] = '\0';
164 if (!VALID_DEVICE(redif
))
166 memset(redif
, 0, STRING_SIZE
);
171 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
173 fprintf(stderr
, "Cannot read ethernet settings\n");
177 if (strcmp(enableblue
, "on")==0){
178 if (!findkey(kv
, "BLUE_DEV", blueif
)){
179 fprintf(stderr
, "Cannot read BLUE_DEV\n");
183 if (strcmp(enableorange
, "on")==0){
184 if (!findkey(kv
, "ORANGE_DEV", orangeif
)){
185 fprintf(stderr
, "Cannot read ORNAGE_DEV\n");
192 void executeCommand(char *command
) {
194 printf(strncat(command
, "\n", 2));
196 safe_system(strncat(command
, " >/dev/null 2>&1", 17));
199 void setChainRules(char *chain
, char *interface
, char *protocol
, char *port
)
201 char str
[STRING_SIZE
];
203 sprintf(str
, "/sbin/iptables -A %sINPUT -i %s -p %s --dport %s -j ACCEPT", chain
, interface
, protocol
, port
);
205 sprintf(str
, "/sbin/iptables -A %sINPUT -i tun+ -j ACCEPT", chain
);
207 sprintf(str
, "/sbin/iptables -A %sFORWARD -i tun+ -j ACCEPT", chain
);
211 void flushChain(char *chain
) {
212 char str
[STRING_SIZE
];
214 sprintf(str
, "/sbin/iptables -F %sINPUT", chain
);
216 sprintf(str
, "/sbin/iptables -F %sFORWARD", chain
);
221 void deleteChainReference(char *chain
) {
222 char str
[STRING_SIZE
];
224 sprintf(str
, "/sbin/iptables -D INPUT -j %sINPUT", chain
);
227 sprintf(str
, "/sbin/iptables -D FORWARD -j %sFORWARD", chain
);
232 void deleteChain(char *chain
) {
233 char str
[STRING_SIZE
];
235 sprintf(str
, "/sbin/iptables -X %sINPUT", chain
);
237 sprintf(str
, "/sbin/iptables -X %sFORWARD", chain
);
241 void deleteAllChains(void) {
242 // not an elegant solution, but to avoid timing problems with undeleted chain references
243 deleteChainReference(OVPNRED
);
244 deleteChainReference(OVPNBLUE
);
245 deleteChainReference(OVPNORANGE
);
247 flushChain(OVPNBLUE
);
248 flushChain(OVPNORANGE
);
249 deleteChain(OVPNRED
);
250 deleteChain(OVPNBLUE
);
251 deleteChain(OVPNORANGE
);
254 void createChainReference(char *chain
) {
255 char str
[STRING_SIZE
];
256 sprintf(str
, "/sbin/iptables -I INPUT %s -j %sINPUT", "14", chain
);
258 sprintf(str
, "/sbin/iptables -I FORWARD %s -j %sFORWARD", "12", chain
);
262 void createChain(char *chain
) {
263 char str
[STRING_SIZE
];
264 sprintf(str
, "/sbin/iptables -N %sINPUT", chain
);
266 sprintf(str
, "/sbin/iptables -N %sFORWARD", chain
);
270 void createAllChains(void) {
271 if (!((strcmp(enablered
, "on")==0) || (strcmp(enableblue
, "on")==0) || (strcmp(enableorange
, "on")==0))){
272 fprintf(stderr
, "OpenVPN is not enabled on any interface\n");
275 // create chain and chain references
276 if (!strcmp(enableorange
, "on")) {
277 if (strlen(orangeif
)) {
278 createChain(OVPNORANGE
);
279 createChainReference(OVPNORANGE
);
281 fprintf(stderr
, "OpenVPN enabled on orange but no orange interface found\n");
286 if (!strcmp(enableblue
, "on")) {
287 if (strlen(blueif
)) {
288 createChain(OVPNBLUE
);
289 createChainReference(OVPNBLUE
);
291 fprintf(stderr
, "OpenVPN enabled on blue but no blue interface found\n");
296 if (!strcmp(enablered
, "on")) {
298 createChain(OVPNRED
);
299 createChainReference(OVPNRED
);
301 fprintf(stderr
, "OpenVPN enabled on red but no red interface found\n");
308 void setFirewallRules(void) {
309 char protocol
[STRING_SIZE
] = "";
310 char dport
[STRING_SIZE
] = "";
311 char dovpnip
[STRING_SIZE
] = "";
313 /* check if it makes sence to proceed further */
314 if (!((strcmp(enablered
, "on")==0) || (strcmp(enableblue
, "on")==0) || (strcmp(enableorange
, "on")==0))){
315 fprintf(stderr
, "Config error, at least one device must be enabled\n");
319 kv
= initkeyvalues();
320 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ovpn/settings"))
322 fprintf(stderr
, "Cannot read ovpn settings\n");
326 /* we got one device, so lets proceed further */
327 if (!findkey(kv
, "DDEST_PORT", dport
)){
328 fprintf(stderr
, "Cannot read DDEST_PORT\n");
332 if (!findkey(kv
, "DPROTOCOL", protocol
)){
333 fprintf(stderr
, "Cannot read DPROTOCOL\n");
337 if (!findkey(kv
, "VPN_IP", dovpnip
)){
338 fprintf(stderr
, "Cannot read VPN_IP\n");
339 // exit(1); step further as we don't need an ip
343 // read connection configuration
344 connection
*conn
= getConnections();
348 flushChain(OVPNBLUE
);
349 flushChain(OVPNORANGE
);
351 // set firewall rules
352 if (!strcmp(enablered
, "on") && strlen(redif
))
353 setChainRules(OVPNRED
, redif
, protocol
, dport
);
354 if (!strcmp(enableblue
, "on") && strlen(blueif
))
355 setChainRules(OVPNBLUE
, blueif
, protocol
, dport
);
356 if (!strcmp(enableorange
, "on") && strlen(orangeif
))
357 setChainRules(OVPNORANGE
, orangeif
, protocol
, dport
);
359 // set firewall rules for n2n connections
362 sprintf(port
, "%d", conn
->port
);
363 setChainRules(OVPNRED
, redif
, conn
->proto
, port
);
368 void stopDaemon(void) {
369 char command
[STRING_SIZE
];
371 int pid
= readPidFile("/var/run/openvpn.pid");
376 fprintf(stderr
, "Killing PID %d.\n", pid
);
379 snprintf(command
, STRING_SIZE
- 1, "/bin/rm -f /var/run/openvpn.pid");
380 executeCommand(command
);
383 void startDaemon(void) {
384 char command
[STRING_SIZE
];
386 if (!((strcmp(enablered
, "on")==0) || (strcmp(enableblue
, "on")==0) || (strcmp(enableorange
, "on")==0))){
387 fprintf(stderr
, "OpenVPN is not enabled on any interface\n");
390 snprintf(command
, STRING_SIZE
-1, "/sbin/modprobe tun");
391 executeCommand(command
);
392 snprintf(command
, STRING_SIZE
-1, "/usr/sbin/openvpn --config /var/ipfire/ovpn/server.conf");
393 executeCommand(command
);
397 void startNet2Net(char *name
) {
398 connection
*conn
= NULL
;
399 connection
*conn_iter
;
401 conn_iter
= getConnections();
404 if (strcmp(conn_iter
->name
, name
) == 0) {
408 conn_iter
= conn_iter
->next
;
412 fprintf(stderr
, "Connection not found.\n");
416 char configfile
[STRING_SIZE
];
417 snprintf(configfile
, STRING_SIZE
- 1, CONFIG_ROOT
"/ovpn/n2nconf/%s/%s.conf",
418 conn
->name
, conn
->name
);
420 FILE *fp
= fopen(configfile
, "r");
422 fprintf(stderr
, "Could not find configuration file for connection '%s' at '%s'.\n",
423 conn
->name
, configfile
);
428 // Make sure all firewall rules are up to date.
431 char command
[STRING_SIZE
];
432 sprintf(command
, "/usr/sbin/openvpn --config %s", configfile
);
433 executeCommand(command
);
436 int readPidFile(const char *pidfile
) {
437 FILE *fp
= fopen(pidfile
, "r");
439 fprintf(stderr
, "PID file not found: '%s'\n", pidfile
);
444 fscanf(fp
, "%d", &pid
);
450 void killNet2Net(char *name
) {
451 connection
*conn
= NULL
;
452 connection
*conn_iter
;
454 conn_iter
= getConnections();
457 if (strcmp(conn_iter
->name
, name
) == 0) {
461 conn_iter
= conn_iter
->next
;
465 fprintf(stderr
, "Connection not found.\n");
469 char pidfile
[STRING_SIZE
];
470 snprintf(&pidfile
, STRING_SIZE
- 1, "/var/run/%sn2n.pid", conn
->name
);
472 int pid
= readPidFile(pidfile
);
477 fprintf(stderr
, "Killing PID %d.\n", pid
);
483 void displayopenvpn(void) {
484 char command
[STRING_SIZE
];
486 snprintf(command
, STRING_SIZE
- 1, "/bin/killall -sSIGUSR2 openvpn");
487 executeCommand(command
);
490 int main(int argc
, char *argv
[]) {
497 if( (strcmp(argv
[1], "-sn2n") == 0) || (strcmp(argv
[1], "--start-net-2-net") == 0) ) {
498 startNet2Net(argv
[2]);
501 else if( (strcmp(argv
[1], "-kn2n") == 0) || (strcmp(argv
[1], "--kill-net-2-net") == 0) ) {
502 killNet2Net(argv
[2]);
510 if( (strcmp(argv
[1], "-k") == 0) || (strcmp(argv
[1], "--kill") == 0) ) {
514 else if( (strcmp(argv
[1], "-d") == 0) || (strcmp(argv
[1], "--display") == 0) ) {
518 else if( (strcmp(argv
[1], "-dcr") == 0) || (strcmp(argv
[1], "--delete-chains-and-rules") == 0) ) {
525 if( (strcmp(argv
[1], "-s") == 0) || (strcmp(argv
[1], "--start") == 0) ) {
532 else if( (strcmp(argv
[1], "-sdo") == 0) || (strcmp(argv
[1], "--start-daemon-only") == 0) ) {
536 else if( (strcmp(argv
[1], "-r") == 0) || (strcmp(argv
[1], "--restart") == 0) ) {
544 else if( (strcmp(argv
[1], "-fwr") == 0) || (strcmp(argv
[1], "--firewall-rules") == 0) ) {
550 else if( (strcmp(argv
[1], "-ccr") == 0) || (strcmp(argv
[1], "--create-chains-and-rules") == 0) ) {