]>
git.ipfire.org Git - people/mfischer/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");
48 ACCEPT the ipsec protocol ah, esp & udp (for nat traversal) on the specified interface
50 void open_physical (char *interface
, int nat_traversal_port
) {
51 char str
[STRING_SIZE
];
54 // sprintf(str, "/sbin/iptables -A " phystable " -p 47 -i %s -j ACCEPT", interface);
57 // sprintf(str, "/sbin/iptables -A " phystable " -p 50 -i %s -j ACCEPT", interface);
60 // sprintf(str, "/sbin/iptables -A " phystable " -p 51 -i %s -j ACCEPT", interface);
64 sprintf(str
, "/sbin/iptables -D IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT >/dev/null 2>&1", interface
);
66 sprintf(str
, "/sbin/iptables -A IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT", interface
);
69 if (! nat_traversal_port
)
72 sprintf(str
, "/sbin/iptables -D IPSECINPUT -p udp -i %s --dport %i -j ACCEPT >/dev/null 2>&1", interface
, nat_traversal_port
);
74 sprintf(str
, "/sbin/iptables -A IPSECINPUT -p udp -i %s --dport %i -j ACCEPT", interface
, nat_traversal_port
);
78 void ipsec_norules() {
79 /* clear input rules */
80 safe_system("/sbin/iptables -F IPSECINPUT");
81 safe_system("/sbin/iptables -F IPSECFORWARD");
82 safe_system("/sbin/iptables -F IPSECOUTPUT");
86 return values from the vpn config file or false if not 'on'
88 int decode_line (char *s
,
98 if (s
[strlen(s
) - 1] == '\n')
99 s
[strlen(s
) - 1] = '\0';
101 char *result
= strsep(&s
, ",");
105 if ((count
== 1) && strcmp(result
, "on") != 0)
106 return 0; // a disabled line
112 result
= strsep(&s
, ",");
115 // check other syntax
119 if (strspn(*name
, LETTERS_NUMBERS
) != strlen(*name
)) {
120 fprintf(stderr
, "Bad connection name: %s\n", *name
);
124 if (! (strcmp(*type
, "host") == 0 || strcmp(*type
, "net") == 0)) {
125 fprintf(stderr
, "Bad connection type: %s\n", *type
);
129 //it's a valid & active line
134 issue ipsec commmands to turn on connection 'name'
136 void turn_connection_on(char *name
, char *type
) {
138 * To bring up a connection, we need to reload the configuration
139 * and issue ipsec up afterwards. To make sure the connection
140 * is not established from the start, we bring it down in advance.
142 char command
[STRING_SIZE
];
144 // Bring down the connection (if established).
145 snprintf(command
, STRING_SIZE
- 1,
146 "/usr/sbin/ipsec down %s >/dev/null", name
);
147 safe_system(command
);
149 // Reload the configuration into the daemon (#10339).
152 // Bring the connection up again.
153 snprintf(command
, STRING_SIZE
- 1,
154 "/usr/sbin/ipsec up %s >/dev/null", name
);
155 safe_system(command
);
159 issue ipsec commmands to turn off connection 'name'
161 void turn_connection_off (char *name
) {
163 * To turn off a connection, all SAs must be turned down.
164 * After that, the configuration must be reloaded.
166 char command
[STRING_SIZE
];
168 // Bring down the connection.
169 snprintf(command
, STRING_SIZE
- 1,
170 "/usr/sbin/ipsec down %s >/dev/null", name
);
171 safe_system(command
);
173 // Reload, so the connection is dropped.
177 void ipsec_reload() {
178 /* Re-read all configuration files and secrets and
179 * reload the daemon (#10339).
181 safe_system("/usr/sbin/ipsec rereadall >/dev/null 2>&1");
182 safe_system("/usr/sbin/ipsec reload >/dev/null 2>&1");
185 int main(int argc
, char *argv
[]) {
186 char configtype
[STRING_SIZE
];
187 char redtype
[STRING_SIZE
] = "";
188 struct keyvalue
*kv
= NULL
;
200 if (strcmp(argv
[1], "I") == 0) {
201 safe_system("/usr/sbin/ipsec status");
205 if (strcmp(argv
[1], "R") == 0) {
210 /* FIXME: workaround for pclose() issue - still no real idea why
211 * this is happening */
212 signal(SIGCHLD
, SIG_DFL
);
214 /* handle operations that doesn't need start the ipsec system */
216 if (strcmp(argv
[1], "D") == 0) {
217 safe_system("/usr/sbin/ipsec stop >/dev/null 2>&1");
223 /* read vpn config */
225 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
227 fprintf(stderr
, "Cannot read vpn settings\n");
231 /* check is the vpn system is enabled */
234 findkey(kv
, "ENABLED", s
);
236 if (strcmp (s
, "on") != 0)
240 /* read interface settings */
242 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
244 fprintf(stderr
, "Cannot read ethernet settings\n");
247 if (!findkey(kv
, "CONFIG_TYPE", configtype
))
249 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
252 findkey(kv
, "RED_TYPE", redtype
);
255 /* Loop through the config file to find physical interface that will accept IPSEC */
256 int enable_red
=0; // states 0: not used
257 int enable_green
=0; // 1: error condition
258 int enable_orange
=0; // 2: good
260 char if_red
[STRING_SIZE
] = "";
261 char if_green
[STRING_SIZE
] = "";
262 char if_orange
[STRING_SIZE
] = "";
263 char if_blue
[STRING_SIZE
] = "";
266 // when RED is up, find interface name in special file
267 FILE *ifacefile
= NULL
;
268 if ((ifacefile
= fopen(CONFIG_ROOT
"/red/iface", "r"))) {
269 if (fgets(if_red
, STRING_SIZE
, ifacefile
)) {
270 if (if_red
[strlen(if_red
) - 1] == '\n')
271 if_red
[strlen(if_red
) - 1] = '\0';
275 if (VALID_DEVICE(if_red
))
279 // Check if GREEN is enabled.
280 findkey(kv
, "GREEN_DEV", if_green
);
281 if (VALID_DEVICE(if_green
))
284 // Check if ORANGE is enabled.
285 findkey(kv
, "ORANGE_DEV", if_orange
);
286 if (VALID_DEVICE(if_orange
))
289 // Check if BLUE is enabled.
290 findkey(kv
, "BLUE_DEV", if_blue
);
291 if (VALID_DEVICE(if_blue
))
296 // exit if nothing to do
297 if ((enable_red
+enable_green
+enable_orange
+enable_blue
) == 0)
302 open_physical(if_red
, 4500);
304 if (enable_green
> 0)
305 open_physical(if_green
, 4500);
307 if (enable_orange
> 0)
308 open_physical(if_orange
, 4500);
311 open_physical(if_blue
, 4500);
314 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
315 safe_system("/usr/sbin/ipsec restart >/dev/null");
319 // it is a selective start or stop
320 // second param is only a number 'key'
321 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
322 fprintf(stderr
, "Bad arg: %s\n", argv
[2]);
327 // search the vpn pointed by 'key'
328 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
329 fprintf(stderr
, "Couldn't open vpn settings file");
332 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
336 if (!decode_line(s
,&key
,&name
,&type
))
339 // is it the 'key' requested ?
340 if (strcmp(argv
[2], key
) != 0)
343 // Start or Delete this Connection
344 if (strcmp(argv
[1], "S") == 0)
345 turn_connection_on (name
, type
);
346 else if (strcmp(argv
[1], "D") == 0)
347 turn_connection_off (name
);
349 fprintf(stderr
, "Bad command\n");