]>
git.ipfire.org Git - people/teissler/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
;
282 /* Get vpnwatch pid */
284 if (( fd
= open("/var/run/vpn-watch.pid", O_RDONLY
)) != "" ) {
286 safe_system("kill -9 $(cat /var/run/vpn-watch.pid)");
287 safe_system("unlink /var/run/vpn-watch.pid)");
290 /* FIXME: workaround for pclose() issue - still no real idea why
291 * this is happening */
292 signal(SIGCHLD
, SIG_DFL
);
294 /* handle operations that doesn't need start the ipsec system */
296 if (strcmp(argv
[1], "D") == 0) {
298 /* Only shutdown pluto if it really is running */
301 if ((fd
= open("/var/run/pluto.pid", O_RDONLY
)) != -1) {
302 safe_system("/etc/rc.d/init.d/ipsec stop 2> /dev/null >/dev/null");
308 if (strcmp(argv
[1], "R") == 0) {
309 safe_system("/usr/sbin/ipsec auto --rereadall");
314 /* clear iptables vpn rules */
317 /* read vpn config */
319 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
321 fprintf(stderr
, "Cannot read vpn settings\n");
325 /* check is the vpn system is enabled */
328 findkey(kv
, "ENABLED", s
);
330 if (strcmp (s
, "on") != 0)
334 /* read interface settings */
336 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
338 fprintf(stderr
, "Cannot read ethernet settings\n");
341 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
343 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
346 findkey(kv
, "RED_TYPE", redtype
);
349 /* Loop through the config file to find physical interface that will accept IPSEC */
350 int enable_red
=0; // states 0: not used
351 int enable_green
=0; // 1: error condition
352 int enable_orange
=0; // 2: good
354 char if_red
[STRING_SIZE
] = "";
355 char if_green
[STRING_SIZE
] = "";
356 char if_orange
[STRING_SIZE
] = "";
357 char if_blue
[STRING_SIZE
] = "";
361 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
362 fprintf(stderr
, "Couldn't open vpn settings file");
365 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
370 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
372 /* search interface */
373 if (!enable_red
&& strcmp (interface
, "RED") == 0) {
374 // when RED is up, find interface name in special file
375 FILE *ifacefile
= NULL
;
376 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
377 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
378 if (if_red
[strlen(if_red
) - 1] == '\n')
379 if_red
[strlen(if_red
) - 1] = '\0';
383 if (VALID_DEVICE(if_red
))
384 enable_red
+=2; // present and running
388 if (!enable_green
&& strcmp (interface
, "GREEN") == 0) {
390 findkey(kv
, "GREEN_DEV", if_green
);
391 if (VALID_DEVICE(if_green
))
394 fprintf(stderr
, "IPSec enabled on green but green interface is invalid or not found\n");
397 if (!enable_orange
&& strcmp (interface
, "ORANGE") == 0) {
399 findkey(kv
, "ORANGE_DEV", if_orange
);
400 if (VALID_DEVICE(if_orange
))
403 fprintf(stderr
, "IPSec enabled on orange but orange interface is invalid or not found\n");
406 if (!enable_blue
&& strcmp (interface
, "BLUE") == 0) {
408 findkey(kv
, "BLUE_DEV", if_blue
);
409 if (VALID_DEVICE(if_blue
))
412 fprintf(stderr
, "IPSec enabled on blue but blue interface is invalid or not found\n");
419 // do nothing if something is in error condition
420 if ((enable_red
==1) || (enable_green
==1) || (enable_orange
==1) || (enable_blue
==1) )
423 // exit if nothing to do
424 if ( (enable_red
+enable_green
+enable_orange
+enable_blue
) == 0 )
428 // todo: read a nat_t indicator to allow or not openning UDP/4500
430 open_physical(if_red
, 4500);
433 open_physical(if_green
, 4500);
435 if (enable_orange
==2)
436 open_physical(if_orange
, 4500);
439 open_physical(if_blue
, 4500);
441 // then open the ipsecX
445 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
447 safe_system("/usr/sbin/ipsec tncfg --clear >/dev/null");
448 safe_system("/etc/rc.d/init.d/ipsec restart >/dev/null");
449 add_alias_interfaces(configtype
, redtype
, if_red
, (enable_red
+enable_green
+enable_orange
+enable_blue
) >>1 );
453 // it is a selective start or stop
454 // second param is only a number 'key'
455 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
457 fprintf(stderr
, "Bad arg\n");
462 // search the vpn pointed by 'key'
463 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
465 fprintf(stderr
, "Couldn't open vpn settings file");
468 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
473 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
476 // start/stop a vpn if belonging to specified interface
477 if (strcmp(argv
[1], interface
) == 0 ) {
478 if (strcmp(argv
[2], "0")==0)
479 turn_connection_off (name
);
481 turn_connection_on (name
, type
);
484 // is it the 'key' requested ?
485 if (strcmp(argv
[2], key
) != 0)
487 // Start or Delete this Connection
488 if (strcmp(argv
[1], "S") == 0)
489 turn_connection_on (name
, type
);
491 if (strcmp(argv
[1], "D") == 0)
492 turn_connection_off (name
);
495 fprintf(stderr
, "Bad command\n");
500 safe_system("/usr/local/bin/vpn-watch &");