]>
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> | |
3d1fbbb0 MT |
7 | #include <arpa/inet.h> |
8 | #include <netinet/in.h> | |
6e13d0a5 MT |
9 | #include <fcntl.h> |
10 | #include "setuid.h" | |
11 | #include "libsmooth.h" | |
12 | ||
c894a342 | 13 | #define noovpndebug |
6e13d0a5 MT |
14 | |
15 | // global vars | |
16 | struct keyvalue *kv = NULL; | |
17 | FILE *ifacefile = NULL; | |
18 | ||
19 | char redif[STRING_SIZE]; | |
20 | char blueif[STRING_SIZE]; | |
21 | char orangeif[STRING_SIZE]; | |
22 | char enablered[STRING_SIZE] = "off"; | |
23 | char enableblue[STRING_SIZE] = "off"; | |
24 | char enableorange[STRING_SIZE] = "off"; | |
25 | ||
26 | // consts | |
27 | char OVPNRED[STRING_SIZE] = "OVPN"; | |
28 | char OVPNBLUE[STRING_SIZE] = "OVPN_BLUE_"; | |
29 | char OVPNORANGE[STRING_SIZE] = "OVPN_ORANGE_"; | |
c31f18b6 | 30 | char OVPNBLOCK[STRING_SIZE] = "OVPNBLOCK"; |
3d1fbbb0 MT |
31 | char OVPNNAT[STRING_SIZE] = "OVPNNAT"; |
32 | char WRAPPERVERSION[STRING_SIZE] = "ipfire-2.2.3"; | |
6925b8ef AF |
33 | |
34 | struct connection_struct { | |
35 | char name[STRING_SIZE]; | |
91a0a221 | 36 | char type[STRING_SIZE]; |
6925b8ef | 37 | char proto[STRING_SIZE]; |
99b01b84 | 38 | char status[STRING_SIZE]; |
3d1fbbb0 MT |
39 | char local_subnet[STRING_SIZE]; |
40 | char transfer_subnet[STRING_SIZE]; | |
41 | char role[STRING_SIZE]; | |
6925b8ef AF |
42 | int port; |
43 | struct connection_struct *next; | |
44 | }; | |
45 | ||
46 | typedef struct connection_struct connection; | |
6e13d0a5 MT |
47 | |
48 | void exithandler(void) | |
49 | { | |
50 | if(kv) | |
51 | freekeyvalues(kv); | |
52 | if (ifacefile) | |
53 | fclose(ifacefile); | |
54 | } | |
55 | ||
56 | void usage(void) | |
57 | { | |
58 | #ifdef ovpndebug | |
07081137 | 59 | printf("Wrapper for OpenVPN %s-debug\n", WRAPPERVERSION); |
6e13d0a5 | 60 | #else |
07081137 | 61 | printf("Wrapper for OpenVPN %s\n", WRAPPERVERSION); |
6e13d0a5 MT |
62 | #endif |
63 | printf("openvpnctrl <option>\n"); | |
64 | printf(" Valid options are:\n"); | |
65 | printf(" -s --start\n"); | |
66 | printf(" starts OpenVPN (implicitly creates chains and firewall rules)\n"); | |
67 | printf(" -k --kill\n"); | |
68 | printf(" kills/stops OpenVPN\n"); | |
69 | printf(" -r --restart\n"); | |
70 | printf(" restarts OpenVPN (implicitly creates chains and firewall rules)\n"); | |
64f0c354 MT |
71 | printf(" -sn2n --start-net-2-net\n"); |
72 | printf(" starts all net2net connections\n"); | |
73 | printf(" you may pass a connection name to the switch to only start a specific one\n"); | |
74 | printf(" -kn2n --kill-net-2-net\n"); | |
75 | printf(" kills all net2net connections\n"); | |
76 | printf(" you may pass a connection name to the switch to only start a specific one\n"); | |
6e13d0a5 MT |
77 | printf(" -d --display\n"); |
78 | printf(" displays OpenVPN status to syslog\n"); | |
79 | printf(" -fwr --firewall-rules\n"); | |
80 | printf(" removes current OpenVPN chains and rules and resets them according to the config\n"); | |
81 | printf(" -sdo --start-daemon-only\n"); | |
afabe9f7 | 82 | printf(" starts OpenVPN daemon only\n"); |
6e13d0a5 MT |
83 | printf(" -ccr --create-chains-and-rules\n"); |
84 | printf(" creates chains and rules for OpenVPN\n"); | |
85 | printf(" -dcr --delete-chains-and-rules\n"); | |
86 | printf(" removes all chains for OpenVPN\n"); | |
87 | exit(1); | |
88 | } | |
89 | ||
6925b8ef AF |
90 | connection *getConnections() { |
91 | FILE *fp = NULL; | |
92 | ||
93 | if (!(fp = fopen(CONFIG_ROOT "/ovpn/ovpnconfig", "r"))) { | |
94 | fprintf(stderr, "Could not open openvpn n2n configuration file.\n"); | |
95 | exit(1); | |
96 | } | |
97 | ||
98 | char line[STRING_SIZE] = ""; | |
d4f2fb97 MT |
99 | char result[STRING_SIZE] = ""; |
100 | char *resultptr; | |
6925b8ef AF |
101 | int count; |
102 | connection *conn_first = NULL; | |
103 | connection *conn_last = NULL; | |
104 | connection *conn_curr; | |
105 | ||
106 | while ((fgets(line, STRING_SIZE, fp) != NULL)) { | |
107 | if (line[strlen(line) - 1] == '\n') | |
108 | line[strlen(line) - 1] = '\0'; | |
109 | ||
110 | conn_curr = (connection *)malloc(sizeof(connection)); | |
111 | memset(conn_curr, 0, sizeof(connection)); | |
112 | ||
113 | if (conn_first == NULL) { | |
114 | conn_first = conn_curr; | |
115 | } else { | |
116 | conn_last->next = conn_curr; | |
117 | } | |
118 | conn_last = conn_curr; | |
119 | ||
120 | count = 0; | |
d4f2fb97 MT |
121 | char *lineptr = &line; |
122 | while (1) { | |
123 | if (*lineptr == NULL) | |
124 | break; | |
125 | ||
126 | resultptr = result; | |
127 | while (*lineptr != NULL) { | |
128 | if (*lineptr == ',') { | |
129 | lineptr++; | |
130 | break; | |
131 | } | |
132 | *resultptr++ = *lineptr++; | |
133 | } | |
134 | *resultptr = '\0'; | |
135 | ||
99b01b84 MT |
136 | if (count == 1) { |
137 | strcpy(conn_curr->status, result); | |
138 | } else if (count == 2) { | |
6925b8ef | 139 | strcpy(conn_curr->name, result); |
91a0a221 MT |
140 | } else if (count == 4) { |
141 | strcpy(conn_curr->type, result); | |
3d1fbbb0 MT |
142 | } else if (count == 7) { |
143 | strcpy(conn_curr->role, result); | |
144 | } else if (count == 9) { | |
145 | strcpy(conn_curr->local_subnet, result); | |
146 | } else if (count == 28) { | |
147 | strcpy(conn_curr->transfer_subnet, result); | |
d4f2fb97 | 148 | } else if (count == 29) { |
6925b8ef | 149 | strcpy(conn_curr->proto, result); |
d4f2fb97 | 150 | } else if (count == 30) { |
6925b8ef AF |
151 | conn_curr->port = atoi(result); |
152 | } | |
153 | ||
6925b8ef AF |
154 | count++; |
155 | } | |
156 | } | |
157 | ||
158 | fclose(fp); | |
159 | ||
160 | return conn_first; | |
161 | } | |
162 | ||
80ca8bd0 MT |
163 | int readPidFile(const char *pidfile) { |
164 | FILE *fp = fopen(pidfile, "r"); | |
165 | if (fp == NULL) { | |
80ca8bd0 MT |
166 | exit(1); |
167 | } | |
168 | ||
169 | int pid = 0; | |
170 | fscanf(fp, "%d", &pid); | |
171 | fclose(fp); | |
172 | ||
173 | return pid; | |
174 | } | |
175 | ||
6e13d0a5 | 176 | void ovpnInit(void) { |
6e13d0a5 MT |
177 | // Read OpenVPN configuration |
178 | kv = initkeyvalues(); | |
179 | if (!readkeyvalues(kv, CONFIG_ROOT "/ovpn/settings")) { | |
180 | fprintf(stderr, "Cannot read ovpn settings\n"); | |
181 | exit(1); | |
182 | } | |
183 | ||
184 | if (!findkey(kv, "ENABLED", enablered)) { | |
6e13d0a5 MT |
185 | exit(1); |
186 | } | |
187 | ||
188 | if (!findkey(kv, "ENABLED_BLUE", enableblue)){ | |
6e13d0a5 MT |
189 | exit(1); |
190 | } | |
191 | ||
192 | if (!findkey(kv, "ENABLED_ORANGE", enableorange)){ | |
6e13d0a5 MT |
193 | exit(1); |
194 | } | |
195 | freekeyvalues(kv); | |
196 | ||
197 | // read interface settings | |
198 | ||
199 | // details for the red int | |
200 | memset(redif, 0, STRING_SIZE); | |
201 | if ((ifacefile = fopen(CONFIG_ROOT "/red/iface", "r"))) | |
202 | { | |
203 | if (fgets(redif, STRING_SIZE, ifacefile)) | |
204 | { | |
205 | if (redif[strlen(redif) - 1] == '\n') | |
206 | redif[strlen(redif) - 1] = '\0'; | |
207 | } | |
208 | fclose (ifacefile); | |
209 | ifacefile = NULL; | |
210 | ||
211 | if (!VALID_DEVICE(redif)) | |
212 | { | |
213 | memset(redif, 0, STRING_SIZE); | |
214 | } | |
215 | } | |
216 | ||
217 | kv=initkeyvalues(); | |
3ad23ee1 | 218 | if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")) { |
6e13d0a5 MT |
219 | fprintf(stderr, "Cannot read ethernet settings\n"); |
220 | exit(1); | |
221 | } | |
222 | ||
3ad23ee1 MT |
223 | if (strcmp(enableblue, "on") == 0) { |
224 | if (!findkey(kv, "BLUE_DEV", blueif)) { | |
6e13d0a5 MT |
225 | exit(1); |
226 | } | |
227 | } | |
3ad23ee1 MT |
228 | |
229 | if (strcmp(enableorange, "on") == 0) { | |
230 | if (!findkey(kv, "ORANGE_DEV", orangeif)) { | |
6e13d0a5 MT |
231 | exit(1); |
232 | } | |
3ad23ee1 | 233 | } |
6e13d0a5 MT |
234 | freekeyvalues(kv); |
235 | } | |
236 | ||
237 | void executeCommand(char *command) { | |
238 | #ifdef ovpndebug | |
239 | printf(strncat(command, "\n", 2)); | |
240 | #endif | |
241 | safe_system(strncat(command, " >/dev/null 2>&1", 17)); | |
242 | } | |
243 | ||
244 | void setChainRules(char *chain, char *interface, char *protocol, char *port) | |
245 | { | |
246 | char str[STRING_SIZE]; | |
07081137 | 247 | |
6e13d0a5 MT |
248 | sprintf(str, "/sbin/iptables -A %sINPUT -i %s -p %s --dport %s -j ACCEPT", chain, interface, protocol, port); |
249 | executeCommand(str); | |
6e13d0a5 MT |
250 | } |
251 | ||
252 | void flushChain(char *chain) { | |
253 | char str[STRING_SIZE]; | |
254 | ||
d2c4a3ca | 255 | sprintf(str, "/sbin/iptables -F %s", chain); |
6e13d0a5 | 256 | executeCommand(str); |
6e13d0a5 MT |
257 | } |
258 | ||
3d1fbbb0 MT |
259 | void flushChainNAT(char *chain) { |
260 | char str[STRING_SIZE]; | |
261 | ||
262 | sprintf(str, "/sbin/iptables -t nat -F %s", chain); | |
263 | executeCommand(str); | |
264 | } | |
265 | ||
d2c4a3ca MT |
266 | void flushChainINPUT(char *chain) { |
267 | char str[STRING_SIZE]; | |
268 | ||
269 | snprintf(str, STRING_SIZE, "%sINPUT", chain); | |
270 | flushChain(str); | |
271 | } | |
272 | ||
6e13d0a5 MT |
273 | void deleteChainReference(char *chain) { |
274 | char str[STRING_SIZE]; | |
275 | ||
276 | sprintf(str, "/sbin/iptables -D INPUT -j %sINPUT", chain); | |
277 | executeCommand(str); | |
6e13d0a5 MT |
278 | } |
279 | ||
280 | void deleteChain(char *chain) { | |
281 | char str[STRING_SIZE]; | |
282 | ||
283 | sprintf(str, "/sbin/iptables -X %sINPUT", chain); | |
284 | executeCommand(str); | |
6e13d0a5 MT |
285 | } |
286 | ||
287 | void deleteAllChains(void) { | |
288 | // not an elegant solution, but to avoid timing problems with undeleted chain references | |
289 | deleteChainReference(OVPNRED); | |
290 | deleteChainReference(OVPNBLUE); | |
291 | deleteChainReference(OVPNORANGE); | |
d2c4a3ca MT |
292 | flushChainINPUT(OVPNRED); |
293 | flushChainINPUT(OVPNBLUE); | |
294 | flushChainINPUT(OVPNORANGE); | |
6e13d0a5 MT |
295 | deleteChain(OVPNRED); |
296 | deleteChain(OVPNBLUE); | |
297 | deleteChain(OVPNORANGE); | |
2181b555 MT |
298 | |
299 | // Only flush chains that are created by the firewall | |
300 | flushChain(OVPNBLOCK); | |
301 | flushChainNAT(OVPNNAT); | |
6e13d0a5 MT |
302 | } |
303 | ||
304 | void createChainReference(char *chain) { | |
305 | char str[STRING_SIZE]; | |
306 | sprintf(str, "/sbin/iptables -I INPUT %s -j %sINPUT", "14", chain); | |
307 | executeCommand(str); | |
6e13d0a5 MT |
308 | } |
309 | ||
310 | void createChain(char *chain) { | |
311 | char str[STRING_SIZE]; | |
312 | sprintf(str, "/sbin/iptables -N %sINPUT", chain); | |
313 | executeCommand(str); | |
6e13d0a5 MT |
314 | } |
315 | ||
316 | void createAllChains(void) { | |
858d8d90 MT |
317 | // create chain and chain references |
318 | if (!strcmp(enableorange, "on")) { | |
3ad23ee1 | 319 | if (strlen(orangeif) > 0) { |
858d8d90 MT |
320 | createChain(OVPNORANGE); |
321 | createChainReference(OVPNORANGE); | |
322 | } else { | |
323 | fprintf(stderr, "OpenVPN enabled on orange but no orange interface found\n"); | |
324 | //exit(1); | |
6e13d0a5 | 325 | } |
858d8d90 MT |
326 | } |
327 | ||
328 | if (!strcmp(enableblue, "on")) { | |
3ad23ee1 | 329 | if (strlen(blueif) > 0) { |
858d8d90 MT |
330 | createChain(OVPNBLUE); |
331 | createChainReference(OVPNBLUE); | |
332 | } else { | |
333 | fprintf(stderr, "OpenVPN enabled on blue but no blue interface found\n"); | |
334 | //exit(1); | |
6e13d0a5 | 335 | } |
858d8d90 MT |
336 | } |
337 | ||
338 | if (!strcmp(enablered, "on")) { | |
3ad23ee1 | 339 | if (strlen(redif) > 0) { |
858d8d90 MT |
340 | createChain(OVPNRED); |
341 | createChainReference(OVPNRED); | |
342 | } else { | |
343 | fprintf(stderr, "OpenVPN enabled on red but no red interface found\n"); | |
344 | //exit(1); | |
6e13d0a5 MT |
345 | } |
346 | } | |
347 | } | |
348 | ||
3d1fbbb0 | 349 | char* calcTransferNetAddress(const connection* conn) { |
a19ff965 MT |
350 | char *subnetmask = strdup(conn->transfer_subnet); |
351 | char *address = strsep(&subnetmask, "/"); | |
3d1fbbb0 | 352 | |
cdbe3504 MT |
353 | if ((address == NULL) || (subnetmask == NULL)) { |
354 | goto ERROR; | |
355 | } | |
356 | ||
a19ff965 MT |
357 | in_addr_t _address = inet_addr(address); |
358 | in_addr_t _subnetmask = inet_addr(subnetmask); | |
359 | _address &= _subnetmask; | |
3d1fbbb0 | 360 | |
a19ff965 MT |
361 | if (strcmp(conn->role, "server") == 0) { |
362 | _address += 1 << 24; | |
363 | } else if (strcmp(conn->role, "client") == 0) { | |
364 | _address += 2 << 24; | |
3d1fbbb0 MT |
365 | } else { |
366 | goto ERROR; | |
367 | } | |
368 | ||
a19ff965 MT |
369 | struct in_addr address_info; |
370 | address_info.s_addr = _address; | |
371 | ||
372 | return inet_ntoa(address_info); | |
3d1fbbb0 MT |
373 | |
374 | ERROR: | |
a19ff965 MT |
375 | fprintf(stderr, "Could not determine transfer net address: %s\n", conn->name); |
376 | ||
3d1fbbb0 MT |
377 | free(address); |
378 | return NULL; | |
379 | } | |
380 | ||
381 | char* getLocalSubnetAddress(const connection* conn) { | |
382 | kv = initkeyvalues(); | |
383 | if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")) { | |
384 | fprintf(stderr, "Cannot read ethernet settings\n"); | |
385 | exit(1); | |
386 | } | |
387 | ||
388 | const char *zones[] = {"GREEN", "BLUE", "ORANGE", NULL}; | |
389 | char *zone = NULL; | |
390 | ||
391 | // Get net address of the local openvpn subnet. | |
392 | char *subnetmask = strdup(conn->local_subnet); | |
393 | char *address = strsep(&subnetmask, "/"); | |
394 | ||
395 | if ((address == NULL) || (subnetmask == NULL)) { | |
396 | goto ERROR; | |
397 | } | |
398 | ||
399 | in_addr_t _address = inet_addr(address); | |
400 | in_addr_t _subnetmask = inet_addr(subnetmask); | |
401 | ||
402 | in_addr_t _netaddr = (_address & _subnetmask); | |
403 | in_addr_t _broadcast = (_address | ~_subnetmask); | |
404 | ||
405 | char zone_address_key[STRING_SIZE]; | |
406 | char zone_address[STRING_SIZE]; | |
407 | in_addr_t zone_addr; | |
408 | ||
409 | int i = 0; | |
410 | while (zones[i]) { | |
411 | zone = zones[i++]; | |
412 | snprintf(zone_address_key, STRING_SIZE, "%s_ADDRESS", zone); | |
413 | ||
414 | if (!findkey(kv, zone_address_key, zone_address)) | |
415 | continue; | |
416 | ||
417 | zone_addr = inet_addr(zone_address); | |
418 | if ((zone_addr > _netaddr) && (zone_addr < _broadcast)) { | |
419 | freekeyvalues(kv); | |
420 | ||
421 | return strdup(zone_address); | |
422 | } | |
423 | } | |
424 | ||
425 | ERROR: | |
a19ff965 MT |
426 | fprintf(stderr, "Could not determine local subnet address: %s\n", conn->name); |
427 | ||
3d1fbbb0 MT |
428 | freekeyvalues(kv); |
429 | return NULL; | |
430 | } | |
431 | ||
6e13d0a5 MT |
432 | void setFirewallRules(void) { |
433 | char protocol[STRING_SIZE] = ""; | |
434 | char dport[STRING_SIZE] = ""; | |
435 | char dovpnip[STRING_SIZE] = ""; | |
436 | ||
6e13d0a5 MT |
437 | kv = initkeyvalues(); |
438 | if (!readkeyvalues(kv, CONFIG_ROOT "/ovpn/settings")) | |
439 | { | |
440 | fprintf(stderr, "Cannot read ovpn settings\n"); | |
441 | exit(1); | |
442 | } | |
443 | ||
444 | /* we got one device, so lets proceed further */ | |
445 | if (!findkey(kv, "DDEST_PORT", dport)){ | |
446 | fprintf(stderr, "Cannot read DDEST_PORT\n"); | |
447 | exit(1); | |
448 | } | |
449 | ||
450 | if (!findkey(kv, "DPROTOCOL", protocol)){ | |
451 | fprintf(stderr, "Cannot read DPROTOCOL\n"); | |
452 | exit(1); | |
453 | } | |
454 | ||
455 | if (!findkey(kv, "VPN_IP", dovpnip)){ | |
456 | fprintf(stderr, "Cannot read VPN_IP\n"); | |
6e13d0a5 MT |
457 | } |
458 | freekeyvalues(kv); | |
459 | ||
07081137 | 460 | // Flush all chains. |
d2c4a3ca MT |
461 | flushChainINPUT(OVPNRED); |
462 | flushChainINPUT(OVPNBLUE); | |
463 | flushChainINPUT(OVPNORANGE); | |
2181b555 | 464 | flushChain(OVPNBLOCK); |
3d1fbbb0 | 465 | flushChainNAT(OVPNNAT); |
07081137 | 466 | |
6e13d0a5 MT |
467 | // set firewall rules |
468 | if (!strcmp(enablered, "on") && strlen(redif)) | |
469 | setChainRules(OVPNRED, redif, protocol, dport); | |
470 | if (!strcmp(enableblue, "on") && strlen(blueif)) | |
471 | setChainRules(OVPNBLUE, blueif, protocol, dport); | |
472 | if (!strcmp(enableorange, "on") && strlen(orangeif)) | |
473 | setChainRules(OVPNORANGE, orangeif, protocol, dport); | |
6925b8ef | 474 | |
91a0a221 MT |
475 | // read connection configuration |
476 | connection *conn = getConnections(); | |
477 | ||
6925b8ef | 478 | // set firewall rules for n2n connections |
91a0a221 | 479 | char command[STRING_SIZE]; |
3d1fbbb0 MT |
480 | char *local_subnet_address = NULL; |
481 | char *transfer_subnet_address = NULL; | |
7d653d51 | 482 | while (conn != NULL) { |
91a0a221 MT |
483 | if (strcmp(conn->type, "net") == 0) { |
484 | sprintf(command, "/sbin/iptables -A %sINPUT -i %s -p %s --dport %d -j ACCEPT", | |
485 | OVPNRED, redif, conn->proto, conn->port); | |
486 | executeCommand(command); | |
3d1fbbb0 | 487 | |
c31f18b6 MT |
488 | /* Block all communication from the transfer nets. */ |
489 | snprintf(command, STRING_SIZE, "/sbin/iptables -A %s -s %s -j DROP", | |
490 | OVPNBLOCK, conn->transfer_subnet); | |
491 | executeCommand(command); | |
492 | ||
3d1fbbb0 MT |
493 | local_subnet_address = getLocalSubnetAddress(conn); |
494 | transfer_subnet_address = calcTransferNetAddress(conn); | |
495 | ||
cdbe3504 MT |
496 | if ((local_subnet_address) && (transfer_subnet_address)) { |
497 | snprintf(command, STRING_SIZE, "/sbin/iptables -t nat -A %s -s %s -j SNAT --to-source %s", | |
498 | OVPNNAT, transfer_subnet_address, local_subnet_address); | |
499 | executeCommand(command); | |
500 | } | |
91a0a221 MT |
501 | } |
502 | ||
6925b8ef AF |
503 | conn = conn->next; |
504 | } | |
6e13d0a5 MT |
505 | } |
506 | ||
507 | void stopDaemon(void) { | |
508 | char command[STRING_SIZE]; | |
509 | ||
2bcff894 | 510 | int pid = readPidFile("/var/run/openvpn.pid"); |
80ca8bd0 | 511 | if (!pid > 0) { |
2bcff894 MT |
512 | exit(1); |
513 | } | |
514 | ||
515 | fprintf(stderr, "Killing PID %d.\n", pid); | |
516 | kill(pid, SIGTERM); | |
517 | ||
6e13d0a5 MT |
518 | snprintf(command, STRING_SIZE - 1, "/bin/rm -f /var/run/openvpn.pid"); |
519 | executeCommand(command); | |
520 | } | |
521 | ||
522 | void startDaemon(void) { | |
523 | char command[STRING_SIZE]; | |
524 | ||
3ad23ee1 | 525 | if (!((strcmp(enablered, "on") == 0) || (strcmp(enableblue, "on") == 0) || (strcmp(enableorange, "on") == 0))) { |
6e13d0a5 MT |
526 | fprintf(stderr, "OpenVPN is not enabled on any interface\n"); |
527 | exit(1); | |
528 | } else { | |
7d3af7f7 MT |
529 | snprintf(command, STRING_SIZE-1, "/sbin/modprobe tun"); |
530 | executeCommand(command); | |
072cd997 | 531 | snprintf(command, STRING_SIZE-1, "/usr/sbin/openvpn --config /var/ipfire/ovpn/server.conf"); |
6e13d0a5 MT |
532 | executeCommand(command); |
533 | } | |
534 | } | |
535 | ||
99b01b84 | 536 | int startNet2Net(char *name) { |
6925b8ef AF |
537 | connection *conn = NULL; |
538 | connection *conn_iter; | |
539 | ||
540 | conn_iter = getConnections(); | |
541 | ||
542 | while (conn_iter) { | |
91a0a221 | 543 | if ((strcmp(conn_iter->type, "net") == 0) && (strcmp(conn_iter->name, name) == 0)) { |
6925b8ef AF |
544 | conn = conn_iter; |
545 | break; | |
546 | } | |
547 | conn_iter = conn_iter->next; | |
548 | } | |
549 | ||
550 | if (conn == NULL) { | |
551 | fprintf(stderr, "Connection not found.\n"); | |
99b01b84 MT |
552 | return 1; |
553 | } | |
554 | ||
555 | if (strcmp(conn->status, "on") != 0) { | |
556 | fprintf(stderr, "Connection '%s' is not enabled.\n", conn->name); | |
557 | return 1; | |
6925b8ef AF |
558 | } |
559 | ||
99b01b84 MT |
560 | fprintf(stderr, "Starting connection %s...\n", conn->name); |
561 | ||
39877197 MT |
562 | char configfile[STRING_SIZE]; |
563 | snprintf(configfile, STRING_SIZE - 1, CONFIG_ROOT "/ovpn/n2nconf/%s/%s.conf", | |
564 | conn->name, conn->name); | |
565 | ||
566 | FILE *fp = fopen(configfile, "r"); | |
567 | if (fp == NULL) { | |
568 | fprintf(stderr, "Could not find configuration file for connection '%s' at '%s'.\n", | |
569 | conn->name, configfile); | |
99b01b84 | 570 | return 2; |
39877197 MT |
571 | } |
572 | fclose(fp); | |
573 | ||
07081137 MT |
574 | // Make sure all firewall rules are up to date. |
575 | setFirewallRules(); | |
576 | ||
6925b8ef | 577 | char command[STRING_SIZE]; |
81a789d9 MT |
578 | snprintf(command, STRING_SIZE-1, "/sbin/modprobe tun"); |
579 | executeCommand(command); | |
580 | snprintf(command, STRING_SIZE-1, "/usr/sbin/openvpn --config %s", configfile); | |
6925b8ef | 581 | executeCommand(command); |
99b01b84 MT |
582 | |
583 | return 0; | |
6925b8ef AF |
584 | } |
585 | ||
99b01b84 | 586 | int killNet2Net(char *name) { |
39877197 MT |
587 | connection *conn = NULL; |
588 | connection *conn_iter; | |
589 | ||
590 | conn_iter = getConnections(); | |
591 | ||
592 | while (conn_iter) { | |
593 | if (strcmp(conn_iter->name, name) == 0) { | |
594 | conn = conn_iter; | |
595 | break; | |
596 | } | |
597 | conn_iter = conn_iter->next; | |
598 | } | |
599 | ||
600 | if (conn == NULL) { | |
601 | fprintf(stderr, "Connection not found.\n"); | |
99b01b84 | 602 | return 1; |
39877197 MT |
603 | } |
604 | ||
605 | char pidfile[STRING_SIZE]; | |
80ca8bd0 | 606 | snprintf(pidfile, STRING_SIZE - 1, "/var/run/%sn2n.pid", conn->name); |
39877197 | 607 | |
2bcff894 | 608 | int pid = readPidFile(pidfile); |
80ca8bd0 | 609 | if (!pid > 0) { |
99b01b84 MT |
610 | fprintf(stderr, "Could not read pid file of connection %s.", conn->name); |
611 | return 1; | |
39877197 MT |
612 | } |
613 | ||
99b01b84 | 614 | fprintf(stderr, "Killing connection %s (PID %d)...\n", conn->name, pid); |
39877197 MT |
615 | kill(pid, SIGTERM); |
616 | ||
d4c8b6be MT |
617 | char command[STRING_SIZE]; |
618 | snprintf(command, STRING_SIZE - 1, "/bin/rm -f %s", pidfile); | |
619 | executeCommand(command); | |
620 | ||
99b01b84 | 621 | return 0; |
6925b8ef AF |
622 | } |
623 | ||
64f0c354 | 624 | void startAllNet2Net() { |
99b01b84 MT |
625 | int exitcode = 0, _exitcode = 0; |
626 | ||
64f0c354 MT |
627 | connection *conn = getConnections(); |
628 | ||
629 | while(conn) { | |
99b01b84 MT |
630 | /* Skip all connections that are not of type "net" or disabled. */ |
631 | if ((strcmp(conn->type, "net") != 0) || (strcmp(conn->status, "on") != 0)) { | |
632 | conn = conn->next; | |
633 | continue; | |
634 | } | |
635 | ||
636 | _exitcode = startNet2Net(conn->name); | |
64f0c354 | 637 | conn = conn->next; |
99b01b84 MT |
638 | |
639 | if (_exitcode > exitcode) { | |
640 | exitcode = _exitcode; | |
641 | } | |
64f0c354 MT |
642 | } |
643 | ||
99b01b84 | 644 | exit(exitcode); |
64f0c354 MT |
645 | } |
646 | ||
647 | void killAllNet2Net() { | |
99b01b84 MT |
648 | int exitcode = 0, _exitcode = 0; |
649 | ||
64f0c354 MT |
650 | connection *conn = getConnections(); |
651 | ||
652 | while(conn) { | |
99b01b84 MT |
653 | /* Skip all connections that are not of type "net". */ |
654 | if (strcmp(conn->type, "net") != 0) { | |
655 | conn = conn->next; | |
656 | continue; | |
657 | } | |
658 | ||
659 | _exitcode = killNet2Net(conn->name); | |
64f0c354 | 660 | conn = conn->next; |
99b01b84 MT |
661 | |
662 | if (_exitcode > exitcode) { | |
663 | exitcode = _exitcode; | |
664 | } | |
64f0c354 MT |
665 | } |
666 | ||
99b01b84 | 667 | exit(exitcode); |
64f0c354 MT |
668 | } |
669 | ||
6e13d0a5 MT |
670 | void displayopenvpn(void) { |
671 | char command[STRING_SIZE]; | |
672 | ||
673 | snprintf(command, STRING_SIZE - 1, "/bin/killall -sSIGUSR2 openvpn"); | |
674 | executeCommand(command); | |
675 | } | |
676 | ||
677 | int main(int argc, char *argv[]) { | |
678 | if (!(initsetuid())) | |
679 | exit(1); | |
680 | if(argc < 2) | |
681 | usage(); | |
6925b8ef AF |
682 | |
683 | if(argc == 3) { | |
91a0a221 MT |
684 | ovpnInit(); |
685 | ||
6925b8ef AF |
686 | if( (strcmp(argv[1], "-sn2n") == 0) || (strcmp(argv[1], "--start-net-2-net") == 0) ) { |
687 | startNet2Net(argv[2]); | |
688 | return 0; | |
689 | } | |
690 | else if( (strcmp(argv[1], "-kn2n") == 0) || (strcmp(argv[1], "--kill-net-2-net") == 0) ) { | |
691 | killNet2Net(argv[2]); | |
692 | return 0; | |
693 | } else { | |
694 | usage(); | |
695 | return 1; | |
696 | } | |
697 | } | |
698 | else if(argc == 2) { | |
6e13d0a5 MT |
699 | if( (strcmp(argv[1], "-k") == 0) || (strcmp(argv[1], "--kill") == 0) ) { |
700 | stopDaemon(); | |
701 | return 0; | |
702 | } | |
703 | else if( (strcmp(argv[1], "-d") == 0) || (strcmp(argv[1], "--display") == 0) ) { | |
704 | displayopenvpn(); | |
705 | return 0; | |
706 | } | |
707 | else if( (strcmp(argv[1], "-dcr") == 0) || (strcmp(argv[1], "--delete-chains-and-rules") == 0) ) { | |
708 | deleteAllChains(); | |
709 | return 0; | |
710 | } | |
711 | else { | |
712 | ovpnInit(); | |
713 | ||
714 | if( (strcmp(argv[1], "-s") == 0) || (strcmp(argv[1], "--start") == 0) ) { | |
715 | deleteAllChains(); | |
716 | createAllChains(); | |
717 | setFirewallRules(); | |
718 | startDaemon(); | |
719 | return 0; | |
720 | } | |
64f0c354 MT |
721 | else if( (strcmp(argv[1], "-sn2n") == 0) || (strcmp(argv[1], "--start-net-2-net") == 0) ) { |
722 | startAllNet2Net(); | |
723 | return 0; | |
724 | } | |
725 | else if( (strcmp(argv[1], "-kn2n") == 0) || (strcmp(argv[1], "--kill-net-2-net") == 0) ) { | |
726 | killAllNet2Net(); | |
727 | return 0; | |
728 | } | |
6e13d0a5 MT |
729 | else if( (strcmp(argv[1], "-sdo") == 0) || (strcmp(argv[1], "--start-daemon-only") == 0) ) { |
730 | startDaemon(); | |
731 | return 0; | |
732 | } | |
733 | else if( (strcmp(argv[1], "-r") == 0) || (strcmp(argv[1], "--restart") == 0) ) { | |
734 | stopDaemon(); | |
735 | deleteAllChains(); | |
736 | createAllChains(); | |
737 | setFirewallRules(); | |
738 | startDaemon(); | |
739 | return 0; | |
740 | } | |
741 | else if( (strcmp(argv[1], "-fwr") == 0) || (strcmp(argv[1], "--firewall-rules") == 0) ) { | |
742 | deleteAllChains(); | |
743 | createAllChains(); | |
744 | setFirewallRules(); | |
745 | return 0; | |
746 | } | |
747 | else if( (strcmp(argv[1], "-ccr") == 0) || (strcmp(argv[1], "--create-chains-and-rules") == 0) ) { | |
748 | createAllChains(); | |
749 | setFirewallRules(); | |
750 | return 0; | |
751 | } | |
752 | else { | |
753 | usage(); | |
754 | return 0; | |
755 | } | |
756 | } | |
757 | } | |
758 | else { | |
759 | usage(); | |
760 | return 0; | |
761 | } | |
762 | return 0; | |
763 | } | |
764 |