]>
Commit | Line | Data |
---|---|---|
39877197 | 1 | #include <signal.h> |
6e13d0a5 MT |
2 | #include <stdio.h> |
3 | #include <string.h> | |
4 | #include <unistd.h> | |
5 | #include <stdlib.h> | |
6 | #include <sys/types.h> | |
7 | #include <fcntl.h> | |
8 | #include "setuid.h" | |
9 | #include "libsmooth.h" | |
10 | ||
6e13d0a5 MT |
11 | #define noovpndebug |
12 | ||
13 | // global vars | |
14 | struct keyvalue *kv = NULL; | |
15 | FILE *ifacefile = NULL; | |
16 | ||
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"; | |
23 | ||
24 | // consts | |
25 | char OVPNRED[STRING_SIZE] = "OVPN"; | |
26 | char OVPNBLUE[STRING_SIZE] = "OVPN_BLUE_"; | |
27 | char OVPNORANGE[STRING_SIZE] = "OVPN_ORANGE_"; | |
39877197 | 28 | char WRAPPERVERSION[STRING_SIZE] = "ipfire-2.1.2"; |
6925b8ef AF |
29 | |
30 | struct connection_struct { | |
31 | char name[STRING_SIZE]; | |
32 | char proto[STRING_SIZE]; | |
33 | int port; | |
34 | struct connection_struct *next; | |
35 | }; | |
36 | ||
37 | typedef struct connection_struct connection; | |
6e13d0a5 MT |
38 | |
39 | void exithandler(void) | |
40 | { | |
41 | if(kv) | |
42 | freekeyvalues(kv); | |
43 | if (ifacefile) | |
44 | fclose(ifacefile); | |
45 | } | |
46 | ||
47 | void usage(void) | |
48 | { | |
49 | #ifdef ovpndebug | |
07081137 | 50 | printf("Wrapper for OpenVPN %s-debug\n", WRAPPERVERSION); |
6e13d0a5 | 51 | #else |
07081137 | 52 | printf("Wrapper for OpenVPN %s\n", WRAPPERVERSION); |
6e13d0a5 MT |
53 | #endif |
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"); | |
afabe9f7 | 67 | printf(" starts OpenVPN daemon only\n"); |
6e13d0a5 MT |
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"); | |
72 | exit(1); | |
73 | } | |
74 | ||
6925b8ef AF |
75 | connection *getConnections() { |
76 | FILE *fp = NULL; | |
77 | ||
78 | if (!(fp = fopen(CONFIG_ROOT "/ovpn/ovpnconfig", "r"))) { | |
79 | fprintf(stderr, "Could not open openvpn n2n configuration file.\n"); | |
80 | exit(1); | |
81 | } | |
82 | ||
83 | char line[STRING_SIZE] = ""; | |
84 | char *result; | |
85 | int count; | |
86 | connection *conn_first = NULL; | |
87 | connection *conn_last = NULL; | |
88 | connection *conn_curr; | |
89 | ||
90 | while ((fgets(line, STRING_SIZE, fp) != NULL)) { | |
91 | if (line[strlen(line) - 1] == '\n') | |
92 | line[strlen(line) - 1] = '\0'; | |
93 | ||
94 | conn_curr = (connection *)malloc(sizeof(connection)); | |
95 | memset(conn_curr, 0, sizeof(connection)); | |
96 | ||
97 | if (conn_first == NULL) { | |
98 | conn_first = conn_curr; | |
99 | } else { | |
100 | conn_last->next = conn_curr; | |
101 | } | |
102 | conn_last = conn_curr; | |
103 | ||
104 | count = 0; | |
105 | result = strtok(line, ","); | |
106 | while (result) { | |
107 | if (count == 2) { | |
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); | |
113 | } | |
114 | ||
115 | result = strtok(NULL, ","); | |
116 | count++; | |
117 | } | |
118 | } | |
119 | ||
120 | fclose(fp); | |
121 | ||
122 | return conn_first; | |
123 | } | |
124 | ||
6e13d0a5 MT |
125 | void ovpnInit(void) { |
126 | ||
127 | // Read OpenVPN configuration | |
128 | kv = initkeyvalues(); | |
129 | if (!readkeyvalues(kv, CONFIG_ROOT "/ovpn/settings")) { | |
130 | fprintf(stderr, "Cannot read ovpn settings\n"); | |
131 | exit(1); | |
132 | } | |
133 | ||
134 | if (!findkey(kv, "ENABLED", enablered)) { | |
135 | fprintf(stderr, "Cannot read ENABLED\n"); | |
136 | exit(1); | |
137 | } | |
138 | ||
139 | if (!findkey(kv, "ENABLED_BLUE", enableblue)){ | |
140 | fprintf(stderr, "Cannot read ENABLED_BLUE\n"); | |
141 | exit(1); | |
142 | } | |
143 | ||
144 | if (!findkey(kv, "ENABLED_ORANGE", enableorange)){ | |
145 | fprintf(stderr, "Cannot read ENABLED_ORANGE\n"); | |
146 | exit(1); | |
147 | } | |
148 | freekeyvalues(kv); | |
149 | ||
150 | // read interface settings | |
151 | ||
152 | // details for the red int | |
153 | memset(redif, 0, STRING_SIZE); | |
154 | if ((ifacefile = fopen(CONFIG_ROOT "/red/iface", "r"))) | |
155 | { | |
156 | if (fgets(redif, STRING_SIZE, ifacefile)) | |
157 | { | |
158 | if (redif[strlen(redif) - 1] == '\n') | |
159 | redif[strlen(redif) - 1] = '\0'; | |
160 | } | |
161 | fclose (ifacefile); | |
162 | ifacefile = NULL; | |
163 | ||
164 | if (!VALID_DEVICE(redif)) | |
165 | { | |
166 | memset(redif, 0, STRING_SIZE); | |
167 | } | |
168 | } | |
169 | ||
170 | kv=initkeyvalues(); | |
171 | if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")) | |
172 | { | |
173 | fprintf(stderr, "Cannot read ethernet settings\n"); | |
174 | exit(1); | |
175 | } | |
176 | ||
177 | if (strcmp(enableblue, "on")==0){ | |
178 | if (!findkey(kv, "BLUE_DEV", blueif)){ | |
179 | fprintf(stderr, "Cannot read BLUE_DEV\n"); | |
180 | exit(1); | |
181 | } | |
182 | } | |
183 | if (strcmp(enableorange, "on")==0){ | |
184 | if (!findkey(kv, "ORANGE_DEV", orangeif)){ | |
185 | fprintf(stderr, "Cannot read ORNAGE_DEV\n"); | |
186 | exit(1); | |
187 | } | |
188 | } | |
189 | freekeyvalues(kv); | |
190 | } | |
191 | ||
192 | void executeCommand(char *command) { | |
193 | #ifdef ovpndebug | |
194 | printf(strncat(command, "\n", 2)); | |
195 | #endif | |
196 | safe_system(strncat(command, " >/dev/null 2>&1", 17)); | |
197 | } | |
198 | ||
199 | void setChainRules(char *chain, char *interface, char *protocol, char *port) | |
200 | { | |
201 | char str[STRING_SIZE]; | |
07081137 | 202 | |
6e13d0a5 MT |
203 | sprintf(str, "/sbin/iptables -A %sINPUT -i %s -p %s --dport %s -j ACCEPT", chain, interface, protocol, port); |
204 | executeCommand(str); | |
205 | sprintf(str, "/sbin/iptables -A %sINPUT -i tun+ -j ACCEPT", chain); | |
206 | executeCommand(str); | |
207 | sprintf(str, "/sbin/iptables -A %sFORWARD -i tun+ -j ACCEPT", chain); | |
208 | executeCommand(str); | |
209 | } | |
210 | ||
211 | void flushChain(char *chain) { | |
212 | char str[STRING_SIZE]; | |
213 | ||
214 | sprintf(str, "/sbin/iptables -F %sINPUT", chain); | |
215 | executeCommand(str); | |
216 | sprintf(str, "/sbin/iptables -F %sFORWARD", chain); | |
217 | executeCommand(str); | |
218 | safe_system(str); | |
219 | } | |
220 | ||
221 | void deleteChainReference(char *chain) { | |
222 | char str[STRING_SIZE]; | |
223 | ||
224 | sprintf(str, "/sbin/iptables -D INPUT -j %sINPUT", chain); | |
225 | executeCommand(str); | |
226 | safe_system(str); | |
227 | sprintf(str, "/sbin/iptables -D FORWARD -j %sFORWARD", chain); | |
228 | executeCommand(str); | |
229 | safe_system(str); | |
230 | } | |
231 | ||
232 | void deleteChain(char *chain) { | |
233 | char str[STRING_SIZE]; | |
234 | ||
235 | sprintf(str, "/sbin/iptables -X %sINPUT", chain); | |
236 | executeCommand(str); | |
237 | sprintf(str, "/sbin/iptables -X %sFORWARD", chain); | |
238 | executeCommand(str); | |
239 | } | |
240 | ||
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); | |
246 | flushChain(OVPNRED); | |
247 | flushChain(OVPNBLUE); | |
248 | flushChain(OVPNORANGE); | |
249 | deleteChain(OVPNRED); | |
250 | deleteChain(OVPNBLUE); | |
251 | deleteChain(OVPNORANGE); | |
252 | } | |
253 | ||
254 | void createChainReference(char *chain) { | |
255 | char str[STRING_SIZE]; | |
256 | sprintf(str, "/sbin/iptables -I INPUT %s -j %sINPUT", "14", chain); | |
257 | executeCommand(str); | |
258 | sprintf(str, "/sbin/iptables -I FORWARD %s -j %sFORWARD", "12", chain); | |
259 | executeCommand(str); | |
260 | } | |
261 | ||
262 | void createChain(char *chain) { | |
263 | char str[STRING_SIZE]; | |
264 | sprintf(str, "/sbin/iptables -N %sINPUT", chain); | |
265 | executeCommand(str); | |
266 | sprintf(str, "/sbin/iptables -N %sFORWARD", chain); | |
267 | executeCommand(str); | |
268 | } | |
269 | ||
270 | void createAllChains(void) { | |
858d8d90 MT |
271 | // create chain and chain references |
272 | if (!strcmp(enableorange, "on")) { | |
273 | if (strlen(orangeif)) { | |
274 | createChain(OVPNORANGE); | |
275 | createChainReference(OVPNORANGE); | |
276 | } else { | |
277 | fprintf(stderr, "OpenVPN enabled on orange but no orange interface found\n"); | |
278 | //exit(1); | |
6e13d0a5 | 279 | } |
858d8d90 MT |
280 | } |
281 | ||
282 | if (!strcmp(enableblue, "on")) { | |
283 | if (strlen(blueif)) { | |
284 | createChain(OVPNBLUE); | |
285 | createChainReference(OVPNBLUE); | |
286 | } else { | |
287 | fprintf(stderr, "OpenVPN enabled on blue but no blue interface found\n"); | |
288 | //exit(1); | |
6e13d0a5 | 289 | } |
858d8d90 MT |
290 | } |
291 | ||
292 | if (!strcmp(enablered, "on")) { | |
293 | if (strlen(redif)) { | |
294 | createChain(OVPNRED); | |
295 | createChainReference(OVPNRED); | |
296 | } else { | |
297 | fprintf(stderr, "OpenVPN enabled on red but no red interface found\n"); | |
298 | //exit(1); | |
6e13d0a5 MT |
299 | } |
300 | } | |
301 | } | |
302 | ||
303 | void setFirewallRules(void) { | |
304 | char protocol[STRING_SIZE] = ""; | |
305 | char dport[STRING_SIZE] = ""; | |
306 | char dovpnip[STRING_SIZE] = ""; | |
307 | ||
6e13d0a5 MT |
308 | kv = initkeyvalues(); |
309 | if (!readkeyvalues(kv, CONFIG_ROOT "/ovpn/settings")) | |
310 | { | |
311 | fprintf(stderr, "Cannot read ovpn settings\n"); | |
312 | exit(1); | |
313 | } | |
314 | ||
315 | /* we got one device, so lets proceed further */ | |
316 | if (!findkey(kv, "DDEST_PORT", dport)){ | |
317 | fprintf(stderr, "Cannot read DDEST_PORT\n"); | |
318 | exit(1); | |
319 | } | |
320 | ||
321 | if (!findkey(kv, "DPROTOCOL", protocol)){ | |
322 | fprintf(stderr, "Cannot read DPROTOCOL\n"); | |
323 | exit(1); | |
324 | } | |
325 | ||
326 | if (!findkey(kv, "VPN_IP", dovpnip)){ | |
327 | fprintf(stderr, "Cannot read VPN_IP\n"); | |
328 | // exit(1); step further as we don't need an ip | |
329 | } | |
330 | freekeyvalues(kv); | |
331 | ||
6925b8ef AF |
332 | // read connection configuration |
333 | connection *conn = getConnections(); | |
334 | ||
07081137 MT |
335 | // Flush all chains. |
336 | flushChain(OVPNRED); | |
337 | flushChain(OVPNBLUE); | |
338 | flushChain(OVPNORANGE); | |
339 | ||
6e13d0a5 MT |
340 | // set firewall rules |
341 | if (!strcmp(enablered, "on") && strlen(redif)) | |
342 | setChainRules(OVPNRED, redif, protocol, dport); | |
343 | if (!strcmp(enableblue, "on") && strlen(blueif)) | |
344 | setChainRules(OVPNBLUE, blueif, protocol, dport); | |
345 | if (!strcmp(enableorange, "on") && strlen(orangeif)) | |
346 | setChainRules(OVPNORANGE, orangeif, protocol, dport); | |
6925b8ef AF |
347 | |
348 | // set firewall rules for n2n connections | |
07081137 | 349 | char *port; |
6925b8ef AF |
350 | while (conn) { |
351 | sprintf(port, "%d", conn->port); | |
07081137 | 352 | setChainRules(OVPNRED, redif, conn->proto, port); |
6925b8ef AF |
353 | conn = conn->next; |
354 | } | |
6e13d0a5 MT |
355 | } |
356 | ||
357 | void stopDaemon(void) { | |
358 | char command[STRING_SIZE]; | |
359 | ||
2bcff894 MT |
360 | int pid = readPidFile("/var/run/openvpn.pid"); |
361 | if (pid == NULL) { | |
362 | exit(1); | |
363 | } | |
364 | ||
365 | fprintf(stderr, "Killing PID %d.\n", pid); | |
366 | kill(pid, SIGTERM); | |
367 | ||
6e13d0a5 MT |
368 | snprintf(command, STRING_SIZE - 1, "/bin/rm -f /var/run/openvpn.pid"); |
369 | executeCommand(command); | |
370 | } | |
371 | ||
372 | void startDaemon(void) { | |
373 | char command[STRING_SIZE]; | |
374 | ||
375 | if (!((strcmp(enablered, "on")==0) || (strcmp(enableblue, "on")==0) || (strcmp(enableorange, "on")==0))){ | |
376 | fprintf(stderr, "OpenVPN is not enabled on any interface\n"); | |
377 | exit(1); | |
378 | } else { | |
7d3af7f7 MT |
379 | snprintf(command, STRING_SIZE-1, "/sbin/modprobe tun"); |
380 | executeCommand(command); | |
072cd997 | 381 | snprintf(command, STRING_SIZE-1, "/usr/sbin/openvpn --config /var/ipfire/ovpn/server.conf"); |
6e13d0a5 MT |
382 | executeCommand(command); |
383 | } | |
384 | } | |
385 | ||
6925b8ef AF |
386 | void startNet2Net(char *name) { |
387 | connection *conn = NULL; | |
388 | connection *conn_iter; | |
389 | ||
390 | conn_iter = getConnections(); | |
391 | ||
392 | while (conn_iter) { | |
393 | if (strcmp(conn_iter->name, name) == 0) { | |
394 | conn = conn_iter; | |
395 | break; | |
396 | } | |
397 | conn_iter = conn_iter->next; | |
398 | } | |
399 | ||
400 | if (conn == NULL) { | |
401 | fprintf(stderr, "Connection not found.\n"); | |
402 | exit(1); | |
403 | } | |
404 | ||
39877197 MT |
405 | char configfile[STRING_SIZE]; |
406 | snprintf(configfile, STRING_SIZE - 1, CONFIG_ROOT "/ovpn/n2nconf/%s/%s.conf", | |
407 | conn->name, conn->name); | |
408 | ||
409 | FILE *fp = fopen(configfile, "r"); | |
410 | if (fp == NULL) { | |
411 | fprintf(stderr, "Could not find configuration file for connection '%s' at '%s'.\n", | |
412 | conn->name, configfile); | |
413 | exit(2); | |
414 | } | |
415 | fclose(fp); | |
416 | ||
07081137 MT |
417 | // Make sure all firewall rules are up to date. |
418 | setFirewallRules(); | |
419 | ||
6925b8ef | 420 | char command[STRING_SIZE]; |
39877197 | 421 | sprintf(command, "/usr/sbin/openvpn --config %s", configfile); |
6925b8ef AF |
422 | executeCommand(command); |
423 | } | |
424 | ||
2bcff894 MT |
425 | int readPidFile(const char *pidfile) { |
426 | FILE *fp = fopen(pidfile, "r"); | |
427 | if (fp == NULL) { | |
428 | fprintf(stderr, "PID file not found: '%s'\n", pidfile); | |
429 | exit(1); | |
430 | } | |
431 | ||
432 | int pid = NULL; | |
433 | fscanf(fp, "%d", &pid); | |
434 | fclose(fp); | |
435 | ||
436 | return pid; | |
437 | } | |
438 | ||
39877197 MT |
439 | void killNet2Net(char *name) { |
440 | connection *conn = NULL; | |
441 | connection *conn_iter; | |
442 | ||
443 | conn_iter = getConnections(); | |
444 | ||
445 | while (conn_iter) { | |
446 | if (strcmp(conn_iter->name, name) == 0) { | |
447 | conn = conn_iter; | |
448 | break; | |
449 | } | |
450 | conn_iter = conn_iter->next; | |
451 | } | |
452 | ||
453 | if (conn == NULL) { | |
454 | fprintf(stderr, "Connection not found.\n"); | |
455 | exit(1); | |
456 | } | |
457 | ||
458 | char pidfile[STRING_SIZE]; | |
459 | snprintf(&pidfile, STRING_SIZE - 1, "/var/run/%sn2n.pid", conn->name); | |
460 | ||
2bcff894 MT |
461 | int pid = readPidFile(pidfile); |
462 | if (pid == NULL) { | |
39877197 MT |
463 | exit(1); |
464 | } | |
465 | ||
39877197 MT |
466 | fprintf(stderr, "Killing PID %d.\n", pid); |
467 | kill(pid, SIGTERM); | |
468 | ||
469 | exit(0); | |
6925b8ef AF |
470 | } |
471 | ||
6e13d0a5 MT |
472 | void displayopenvpn(void) { |
473 | char command[STRING_SIZE]; | |
474 | ||
475 | snprintf(command, STRING_SIZE - 1, "/bin/killall -sSIGUSR2 openvpn"); | |
476 | executeCommand(command); | |
477 | } | |
478 | ||
479 | int main(int argc, char *argv[]) { | |
480 | if (!(initsetuid())) | |
481 | exit(1); | |
482 | if(argc < 2) | |
483 | usage(); | |
6925b8ef AF |
484 | |
485 | if(argc == 3) { | |
486 | if( (strcmp(argv[1], "-sn2n") == 0) || (strcmp(argv[1], "--start-net-2-net") == 0) ) { | |
487 | startNet2Net(argv[2]); | |
488 | return 0; | |
489 | } | |
490 | else if( (strcmp(argv[1], "-kn2n") == 0) || (strcmp(argv[1], "--kill-net-2-net") == 0) ) { | |
491 | killNet2Net(argv[2]); | |
492 | return 0; | |
493 | } else { | |
494 | usage(); | |
495 | return 1; | |
496 | } | |
497 | } | |
498 | else if(argc == 2) { | |
6e13d0a5 MT |
499 | if( (strcmp(argv[1], "-k") == 0) || (strcmp(argv[1], "--kill") == 0) ) { |
500 | stopDaemon(); | |
501 | return 0; | |
502 | } | |
503 | else if( (strcmp(argv[1], "-d") == 0) || (strcmp(argv[1], "--display") == 0) ) { | |
504 | displayopenvpn(); | |
505 | return 0; | |
506 | } | |
507 | else if( (strcmp(argv[1], "-dcr") == 0) || (strcmp(argv[1], "--delete-chains-and-rules") == 0) ) { | |
508 | deleteAllChains(); | |
509 | return 0; | |
510 | } | |
511 | else { | |
512 | ovpnInit(); | |
513 | ||
514 | if( (strcmp(argv[1], "-s") == 0) || (strcmp(argv[1], "--start") == 0) ) { | |
515 | deleteAllChains(); | |
516 | createAllChains(); | |
517 | setFirewallRules(); | |
518 | startDaemon(); | |
519 | return 0; | |
520 | } | |
521 | else if( (strcmp(argv[1], "-sdo") == 0) || (strcmp(argv[1], "--start-daemon-only") == 0) ) { | |
522 | startDaemon(); | |
523 | return 0; | |
524 | } | |
525 | else if( (strcmp(argv[1], "-r") == 0) || (strcmp(argv[1], "--restart") == 0) ) { | |
526 | stopDaemon(); | |
527 | deleteAllChains(); | |
528 | createAllChains(); | |
529 | setFirewallRules(); | |
530 | startDaemon(); | |
531 | return 0; | |
532 | } | |
533 | else if( (strcmp(argv[1], "-fwr") == 0) || (strcmp(argv[1], "--firewall-rules") == 0) ) { | |
534 | deleteAllChains(); | |
535 | createAllChains(); | |
536 | setFirewallRules(); | |
537 | return 0; | |
538 | } | |
539 | else if( (strcmp(argv[1], "-ccr") == 0) || (strcmp(argv[1], "--create-chains-and-rules") == 0) ) { | |
540 | createAllChains(); | |
541 | setFirewallRules(); | |
542 | return 0; | |
543 | } | |
544 | else { | |
545 | usage(); | |
546 | return 0; | |
547 | } | |
548 | } | |
549 | } | |
550 | else { | |
551 | usage(); | |
552 | return 0; | |
553 | } | |
554 | return 0; | |
555 | } | |
556 |