]>
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>
21 This module is responsible for start stop of the vpn system.
23 1) it allows AH & ESP to get in from interface where a vpn is mounted
24 The NAT traversal is used on the udp 4500 port.
26 2) it starts the ipsec daemon
27 The RED interface is a problem because it can be up or down a startup.
28 Then, the state change and it must not affect other VPN mounted on
30 Unfortunatly, openswan 1 cannot do that correctly. It cannot use an
31 interface without restarting everything.
36 fprintf (stderr
, "Usage:\n");
37 fprintf (stderr
, "\tipsecctrl S [connectionkey]\n");
38 fprintf (stderr
, "\tipsecctrl D [connectionkey]\n");
39 fprintf (stderr
, "\tipsecctrl R\n");
40 fprintf (stderr
, "\tipsecctrl I\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");
44 fprintf (stderr
, "\t\tI : Print Statusinfo\n");
47 static void ipsec_reload() {
48 /* Re-read all configuration files and secrets and
49 * reload the daemon (#10339).
51 safe_system("/usr/sbin/ipsec rereadall >/dev/null 2>&1");
52 safe_system("/usr/sbin/ipsec reload >/dev/null 2>&1");
56 ACCEPT the ipsec protocol ah, esp & udp (for nat traversal) on the specified interface
58 void open_physical (char *interface
, int nat_traversal_port
) {
59 char str
[STRING_SIZE
];
62 sprintf(str
, "/sbin/iptables --wait -D IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT >/dev/null 2>&1", interface
);
64 sprintf(str
, "/sbin/iptables --wait -A IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT", interface
);
67 if (! nat_traversal_port
)
70 sprintf(str
, "/sbin/iptables --wait -D IPSECINPUT -p udp -i %s --dport %i -j ACCEPT >/dev/null 2>&1", interface
, nat_traversal_port
);
72 sprintf(str
, "/sbin/iptables --wait -A IPSECINPUT -p udp -i %s --dport %i -j ACCEPT", interface
, nat_traversal_port
);
76 void ipsec_norules() {
77 /* clear input rules */
78 safe_system("/sbin/iptables --wait -F IPSECINPUT");
79 safe_system("/sbin/iptables --wait -F IPSECFORWARD");
80 safe_system("/sbin/iptables --wait -F IPSECOUTPUT");
84 return values from the vpn config file or false if not 'on'
86 int decode_line (char *s
,
96 if (s
[strlen(s
) - 1] == '\n')
97 s
[strlen(s
) - 1] = '\0';
99 char *result
= strsep(&s
, ",");
103 if ((count
== 1) && strcmp(result
, "on") != 0)
104 return 0; // a disabled line
110 result
= strsep(&s
, ",");
113 // check other syntax
117 if (strspn(*name
, LETTERS_NUMBERS
) != strlen(*name
)) {
118 fprintf(stderr
, "Bad connection name: %s\n", *name
);
122 if (! (strcmp(*type
, "host") == 0 || strcmp(*type
, "net") == 0)) {
123 fprintf(stderr
, "Bad connection type: %s\n", *type
);
127 //it's a valid & active line
132 issue ipsec commmands to turn on connection 'name'
134 void turn_connection_on(char *name
, char *type
) {
136 * To bring up a connection, we need to reload the configuration
137 * and issue ipsec up afterwards. To make sure the connection
138 * is not established from the start, we bring it down in advance.
140 char command
[STRING_SIZE
];
142 // Bring down the connection (if established).
143 snprintf(command
, STRING_SIZE
- 1,
144 "/usr/sbin/ipsec down %s >/dev/null", name
);
145 safe_system(command
);
147 // Reload the IPsec block chain
148 safe_system("/usr/lib/firewall/ipsec-block >/dev/null");
150 // Reload the configuration into the daemon (#10339).
153 // Bring the connection up again.
154 snprintf(command
, STRING_SIZE
- 1,
155 "/usr/sbin/ipsec up %s >/dev/null", name
);
156 safe_system(command
);
160 issue ipsec commmands to turn off connection 'name'
162 void turn_connection_off (char *name
) {
164 * To turn off a connection, all SAs must be turned down.
165 * After that, the configuration must be reloaded.
167 char command
[STRING_SIZE
];
169 // Bring down the connection.
170 snprintf(command
, STRING_SIZE
- 1,
171 "/usr/sbin/ipsec down %s >/dev/null", name
);
172 safe_system(command
);
174 // Reload, so the connection is dropped.
177 // Reload the IPsec block chain
178 safe_system("/usr/lib/firewall/ipsec-block >/dev/null");
181 int main(int argc
, char *argv
[]) {
182 char configtype
[STRING_SIZE
];
183 char redtype
[STRING_SIZE
] = "";
184 struct keyvalue
*kv
= NULL
;
196 if (strcmp(argv
[1], "I") == 0) {
197 safe_system("/usr/sbin/ipsec status");
201 if (strcmp(argv
[1], "R") == 0) {
206 /* FIXME: workaround for pclose() issue - still no real idea why
207 * this is happening */
208 signal(SIGCHLD
, SIG_DFL
);
210 /* handle operations that doesn't need start the ipsec system */
212 if (strcmp(argv
[1], "D") == 0) {
213 safe_system("/usr/sbin/ipsec stop >/dev/null 2>&1");
219 /* read vpn config */
221 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
223 fprintf(stderr
, "Cannot read vpn settings\n");
227 /* check is the vpn system is enabled */
230 findkey(kv
, "ENABLED", s
);
232 if (strcmp (s
, "on") != 0)
236 /* read interface settings */
238 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
240 fprintf(stderr
, "Cannot read ethernet settings\n");
243 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
245 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
248 findkey(kv
, "RED_TYPE", redtype
);
251 /* Loop through the config file to find physical interface that will accept IPSEC */
252 int enable_red
=0; // states 0: not used
253 int enable_green
=0; // 1: error condition
254 int enable_orange
=0; // 2: good
256 char if_red
[STRING_SIZE
] = "";
257 char if_green
[STRING_SIZE
] = "";
258 char if_orange
[STRING_SIZE
] = "";
259 char if_blue
[STRING_SIZE
] = "";
262 // when RED is up, find interface name in special file
263 FILE *ifacefile
= NULL
;
264 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
265 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
266 if (if_red
[strlen(if_red
) - 1] == '\n')
267 if_red
[strlen(if_red
) - 1] = '\0';
271 if (VALID_DEVICE(if_red
))
275 // Check if GREEN is enabled.
276 findkey(kv
, "GREEN_DEV", if_green
);
277 if (VALID_DEVICE(if_green
))
280 // Check if ORANGE is enabled.
281 findkey(kv
, "ORANGE_DEV", if_orange
);
282 if (VALID_DEVICE(if_orange
))
285 // Check if BLUE is enabled.
286 findkey(kv
, "BLUE_DEV", if_blue
);
287 if (VALID_DEVICE(if_blue
))
292 // exit if nothing to do
293 if ((enable_red
+enable_green
+enable_orange
+enable_blue
) == 0)
298 open_physical(if_red
, 4500);
300 if (enable_green
> 0)
301 open_physical(if_green
, 4500);
303 if (enable_orange
> 0)
304 open_physical(if_orange
, 4500);
307 open_physical(if_blue
, 4500);
310 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
311 safe_system("/usr/lib/firewall/ipsec-block >/dev/null");
312 safe_system("/usr/sbin/ipsec restart >/dev/null");
316 // it is a selective start or stop
317 // second param is only a number 'key'
318 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
319 fprintf(stderr
, "Bad arg: %s\n", argv
[2]);
324 // search the vpn pointed by 'key'
325 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
326 fprintf(stderr
, "Couldn't open vpn settings file");
329 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
333 if (!decode_line(s
,&key
,&name
,&type
))
336 // is it the 'key' requested ?
337 if (strcmp(argv
[2], key
) != 0)
340 // Start or Delete this Connection
341 if (strcmp(argv
[1], "S") == 0)
342 turn_connection_on (name
, type
);
343 else if (strcmp(argv
[1], "D") == 0)
344 turn_connection_off (name
);
346 fprintf(stderr
, "Bad command\n");