]>
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);
61 sprintf(str
, "/sbin/iptables -A IPSECINPUT -p udp -i %s --sport 500 --dport 500 -j ACCEPT", interface
);
64 if (! nat_traversal_port
)
67 sprintf(str
, "/sbin/iptables -A IPSECINPUT -p udp -i %s --dport %i -j ACCEPT", interface
, nat_traversal_port
);
71 void ipsec_norules() {
72 /* clear input rules */
73 safe_system("/sbin/iptables -F IPSECINPUT");
74 safe_system("/sbin/iptables -F IPSECFORWARD");
75 safe_system("/sbin/iptables -F IPSECOUTPUT");
80 return values from the vpn config file or false if not 'on'
82 int decode_line (char *s
,
93 if (s
[strlen(s
) - 1] == '\n')
94 s
[strlen(s
) - 1] = '\0';
96 char *result
= strsep(&s
, ",");
100 if ((count
== 1) && strcmp(result
, "on") != 0)
101 return 0; // a disabled line
109 result
= strsep(&s
, ",");
112 // check other syntax
116 if (strspn(*name
, LETTERS_NUMBERS
) != strlen(*name
)) {
117 fprintf(stderr
, "Bad connection name: %s\n", *name
);
121 if (! (strcmp(*type
, "host") == 0 || strcmp(*type
, "net") == 0)) {
122 fprintf(stderr
, "Bad connection type: %s\n", *type
);
126 if (! (strcmp(*interface
, "RED") == 0 || strcmp(*interface
, "GREEN") == 0 ||
127 strcmp(*interface
, "ORANGE") == 0 || strcmp(*interface
, "BLUE") == 0)) {
128 fprintf(stderr
, "Bad interface name: %s\n", *interface
);
131 //it's a valid & active line
136 issue ipsec commmands to turn on connection 'name'
138 void turn_connection_on (char *name
, char *type
) {
139 char command
[STRING_SIZE
];
141 safe_system("/usr/sbin/ipsec whack --rereadsecrets >/dev/null");
142 memset(command
, 0, STRING_SIZE
);
143 snprintf(command
, STRING_SIZE
- 1,
144 "/usr/sbin/ipsec auto --replace %s >/dev/null", name
);
145 safe_system(command
);
146 if (strcmp(type
, "net") == 0) {
147 memset(command
, 0, STRING_SIZE
);
148 snprintf(command
, STRING_SIZE
- 1,
149 "/usr/sbin/ipsec whack --asynchronous --name %s --initiate >/dev/null", name
);
150 safe_system(command
);
154 issue ipsec commmands to turn off connection 'name'
156 void turn_connection_off (char *name
) {
157 char command
[STRING_SIZE
];
159 memset(command
, 0, STRING_SIZE
);
160 snprintf(command
, STRING_SIZE
- 1,
161 "/usr/sbin/ipsec whack --name %s --terminate >/dev/null", name
);
162 safe_system(command
);
163 memset(command
, 0, STRING_SIZE
);
164 snprintf(command
, STRING_SIZE
- 1,
165 "/usr/sbin/ipsec whack --delete --name %s >/dev/null", name
);
166 safe_system(command
);
167 safe_system("/usr/sbin/ipsec whack --rereadsecrets >/dev/null");
171 int main(int argc
, char *argv
[]) {
173 char configtype
[STRING_SIZE
];
174 char redtype
[STRING_SIZE
] = "";
175 struct keyvalue
*kv
= NULL
;
186 /* Get vpnwatch pid */
188 if ( (argc
== 2) && (file
= fopen("/var/run/vpn-watch.pid", "r"))) {
189 safe_system("kill -9 $(cat /var/run/vpn-watch.pid)");
190 safe_system("unlink /var/run/vpn-watch.pid");
194 /* FIXME: workaround for pclose() issue - still no real idea why
195 * this is happening */
196 signal(SIGCHLD
, SIG_DFL
);
198 /* handle operations that doesn't need start the ipsec system */
200 if (strcmp(argv
[1], "D") == 0) {
202 /* Only shutdown pluto if it really is running */
204 if (file
= fopen("/var/run/pluto.pid", "r")) {
205 safe_system("/etc/rc.d/init.d/ipsec stop 2> /dev/null >/dev/null");
211 if (strcmp(argv
[1], "R") == 0) {
212 safe_system("/usr/sbin/ipsec whack --rereadall");
216 if (strcmp(argv
[1], "I") == 0) {
217 safe_system("/usr/sbin/ipsec whack --status");
223 /* clear iptables vpn rules */
226 /* read vpn config */
228 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
230 fprintf(stderr
, "Cannot read vpn settings\n");
234 /* check is the vpn system is enabled */
237 findkey(kv
, "ENABLED", s
);
239 if (strcmp (s
, "on") != 0)
243 /* read interface settings */
245 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
247 fprintf(stderr
, "Cannot read ethernet settings\n");
250 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
252 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
255 findkey(kv
, "RED_TYPE", redtype
);
258 /* Loop through the config file to find physical interface that will accept IPSEC */
259 int enable_red
=0; // states 0: not used
260 int enable_green
=0; // 1: error condition
261 int enable_orange
=0; // 2: good
263 char if_red
[STRING_SIZE
] = "";
264 char if_green
[STRING_SIZE
] = "";
265 char if_orange
[STRING_SIZE
] = "";
266 char if_blue
[STRING_SIZE
] = "";
269 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
270 fprintf(stderr
, "Couldn't open vpn settings file");
273 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
278 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
280 /* search interface */
281 if (!enable_red
&& strcmp (interface
, "RED") == 0) {
282 // when RED is up, find interface name in special file
283 FILE *ifacefile
= NULL
;
284 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
285 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
286 if (if_red
[strlen(if_red
) - 1] == '\n')
287 if_red
[strlen(if_red
) - 1] = '\0';
291 if (VALID_DEVICE(if_red
))
292 enable_red
+=2; // present and running
296 if (!enable_green
&& strcmp (interface
, "GREEN") == 0) {
298 findkey(kv
, "GREEN_DEV", if_green
);
299 if (VALID_DEVICE(if_green
))
302 fprintf(stderr
, "IPSec enabled on green but green interface is invalid or not found\n");
305 if (!enable_orange
&& strcmp (interface
, "ORANGE") == 0) {
307 findkey(kv
, "ORANGE_DEV", if_orange
);
308 if (VALID_DEVICE(if_orange
))
311 fprintf(stderr
, "IPSec enabled on orange but orange interface is invalid or not found\n");
314 if (!enable_blue
&& strcmp (interface
, "BLUE") == 0) {
316 findkey(kv
, "BLUE_DEV", if_blue
);
317 if (VALID_DEVICE(if_blue
))
320 fprintf(stderr
, "IPSec enabled on blue but blue interface is invalid or not found\n");
327 // do nothing if something is in error condition
328 if ((enable_red
==1) || (enable_green
==1) || (enable_orange
==1) || (enable_blue
==1) )
331 // exit if nothing to do
332 if ( (enable_red
+enable_green
+enable_orange
+enable_blue
) == 0 )
336 // todo: read a nat_t indicator to allow or not openning UDP/4500
338 open_physical(if_red
, 4500);
341 open_physical(if_green
, 4500);
343 if (enable_orange
==2)
344 open_physical(if_orange
, 4500);
347 open_physical(if_blue
, 4500);
350 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
351 safe_system("/etc/rc.d/init.d/ipsec restart >/dev/null");
352 safe_system("/usr/local/bin/vpn-watch &");
356 // it is a selective start or stop
357 // second param is only a number 'key'
358 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
360 fprintf(stderr
, "Bad arg\n");
365 // search the vpn pointed by 'key'
366 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
368 fprintf(stderr
, "Couldn't open vpn settings file");
371 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
376 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
379 // start/stop a vpn if belonging to specified interface
380 if (strcmp(argv
[1], interface
) == 0 ) {
381 if (strcmp(argv
[2], "0")==0)
382 turn_connection_off (name
);
384 turn_connection_on (name
, type
);
387 // is it the 'key' requested ?
388 if (strcmp(argv
[2], key
) != 0)
390 // Start or Delete this Connection
391 if (strcmp(argv
[1], "S") == 0)
392 turn_connection_on (name
, type
);
394 if (strcmp(argv
[1], "D") == 0)
395 turn_connection_off (name
);
398 fprintf(stderr
, "Bad command\n");