]>
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.
33 #define phystable "IPSECPHYSICAL"
34 #define virtualtable "IPSECVIRTUAL"
37 fprintf (stderr
, "Usage:\n");
38 fprintf (stderr
, "\tipsecctrl S [connectionkey]\n");
39 fprintf (stderr
, "\tipsecctrl D [connectionkey]\n");
40 fprintf (stderr
, "\tipsecctrl R\n");
41 fprintf (stderr
, "\t\tS : Start/Restart Connection\n");
42 fprintf (stderr
, "\t\tD : Stop Connection\n");
43 fprintf (stderr
, "\t\tR : Reload Certificates and Secrets\n");
47 safe_system("/sbin/modprobe ipsec");
51 ACCEPT the ipsec protocol ah, esp & udp (for nat traversal) on the specified interface
53 void open_physical (char *interface
, int nat_traversal_port
) {
54 char str
[STRING_SIZE
];
57 sprintf(str
, "/sbin/iptables -A " phystable
" -p 47 -i %s -j ACCEPT", interface
);
60 sprintf(str
, "/sbin/iptables -A " phystable
" -p 50 -i %s -j ACCEPT", interface
);
63 sprintf(str
, "/sbin/iptables -A " phystable
" -p 51 -i %s -j ACCEPT", interface
);
66 sprintf(str
, "/sbin/iptables -A " phystable
" -p udp -i %s --sport 500 --dport 500 -j ACCEPT", interface
);
69 if (! nat_traversal_port
)
72 sprintf(str
, "/sbin/iptables -A " phystable
" -p udp -i %s --dport %i -j ACCEPT", interface
, nat_traversal_port
);
77 Basic control for what can flow from/to ipsecX interfaces.
79 rc.firewall call this chain just before ACCEPTing everything
80 from green (-i DEV_GREEN -j ACCEPT).
82 void open_virtual (void) {
83 // allow anything from any ipsec to go on all interface, including other ipsec
84 safe_system("/sbin/iptables -A " virtualtable
" -i ipsec+ -j ACCEPT");
85 //todo: BOT extension?; allowing ipsec0<<==port-list-filter==>>GREEN ?
88 void ipsec_norules() {
89 /* clear input rules */
90 safe_system("/sbin/iptables -F " phystable
);
91 safe_system("/sbin/iptables -F " virtualtable
);
93 // unmap red alias ????
97 void add_alias_interfaces(char *configtype
,
100 int offset
) //reserve room for ipsec0=red, ipsec1=green, ipsec2=orange,ipsec3=blue
106 /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,
107 * exit gracefully. This is not an error... */
108 if (!((strcmp(configtype
, "1")==0) || (strcmp(configtype
, "2")==0) || (strcmp(configtype
, "3")==0) || (strcmp(configtype
, "4")==0)))
111 /* Now check the RED_TYPE - aliases only work with STATIC. */
112 if (!(strcmp(redtype
, "STATIC")==0))
115 /* Now set up the new aliases from the config file */
116 if (!(file
= fopen(CONFIG_ROOT
"/ethernet/aliases", "r")))
118 fprintf(stderr
, "Unable to open aliases configuration file\n");
121 while (fgets(s
, STRING_SIZE
, file
) != NULL
&& (offset
+alias
) < 16 )
123 if (s
[strlen(s
) - 1] == '\n')
124 s
[strlen(s
) - 1] = '\0';
129 char *sptr
= strtok(s
, ",");
139 sptr
= strtok(NULL
, ",");
142 if (!(aliasip
&& enabled
))
145 if (!VALID_IP(aliasip
))
147 fprintf(stderr
, "Bad alias : %s\n", aliasip
);
151 if (strcmp(enabled
, "on") == 0)
153 memset(s
, 0, STRING_SIZE
);
154 snprintf(s
, STRING_SIZE
-1, "/usr/sbin/ipsec tncfg --attach --virtual ipsec%d --physical %s:%d >/dev/null", offset
+alias
, redif
, alias
);
162 return values from the vpn config file or false if not 'on'
164 int decode_line (char *s
,
175 if (s
[strlen(s
) - 1] == '\n')
176 s
[strlen(s
) - 1] = '\0';
178 char *result
= strsep(&s
, ",");
182 if ((count
== 1) && strcmp(result
, "on") != 0)
183 return 0; // a disabled line
191 result
= strsep(&s
, ",");
194 // check other syntax
198 if (strspn(*name
, LETTERS_NUMBERS
) != strlen(*name
)) {
199 fprintf(stderr
, "Bad connection name: %s\n", *name
);
203 if (! (strcmp(*type
, "host") == 0 || strcmp(*type
, "net") == 0)) {
204 fprintf(stderr
, "Bad connection type: %s\n", *type
);
208 if (! (strcmp(*interface
, "RED") == 0 || strcmp(*interface
, "GREEN") == 0 ||
209 strcmp(*interface
, "ORANGE") == 0 || strcmp(*interface
, "BLUE") == 0)) {
210 fprintf(stderr
, "Bad interface name: %s\n", *interface
);
213 //it's a valid & active line
218 issue ipsec commmands to turn on connection 'name'
220 void turn_connection_on (char *name
, char *type
) {
221 char command
[STRING_SIZE
];
223 safe_system("/usr/sbin/ipsec auto --rereadsecrets >/dev/null");
224 memset(command
, 0, STRING_SIZE
);
225 snprintf(command
, STRING_SIZE
- 1,
226 "/usr/sbin/ipsec auto --replace %s >/dev/null", name
);
227 safe_system(command
);
228 if (strcmp(type
, "net") == 0) {
229 memset(command
, 0, STRING_SIZE
);
230 snprintf(command
, STRING_SIZE
- 1,
231 "/usr/sbin/ipsec auto --asynchronous --up %s >/dev/null", name
);
232 safe_system(command
);
236 issue ipsec commmands to turn off connection 'name'
238 void turn_connection_off (char *name
) {
239 char command
[STRING_SIZE
];
241 memset(command
, 0, STRING_SIZE
);
242 snprintf(command
, STRING_SIZE
- 1,
243 "/usr/sbin/ipsec auto --down %s >/dev/null", name
);
244 safe_system(command
);
245 memset(command
, 0, STRING_SIZE
);
246 snprintf(command
, STRING_SIZE
- 1,
247 "/usr/sbin/ipsec auto --delete %s >/dev/null", name
);
248 safe_system(command
);
249 safe_system("/usr/sbin/ipsec auto --rereadsecrets >/dev/null");
253 int main(int argc
, char *argv
[]) {
255 char configtype
[STRING_SIZE
];
256 char redtype
[STRING_SIZE
] = "";
257 struct keyvalue
*kv
= NULL
;
268 /* Get vpnwatch pid */
270 if (file
= fopen("/var/run/vpn-watch.pid", "r")) {
271 safe_system("kill -9 $(cat /var/run/vpn-watch.pid)");
272 safe_system("unlink /var/run/vpn-watch.pid)");
276 /* FIXME: workaround for pclose() issue - still no real idea why
277 * this is happening */
278 signal(SIGCHLD
, SIG_DFL
);
280 /* handle operations that doesn't need start the ipsec system */
282 if (strcmp(argv
[1], "D") == 0) {
284 /* Only shutdown pluto if it really is running */
286 if (file
= fopen("/var/run/pluto.pid", "r")) {
287 safe_system("/etc/rc.d/init.d/ipsec stop 2> /dev/null >/dev/null");
293 if (strcmp(argv
[1], "R") == 0) {
294 safe_system("/usr/sbin/ipsec auto --rereadall");
299 /* clear iptables vpn rules */
302 /* read vpn config */
304 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
306 fprintf(stderr
, "Cannot read vpn settings\n");
310 /* check is the vpn system is enabled */
313 findkey(kv
, "ENABLED", s
);
315 if (strcmp (s
, "on") != 0)
319 /* read interface settings */
321 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
323 fprintf(stderr
, "Cannot read ethernet settings\n");
326 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
328 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
331 findkey(kv
, "RED_TYPE", redtype
);
334 /* Loop through the config file to find physical interface that will accept IPSEC */
335 int enable_red
=0; // states 0: not used
336 int enable_green
=0; // 1: error condition
337 int enable_orange
=0; // 2: good
339 char if_red
[STRING_SIZE
] = "";
340 char if_green
[STRING_SIZE
] = "";
341 char if_orange
[STRING_SIZE
] = "";
342 char if_blue
[STRING_SIZE
] = "";
345 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
346 fprintf(stderr
, "Couldn't open vpn settings file");
349 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
354 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
356 /* search interface */
357 if (!enable_red
&& strcmp (interface
, "RED") == 0) {
358 // when RED is up, find interface name in special file
359 FILE *ifacefile
= NULL
;
360 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
361 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
362 if (if_red
[strlen(if_red
) - 1] == '\n')
363 if_red
[strlen(if_red
) - 1] = '\0';
367 if (VALID_DEVICE(if_red
))
368 enable_red
+=2; // present and running
372 if (!enable_green
&& strcmp (interface
, "GREEN") == 0) {
374 findkey(kv
, "GREEN_DEV", if_green
);
375 if (VALID_DEVICE(if_green
))
378 fprintf(stderr
, "IPSec enabled on green but green interface is invalid or not found\n");
381 if (!enable_orange
&& strcmp (interface
, "ORANGE") == 0) {
383 findkey(kv
, "ORANGE_DEV", if_orange
);
384 if (VALID_DEVICE(if_orange
))
387 fprintf(stderr
, "IPSec enabled on orange but orange interface is invalid or not found\n");
390 if (!enable_blue
&& strcmp (interface
, "BLUE") == 0) {
392 findkey(kv
, "BLUE_DEV", if_blue
);
393 if (VALID_DEVICE(if_blue
))
396 fprintf(stderr
, "IPSec enabled on blue but blue interface is invalid or not found\n");
403 // do nothing if something is in error condition
404 if ((enable_red
==1) || (enable_green
==1) || (enable_orange
==1) || (enable_blue
==1) )
407 // exit if nothing to do
408 if ( (enable_red
+enable_green
+enable_orange
+enable_blue
) == 0 )
412 // todo: read a nat_t indicator to allow or not openning UDP/4500
414 open_physical(if_red
, 4500);
417 open_physical(if_green
, 4500);
419 if (enable_orange
==2)
420 open_physical(if_orange
, 4500);
423 open_physical(if_blue
, 4500);
425 // then open the ipsecX
429 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
431 safe_system("/usr/sbin/ipsec tncfg --clear >/dev/null");
432 safe_system("/etc/rc.d/init.d/ipsec restart >/dev/null");
433 add_alias_interfaces(configtype
, redtype
, if_red
, (enable_red
+enable_green
+enable_orange
+enable_blue
) >>1 );
437 // it is a selective start or stop
438 // second param is only a number 'key'
439 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
441 fprintf(stderr
, "Bad arg\n");
446 // search the vpn pointed by 'key'
447 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
449 fprintf(stderr
, "Couldn't open vpn settings file");
452 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
457 if (!decode_line(s
,&key
,&name
,&type
,&interface
))
460 // start/stop a vpn if belonging to specified interface
461 if (strcmp(argv
[1], interface
) == 0 ) {
462 if (strcmp(argv
[2], "0")==0)
463 turn_connection_off (name
);
465 turn_connection_on (name
, type
);
468 // is it the 'key' requested ?
469 if (strcmp(argv
[2], key
) != 0)
471 // Start or Delete this Connection
472 if (strcmp(argv
[1], "S") == 0)
473 turn_connection_on (name
, type
);
475 if (strcmp(argv
[1], "D") == 0)
476 turn_connection_off (name
);
479 fprintf(stderr
, "Bad command\n");
484 safe_system("/usr/local/bin/vpn-watch &");