]>
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
, "2")==0) || (strcmp(configtype
, "3")==0) || (strcmp(configtype
, "6")==0) || (strcmp(configtype
, "7")==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 /* FIXME: workaround for pclose() issue - still no real idea why
283 * this is happening */
284 signal(SIGCHLD
, SIG_DFL
);
286 /* handle operations that doesn't need start the ipsec system */
288 if (strcmp(argv
[1], "D") == 0) {
289 safe_system("/usr/local/bin/vpn-watch --stop");
291 /* Only shutdown pluto if it really is running */
294 if ((fd
= open("/var/run/pluto.pid", O_RDONLY
)) != -1) {
295 safe_system("/etc/rc.d/ipsec stop 2> /dev/null >/dev/null");
301 if (strcmp(argv
[1], "R") == 0) {
302 safe_system("/usr/sbin/ipsec auto --rereadall");
307 /* stop the watch script as soon as possible */
308 safe_system("/usr/local/bin/vpn-watch --stop");
310 /* clear iptables vpn rules */
313 /* read vpn config */
315 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
317 fprintf(stderr
, "Cannot read vpn settings\n");
321 /* check is the vpn system is enabled */
324 findkey(kv
, "ENABLED", s
);
326 if (strcmp (s
, "on") != 0)
330 /* read interface settings */
332 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
334 fprintf(stderr
, "Cannot read ethernet settings\n");
337 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
339 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
342 findkey(kv
, "RED_TYPE", redtype
);
345 /* Loop through the config file to find physical interface that will accept IPSEC */
346 int enable_red
=0; // states 0: not used
347 int enable_green
=0; // 1: error condition
348 int enable_orange
=0; // 2: good
350 char if_red
[STRING_SIZE
] = "";
351 char if_green
[STRING_SIZE
] = "";
352 char if_orange
[STRING_SIZE
] = "";
353 char if_blue
[STRING_SIZE
] = "";
357 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
358 fprintf(stderr
, "Couldn't open vpn settings file");
361 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
366 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
368 /* search interface */
369 if (!enable_red
&& strcmp (interface
, "RED") == 0) {
370 // when RED is up, find interface name in special file
371 FILE *ifacefile
= NULL
;
372 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
373 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
374 if (if_red
[strlen(if_red
) - 1] == '\n')
375 if_red
[strlen(if_red
) - 1] = '\0';
379 if (VALID_DEVICE(if_red
))
380 enable_red
+=2; // present and running
384 if (!enable_green
&& strcmp (interface
, "GREEN") == 0) {
386 findkey(kv
, "GREEN_DEV", if_green
);
387 if (VALID_DEVICE(if_green
))
390 fprintf(stderr
, "IPSec enabled on green but green interface is invalid or not found\n");
393 if (!enable_orange
&& strcmp (interface
, "ORANGE") == 0) {
395 findkey(kv
, "ORANGE_DEV", if_orange
);
396 if (VALID_DEVICE(if_orange
))
399 fprintf(stderr
, "IPSec enabled on orange but orange interface is invalid or not found\n");
402 if (!enable_blue
&& strcmp (interface
, "BLUE") == 0) {
404 findkey(kv
, "BLUE_DEV", if_blue
);
405 if (VALID_DEVICE(if_blue
))
408 fprintf(stderr
, "IPSec enabled on blue but blue interface is invalid or not found\n");
415 // do nothing if something is in error condition
416 if ((enable_red
==1) || (enable_green
==1) || (enable_orange
==1) || (enable_blue
==1) )
419 // exit if nothing to do
420 if ( (enable_red
+enable_green
+enable_orange
+enable_blue
) == 0 )
424 // todo: read a nat_t indicator to allow or not openning UDP/4500
426 open_physical(if_red
, 4500);
429 open_physical(if_green
, 4500);
431 if (enable_orange
==2)
432 open_physical(if_orange
, 4500);
435 open_physical(if_blue
, 4500);
437 // then open the ipsecX
441 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
443 safe_system("/usr/sbin/ipsec tncfg --clear >/dev/null");
444 safe_system("/etc/rc.d/ipsec restart >/dev/null");
445 add_alias_interfaces(configtype
, redtype
, if_red
, (enable_red
+enable_green
+enable_orange
+enable_blue
) >>1 );
446 safe_system("/usr/local/bin/vpn-watch --start");
450 // it is a selective start or stop
451 // second param is only a number 'key'
452 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
454 fprintf(stderr
, "Bad arg\n");
459 // search the vpn pointed by 'key'
460 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
462 fprintf(stderr
, "Couldn't open vpn settings file");
465 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
470 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
473 // start/stop a vpn if belonging to specified interface
474 if (strcmp(argv
[1], interface
) == 0 ) {
475 if (strcmp(argv
[2], "0")==0)
476 turn_connection_off (name
);
478 turn_connection_on (name
, type
);
481 // is it the 'key' requested ?
482 if (strcmp(argv
[2], key
) != 0)
484 // Start or Delete this Connection
485 if (strcmp(argv
[1], "S") == 0)
486 turn_connection_on (name
, type
);
488 if (strcmp(argv
[1], "D") == 0)
489 turn_connection_off (name
);
492 fprintf(stderr
, "Bad command\n");
497 safe_system("/usr/local/bin/vpn-watch --start");