]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/misc-progs/ipsecctrl.c
3 * File originally from the Smoothwall project
4 * (c) 2001 Smoothwall Team
13 #include <sys/types.h>
19 This module is responsible for start stop of the vpn system.
21 1) it allows AH & ESP to get in from interface where a vpn is mounted
22 The NAT traversal is used on the udp 4500 port.
24 2) it starts the ipsec daemon
25 The RED interface is a problem because it can be up or down a startup.
26 Then, the state change and it must not affect other VPN mounted on
28 Unfortunatly, openswan 1 cannot do that correctly. It cannot use an
29 interface without restarting everything.
34 fprintf (stderr
, "Usage:\n");
35 fprintf (stderr
, "\tipsecctrl S [connectionkey]\n");
36 fprintf (stderr
, "\tipsecctrl D [connectionkey]\n");
37 fprintf (stderr
, "\tipsecctrl R\n");
38 fprintf (stderr
, "\tipsecctrl I\n");
39 fprintf (stderr
, "\t\tS : Start/Restart Connection\n");
40 fprintf (stderr
, "\t\tD : Stop Connection\n");
41 fprintf (stderr
, "\t\tR : Reload Certificates and Secrets\n");
42 fprintf (stderr
, "\t\tI : Print Statusinfo\n");
46 ACCEPT the ipsec protocol ah, esp & udp (for nat traversal) on the specified interface
48 void open_physical (char *interface
, int nat_traversal_port
) {
49 char str
[STRING_SIZE
];
52 // sprintf(str, "/sbin/iptables -A " phystable " -p 47 -i %s -j ACCEPT", interface);
55 // sprintf(str, "/sbin/iptables -A " phystable " -p 50 -i %s -j ACCEPT", interface);
58 // sprintf(str, "/sbin/iptables -A " phystable " -p 51 -i %s -j ACCEPT", interface);
62 sprintf(str
, "/sbin/iptables -D IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT >/dev/null 2>&1", interface
);
64 sprintf(str
, "/sbin/iptables -A IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT", interface
);
67 if (! nat_traversal_port
)
70 sprintf(str
, "/sbin/iptables -D IPSECINPUT -p udp -i %s --dport %i -j ACCEPT >/dev/null 2>&1", interface
, nat_traversal_port
);
72 sprintf(str
, "/sbin/iptables -A IPSECINPUT -p udp -i %s --dport %i -j ACCEPT", interface
, nat_traversal_port
);
76 void ipsec_norules() {
77 /* clear input rules */
78 safe_system("/sbin/iptables -F IPSECINPUT");
79 safe_system("/sbin/iptables -F IPSECFORWARD");
80 safe_system("/sbin/iptables -F IPSECOUTPUT");
85 return values from the vpn config file or false if not 'on'
87 int decode_line (char *s
,
98 if (s
[strlen(s
) - 1] == '\n')
99 s
[strlen(s
) - 1] = '\0';
101 char *result
= strsep(&s
, ",");
105 if ((count
== 1) && strcmp(result
, "on") != 0)
106 return 0; // a disabled line
114 result
= strsep(&s
, ",");
117 // check other syntax
121 if (strspn(*name
, LETTERS_NUMBERS
) != strlen(*name
)) {
122 fprintf(stderr
, "Bad connection name: %s\n", *name
);
126 if (! (strcmp(*type
, "host") == 0 || strcmp(*type
, "net") == 0)) {
127 fprintf(stderr
, "Bad connection type: %s\n", *type
);
131 if (! (strcmp(*interface
, "RED") == 0 || strcmp(*interface
, "GREEN") == 0 ||
132 strcmp(*interface
, "ORANGE") == 0 || strcmp(*interface
, "BLUE") == 0)) {
133 fprintf(stderr
, "Bad interface name: %s\n", *interface
);
136 //it's a valid & active line
141 issue ipsec commmands to turn on connection 'name'
143 void turn_connection_on (char *name
, char *type
) {
145 Rename the connection and run ipsec update and rename it back to readd
146 a deleted connection. Because ipsec update ignores connection that have
147 not changed since last load.
149 char command
[STRING_SIZE
];
150 memset(command
, 0, STRING_SIZE
);
151 snprintf(command
, STRING_SIZE
- 1,
152 "sed -i -e 's|^conn %s$|conn %s-renamed|g' /var/ipfire/vpn/ipsec.conf >/dev/null", name
, name
);
153 safe_system(command
);
155 // Down and delete IKEv2 Tunnel before ipsec update
156 snprintf(command
, STRING_SIZE
- 1,
157 "/usr/sbin/ipsec stroke down %s >/dev/null", name
);
158 safe_system(command
);
159 snprintf(command
, STRING_SIZE
- 1,
160 "/usr/sbin/ipsec stroke delete %s >/dev/null", name
);
161 safe_system(command
);
163 safe_system("/etc/rc.d/init.d/ipsec update >/dev/null");
167 // Back to original name
168 snprintf(command
, STRING_SIZE
- 1,
169 "sed -i -e 's|^conn %s-renamed$|conn %s|g' /var/ipfire/vpn/ipsec.conf >/dev/null", name
, name
);
170 safe_system(command
);
172 // Down and delete IKEv2 Tunnel before ipsec update
173 snprintf(command
, STRING_SIZE
- 1,
174 "/usr/sbin/ipsec stroke down %s-renamed >/dev/null", name
);
175 safe_system(command
);
176 snprintf(command
, STRING_SIZE
- 1,
177 "/usr/sbin/ipsec stroke delete %s-renamed >/dev/null", name
);
178 safe_system(command
);
180 safe_system("/etc/rc.d/init.d/ipsec update >/dev/null");
183 issue ipsec commmands to turn off connection 'name'
185 void turn_connection_off (char *name
) {
186 char command
[STRING_SIZE
];
187 memset(command
, 0, STRING_SIZE
);
188 snprintf(command
, STRING_SIZE
- 1,
189 "/usr/sbin/ipsec whack --delete --name %s >/dev/null", name
);
190 safe_system(command
);
191 snprintf(command
, STRING_SIZE
- 1,
192 "/usr/sbin/ipsec stroke down %s >/dev/null", name
);
193 safe_system(command
);
194 snprintf(command
, STRING_SIZE
- 1,
195 "/usr/sbin/ipsec stroke delete %s >/dev/null", name
);
196 safe_system(command
);
198 safe_system("/usr/sbin/ipsec whack --rereadall >/dev/null");
199 safe_system("/usr/sbin/ipsec stroke rereadall >/dev/null");
204 int main(int argc
, char *argv
[]) {
206 char configtype
[STRING_SIZE
];
207 char redtype
[STRING_SIZE
] = "";
208 struct keyvalue
*kv
= NULL
;
220 if (strcmp(argv
[1], "I") == 0) {
221 safe_system("/usr/sbin/ipsec whack --status");
222 safe_system("/usr/sbin/ipsec stroke status");
226 if (strcmp(argv
[1], "R") == 0) {
227 safe_system("/usr/sbin/ipsec whack --rereadall >/dev/null");
228 safe_system("/usr/sbin/ipsec stroke rereadall >/dev/null");
232 /* Get vpnwatch pid */
235 if ((argc
== 2) && (file
= fopen("/var/run/vpn-watch.pid", "r"))) {
236 safe_system("kill -9 $(cat /var/run/vpn-watch.pid)");
237 safe_system("unlink /var/run/vpn-watch.pid");
241 /* FIXME: workaround for pclose() issue - still no real idea why
242 * this is happening */
243 signal(SIGCHLD
, SIG_DFL
);
245 /* handle operations that doesn't need start the ipsec system */
247 if (strcmp(argv
[1], "D") == 0) {
248 /* Only shutdown pluto if it really is running */
250 if (file
= fopen("/var/run/pluto.pid", "r")) {
251 safe_system("/etc/rc.d/init.d/ipsec stop 2> /dev/null >/dev/null");
260 /* read vpn config */
262 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
264 fprintf(stderr
, "Cannot read vpn settings\n");
268 /* check is the vpn system is enabled */
271 findkey(kv
, "ENABLED", s
);
273 if (strcmp (s
, "on") != 0)
277 /* read interface settings */
279 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
281 fprintf(stderr
, "Cannot read ethernet settings\n");
284 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
286 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
289 findkey(kv
, "RED_TYPE", redtype
);
292 /* Loop through the config file to find physical interface that will accept IPSEC */
293 int enable_red
=0; // states 0: not used
294 int enable_green
=0; // 1: error condition
295 int enable_orange
=0; // 2: good
297 char if_red
[STRING_SIZE
] = "";
298 char if_green
[STRING_SIZE
] = "";
299 char if_orange
[STRING_SIZE
] = "";
300 char if_blue
[STRING_SIZE
] = "";
303 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
304 fprintf(stderr
, "Couldn't open vpn settings file");
307 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
312 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
314 /* search interface */
315 if (!enable_red
&& strcmp (interface
, "RED") == 0) {
316 // when RED is up, find interface name in special file
317 FILE *ifacefile
= NULL
;
318 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
319 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
320 if (if_red
[strlen(if_red
) - 1] == '\n')
321 if_red
[strlen(if_red
) - 1] = '\0';
325 if (VALID_DEVICE(if_red
))
326 enable_red
+=2; // present and running
330 if (!enable_green
&& strcmp (interface
, "GREEN") == 0) {
332 findkey(kv
, "GREEN_DEV", if_green
);
333 if (VALID_DEVICE(if_green
))
336 fprintf(stderr
, "IPSec enabled on green but green interface is invalid or not found\n");
339 if (!enable_orange
&& strcmp (interface
, "ORANGE") == 0) {
341 findkey(kv
, "ORANGE_DEV", if_orange
);
342 if (VALID_DEVICE(if_orange
))
345 fprintf(stderr
, "IPSec enabled on orange but orange interface is invalid or not found\n");
348 if (!enable_blue
&& strcmp (interface
, "BLUE") == 0) {
350 findkey(kv
, "BLUE_DEV", if_blue
);
351 if (VALID_DEVICE(if_blue
))
354 fprintf(stderr
, "IPSec enabled on blue but blue interface is invalid or not found\n");
361 // do nothing if something is in error condition
362 if ((enable_red
==1) || (enable_green
==1) || (enable_orange
==1) || (enable_blue
==1) )
365 // exit if nothing to do
366 if ( (enable_red
+enable_green
+enable_orange
+enable_blue
) == 0 )
370 // todo: read a nat_t indicator to allow or not openning UDP/4500
372 open_physical(if_red
, 4500);
375 open_physical(if_green
, 4500);
377 if (enable_orange
==2)
378 open_physical(if_orange
, 4500);
381 open_physical(if_blue
, 4500);
384 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
385 safe_system("/etc/rc.d/init.d/ipsec restart >/dev/null");
386 safe_system("/usr/local/bin/vpn-watch &");
390 // it is a selective start or stop
391 // second param is only a number 'key'
392 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
393 fprintf(stderr
, "Bad arg\n");
398 // search the vpn pointed by 'key'
399 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
400 fprintf(stderr
, "Couldn't open vpn settings file");
403 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
408 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
411 // start/stop a vpn if belonging to specified interface
412 if (strcmp(argv
[1], interface
) == 0 ) {
413 if (strcmp(argv
[2], "0")==0)
414 turn_connection_off (name
);
416 turn_connection_on (name
, type
);
419 // is it the 'key' requested ?
420 if (strcmp(argv
[2], key
) != 0)
422 // Start or Delete this Connection
423 if (strcmp(argv
[1], "S") == 0)
424 turn_connection_on (name
, type
);
426 if (strcmp(argv
[1], "D") == 0)
427 turn_connection_off (name
);
429 fprintf(stderr
, "Bad command\n");