]>
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.
31 IPCop should control vpn this way:
34 call ipsecctrl once to start vpns on all interface
35 RED based vpn won't start because "auto=ignore" instead off "auto=start"
38 call ipsectrl to turn on or off vpn based on RED
43 call ipsectrl S at every event on RED.
44 Consequence: BLUE vpn is not started until RED goes up.
49 #define phystable "IPSECPHYSICAL"
50 #define virtualtable "IPSECVIRTUAL"
53 fprintf (stderr
, "Usage:\n");
54 fprintf (stderr
, "\tipsecctrl S [connectionkey]\n");
55 fprintf (stderr
, "\tipsecctrl D [connectionkey]\n");
56 fprintf (stderr
, "\tipsecctrl R\n");
57 fprintf (stderr
, "\t\tS : Start/Restart Connection\n");
58 fprintf (stderr
, "\t\tD : Stop Connection\n");
59 fprintf (stderr
, "\t\tR : Reload Certificates and Secrets\n");
63 safe_system("/sbin/modprobe ipsec");
67 ACCEPT the ipsec protocol ah, esp & udp (for nat traversal) on the specified interface
69 void open_physical (char *interface
, int nat_traversal_port
) {
70 char str
[STRING_SIZE
];
73 sprintf(str
, "/sbin/iptables -A " phystable
" -p 47 -i %s -j ACCEPT", interface
);
76 sprintf(str
, "/sbin/iptables -A " phystable
" -p 50 -i %s -j ACCEPT", interface
);
79 sprintf(str
, "/sbin/iptables -A " phystable
" -p 51 -i %s -j ACCEPT", interface
);
82 sprintf(str
, "/sbin/iptables -A " phystable
" -p udp -i %s --sport 500 --dport 500 -j ACCEPT", interface
);
85 if (! nat_traversal_port
)
88 sprintf(str
, "/sbin/iptables -A " phystable
" -p udp -i %s --dport %i -j ACCEPT", interface
, nat_traversal_port
);
93 Basic control for what can flow from/to ipsecX interfaces.
95 rc.firewall call this chain just before ACCEPTing everything
96 from green (-i DEV_GREEN -j ACCEPT).
98 void open_virtual (void) {
99 // allow anything from any ipsec to go on all interface, including other ipsec
100 safe_system("/sbin/iptables -A " virtualtable
" -i ipsec+ -j ACCEPT");
101 //todo: BOT extension?; allowing ipsec0<<==port-list-filter==>>GREEN ?
104 void ipsec_norules() {
105 /* clear input rules */
106 safe_system("/sbin/iptables -F " phystable
);
107 safe_system("/sbin/iptables -F " virtualtable
);
109 // unmap red alias ????
113 void add_alias_interfaces(char *configtype
,
116 int offset
) //reserve room for ipsec0=red, ipsec1=green, ipsec2=orange,ipsec3=blue
122 /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,
123 * exit gracefully. This is not an error... */
124 if (!((strcmp(configtype
, "1")==0) || (strcmp(configtype
, "2")==0) || (strcmp(configtype
, "3")==0) || (strcmp(configtype
, "4")==0)))
127 /* Now check the RED_TYPE - aliases only work with STATIC. */
128 if (!(strcmp(redtype
, "STATIC")==0))
131 /* Now set up the new aliases from the config file */
132 if (!(file
= fopen(CONFIG_ROOT
"/ethernet/aliases", "r")))
134 fprintf(stderr
, "Unable to open aliases configuration file\n");
137 while (fgets(s
, STRING_SIZE
, file
) != NULL
&& (offset
+alias
) < 16 )
139 if (s
[strlen(s
) - 1] == '\n')
140 s
[strlen(s
) - 1] = '\0';
145 char *sptr
= strtok(s
, ",");
155 sptr
= strtok(NULL
, ",");
158 if (!(aliasip
&& enabled
))
161 if (!VALID_IP(aliasip
))
163 fprintf(stderr
, "Bad alias : %s\n", aliasip
);
167 if (strcmp(enabled
, "on") == 0)
169 memset(s
, 0, STRING_SIZE
);
170 snprintf(s
, STRING_SIZE
-1, "/usr/sbin/ipsec tncfg --attach --virtual ipsec%d --physical %s:%d >/dev/null", offset
+alias
, redif
, alias
);
178 return values from the vpn config file or false if not 'on'
180 int decode_line (char *s
,
191 if (s
[strlen(s
) - 1] == '\n')
192 s
[strlen(s
) - 1] = '\0';
194 char *result
= strsep(&s
, ",");
198 if ((count
== 1) && strcmp(result
, "on") != 0)
199 return 0; // a disabled line
207 result
= strsep(&s
, ",");
210 // check other syntax
214 if (strspn(*name
, LETTERS_NUMBERS
) != strlen(*name
)) {
215 fprintf(stderr
, "Bad connection name: %s\n", *name
);
219 if (! (strcmp(*type
, "host") == 0 || strcmp(*type
, "net") == 0)) {
220 fprintf(stderr
, "Bad connection type: %s\n", *type
);
224 if (! (strcmp(*interface
, "RED") == 0 || strcmp(*interface
, "GREEN") == 0 ||
225 strcmp(*interface
, "ORANGE") == 0 || strcmp(*interface
, "BLUE") == 0)) {
226 fprintf(stderr
, "Bad interface name: %s\n", *interface
);
229 //it's a valid & active line
234 issue ipsec commmands to turn on connection 'name'
236 void turn_connection_on (char *name
, char *type
) {
237 char command
[STRING_SIZE
];
239 safe_system("/usr/sbin/ipsec auto --rereadsecrets >/dev/null");
240 memset(command
, 0, STRING_SIZE
);
241 snprintf(command
, STRING_SIZE
- 1,
242 "/usr/sbin/ipsec auto --replace %s >/dev/null", name
);
243 safe_system(command
);
244 if (strcmp(type
, "net") == 0) {
245 memset(command
, 0, STRING_SIZE
);
246 snprintf(command
, STRING_SIZE
- 1,
247 "/usr/sbin/ipsec auto --asynchronous --up %s >/dev/null", name
);
248 safe_system(command
);
252 issue ipsec commmands to turn off connection 'name'
254 void turn_connection_off (char *name
) {
255 char command
[STRING_SIZE
];
257 memset(command
, 0, STRING_SIZE
);
258 snprintf(command
, STRING_SIZE
- 1,
259 "/usr/sbin/ipsec auto --down %s >/dev/null", name
);
260 safe_system(command
);
261 memset(command
, 0, STRING_SIZE
);
262 snprintf(command
, STRING_SIZE
- 1,
263 "/usr/sbin/ipsec auto --delete %s >/dev/null", name
);
264 safe_system(command
);
265 safe_system("/usr/sbin/ipsec auto --rereadsecrets >/dev/null");
269 int main(int argc
, char *argv
[]) {
271 char configtype
[STRING_SIZE
];
272 char redtype
[STRING_SIZE
] = "";
273 struct keyvalue
*kv
= NULL
;
284 /* Get vpnwatch pid */
286 if (file
= fopen("/var/run/vpn-watch.pid", "r")) {
287 safe_system("kill -9 fd");
288 safe_system("unlink /var/run/vpn-watch.pid)");
292 /* FIXME: workaround for pclose() issue - still no real idea why
293 * this is happening */
294 signal(SIGCHLD
, SIG_DFL
);
296 /* handle operations that doesn't need start the ipsec system */
298 if (strcmp(argv
[1], "D") == 0) {
300 /* Only shutdown pluto if it really is running */
303 if ((fd
= open("/var/run/pluto.pid", O_RDONLY
)) != -1) {
304 safe_system("/etc/rc.d/init.d/ipsec stop 2> /dev/null >/dev/null");
310 if (strcmp(argv
[1], "R") == 0) {
311 safe_system("/usr/sbin/ipsec auto --rereadall");
316 /* clear iptables vpn rules */
319 /* read vpn config */
321 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
323 fprintf(stderr
, "Cannot read vpn settings\n");
327 /* check is the vpn system is enabled */
330 findkey(kv
, "ENABLED", s
);
332 if (strcmp (s
, "on") != 0)
336 /* read interface settings */
338 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
340 fprintf(stderr
, "Cannot read ethernet settings\n");
343 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
345 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
348 findkey(kv
, "RED_TYPE", redtype
);
351 /* Loop through the config file to find physical interface that will accept IPSEC */
352 int enable_red
=0; // states 0: not used
353 int enable_green
=0; // 1: error condition
354 int enable_orange
=0; // 2: good
356 char if_red
[STRING_SIZE
] = "";
357 char if_green
[STRING_SIZE
] = "";
358 char if_orange
[STRING_SIZE
] = "";
359 char if_blue
[STRING_SIZE
] = "";
362 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
363 fprintf(stderr
, "Couldn't open vpn settings file");
366 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
371 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
373 /* search interface */
374 if (!enable_red
&& strcmp (interface
, "RED") == 0) {
375 // when RED is up, find interface name in special file
376 FILE *ifacefile
= NULL
;
377 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
378 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
379 if (if_red
[strlen(if_red
) - 1] == '\n')
380 if_red
[strlen(if_red
) - 1] = '\0';
384 if (VALID_DEVICE(if_red
))
385 enable_red
+=2; // present and running
389 if (!enable_green
&& strcmp (interface
, "GREEN") == 0) {
391 findkey(kv
, "GREEN_DEV", if_green
);
392 if (VALID_DEVICE(if_green
))
395 fprintf(stderr
, "IPSec enabled on green but green interface is invalid or not found\n");
398 if (!enable_orange
&& strcmp (interface
, "ORANGE") == 0) {
400 findkey(kv
, "ORANGE_DEV", if_orange
);
401 if (VALID_DEVICE(if_orange
))
404 fprintf(stderr
, "IPSec enabled on orange but orange interface is invalid or not found\n");
407 if (!enable_blue
&& strcmp (interface
, "BLUE") == 0) {
409 findkey(kv
, "BLUE_DEV", if_blue
);
410 if (VALID_DEVICE(if_blue
))
413 fprintf(stderr
, "IPSec enabled on blue but blue interface is invalid or not found\n");
420 // do nothing if something is in error condition
421 if ((enable_red
==1) || (enable_green
==1) || (enable_orange
==1) || (enable_blue
==1) )
424 // exit if nothing to do
425 if ( (enable_red
+enable_green
+enable_orange
+enable_blue
) == 0 )
429 // todo: read a nat_t indicator to allow or not openning UDP/4500
431 open_physical(if_red
, 4500);
434 open_physical(if_green
, 4500);
436 if (enable_orange
==2)
437 open_physical(if_orange
, 4500);
440 open_physical(if_blue
, 4500);
442 // then open the ipsecX
446 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
448 safe_system("/usr/sbin/ipsec tncfg --clear >/dev/null");
449 safe_system("/etc/rc.d/init.d/ipsec restart >/dev/null");
450 add_alias_interfaces(configtype
, redtype
, if_red
, (enable_red
+enable_green
+enable_orange
+enable_blue
) >>1 );
454 // it is a selective start or stop
455 // second param is only a number 'key'
456 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
458 fprintf(stderr
, "Bad arg\n");
463 // search the vpn pointed by 'key'
464 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
466 fprintf(stderr
, "Couldn't open vpn settings file");
469 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
474 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
477 // start/stop a vpn if belonging to specified interface
478 if (strcmp(argv
[1], interface
) == 0 ) {
479 if (strcmp(argv
[2], "0")==0)
480 turn_connection_off (name
);
482 turn_connection_on (name
, type
);
485 // is it the 'key' requested ?
486 if (strcmp(argv
[2], key
) != 0)
488 // Start or Delete this Connection
489 if (strcmp(argv
[1], "S") == 0)
490 turn_connection_on (name
, type
);
492 if (strcmp(argv
[1], "D") == 0)
493 turn_connection_off (name
);
496 fprintf(stderr
, "Bad command\n");
501 safe_system("/usr/local/bin/vpn-watch &");