Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into strongswan-next
[people/teissler/ipfire-2.x.git] / src / misc-progs / ipsecctrl.c
1 /*
2 *
3 * File originally from the Smoothwall project
4 * (c) 2001 Smoothwall Team
5 *
6 */
7
8 #include "libsmooth.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <signal.h>
16 #include "setuid.h"
17
18 /*
19 This module is responsible for start stop of the vpn system.
20
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.
23
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
27 other interface.
28 Unfortunatly, openswan 1 cannot do that correctly. It cannot use an
29 interface without restarting everything.
30
31 */
32
33 void usage() {
34 fprintf (stderr, "Usage:\n");
35 fprintf (stderr, "\tipsecctrl S [connectionkey]\n");
36 fprintf (stderr, "\tipsecctrl D [connectionkey]\n");
37 fprintf (stderr, "\tipsecctrl R\n");
38 fprintf (stderr, "\tipsecctrl I\n");
39 fprintf (stderr, "\t\tS : Start/Restart Connection\n");
40 fprintf (stderr, "\t\tD : Stop Connection\n");
41 fprintf (stderr, "\t\tR : Reload Certificates and Secrets\n");
42 fprintf (stderr, "\t\tI : Print Statusinfo\n");
43 }
44
45 /*
46 ACCEPT the ipsec protocol ah, esp & udp (for nat traversal) on the specified interface
47 */
48 void open_physical (char *interface, int nat_traversal_port) {
49 char str[STRING_SIZE];
50
51 // GRE ???
52 // sprintf(str, "/sbin/iptables -A " phystable " -p 47 -i %s -j ACCEPT", interface);
53 // safe_system(str);
54 // ESP
55 // sprintf(str, "/sbin/iptables -A " phystable " -p 50 -i %s -j ACCEPT", interface);
56 // safe_system(str);
57 // AH
58 // sprintf(str, "/sbin/iptables -A " phystable " -p 51 -i %s -j ACCEPT", interface);
59 // safe_system(str);
60 // IKE
61
62 sprintf(str, "/sbin/iptables -D IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT >/dev/null 2>&1", interface);
63 safe_system(str);
64 sprintf(str, "/sbin/iptables -A IPSECINPUT -p udp -i %s --dport 500 -j ACCEPT", interface);
65 safe_system(str);
66
67 if (! nat_traversal_port)
68 return;
69
70 sprintf(str, "/sbin/iptables -D IPSECINPUT -p udp -i %s --dport %i -j ACCEPT >/dev/null 2>&1", interface, nat_traversal_port);
71 safe_system(str);
72 sprintf(str, "/sbin/iptables -A IPSECINPUT -p udp -i %s --dport %i -j ACCEPT", interface, nat_traversal_port);
73 safe_system(str);
74 }
75
76 void ipsec_norules() {
77 /* clear input rules */
78 safe_system("/sbin/iptables -F IPSECINPUT");
79 safe_system("/sbin/iptables -F IPSECFORWARD");
80 safe_system("/sbin/iptables -F IPSECOUTPUT");
81 }
82
83 /*
84 return values from the vpn config file or false if not 'on'
85 */
86 int decode_line (char *s,
87 char **key,
88 char **name,
89 char **type
90 ) {
91 int count = 0;
92 *key = NULL;
93 *name = NULL;
94 *type = NULL;
95
96 if (s[strlen(s) - 1] == '\n')
97 s[strlen(s) - 1] = '\0';
98
99 char *result = strsep(&s, ",");
100 while (result) {
101 if (count == 0)
102 *key = result;
103 if ((count == 1) && strcmp(result, "on") != 0)
104 return 0; // a disabled line
105 if (count == 2)
106 *name = result;
107 if (count == 4)
108 *type = result;
109 count++;
110 result = strsep(&s, ",");
111 }
112
113 // check other syntax
114 if (! *name)
115 return 0;
116
117 if (strspn(*name, LETTERS_NUMBERS) != strlen(*name)) {
118 fprintf(stderr, "Bad connection name: %s\n", *name);
119 return 0;
120 }
121
122 if (! (strcmp(*type, "host") == 0 || strcmp(*type, "net") == 0)) {
123 fprintf(stderr, "Bad connection type: %s\n", *type);
124 return 0;
125 }
126
127 //it's a valid & active line
128 return 1;
129 }
130
131 /*
132 issue ipsec commmands to turn on connection 'name'
133 */
134 void turn_connection_on(char *name, char *type) {
135 /*
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.
139 */
140 char command[STRING_SIZE];
141
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);
146
147 // Reload the configuration into the daemon (#10339).
148 ipsec_reload();
149
150 // Bring the connection up again.
151 snprintf(command, STRING_SIZE - 1,
152 "/usr/sbin/ipsec up %s >/dev/null", name);
153 safe_system(command);
154 }
155
156 /*
157 issue ipsec commmands to turn off connection 'name'
158 */
159 void turn_connection_off (char *name) {
160 /*
161 * To turn off a connection, all SAs must be turned down.
162 * After that, the configuration must be reloaded.
163 */
164 char command[STRING_SIZE];
165
166 // Bring down the connection.
167 snprintf(command, STRING_SIZE - 1,
168 "/usr/sbin/ipsec down %s >/dev/null", name);
169 safe_system(command);
170
171 // Reload, so the connection is dropped.
172 ipsec_reload();
173 }
174
175 void ipsec_reload() {
176 /* Re-read all configuration files and secrets and
177 * reload the daemon (#10339).
178 */
179 safe_system("/usr/sbin/ipsec rereadall >/dev/null 2>&1");
180 safe_system("/usr/sbin/ipsec reload >/dev/null 2>&1");
181 }
182
183 int main(int argc, char *argv[]) {
184 char configtype[STRING_SIZE];
185 char redtype[STRING_SIZE] = "";
186 struct keyvalue *kv = NULL;
187
188 if (argc < 2) {
189 usage();
190 exit(1);
191 }
192 if (!(initsetuid()))
193 exit(1);
194
195 FILE *file = NULL;
196
197
198 if (strcmp(argv[1], "I") == 0) {
199 safe_system("/usr/sbin/ipsec status");
200 exit(0);
201 }
202
203 if (strcmp(argv[1], "R") == 0) {
204 ipsec_reload();
205 exit(0);
206 }
207
208 /* FIXME: workaround for pclose() issue - still no real idea why
209 * this is happening */
210 signal(SIGCHLD, SIG_DFL);
211
212 /* handle operations that doesn't need start the ipsec system */
213 if (argc == 2) {
214 if (strcmp(argv[1], "D") == 0) {
215 safe_system("/usr/sbin/ipsec stop >/dev/null 2>&1");
216 ipsec_norules();
217 exit(0);
218 }
219 }
220
221 /* read vpn config */
222 kv=initkeyvalues();
223 if (!readkeyvalues(kv, CONFIG_ROOT "/vpn/settings"))
224 {
225 fprintf(stderr, "Cannot read vpn settings\n");
226 exit(1);
227 }
228
229 /* check is the vpn system is enabled */
230 {
231 char s[STRING_SIZE];
232 findkey(kv, "ENABLED", s);
233 freekeyvalues(kv);
234 if (strcmp (s, "on") != 0)
235 exit(0);
236 }
237
238 /* read interface settings */
239 kv=initkeyvalues();
240 if (!readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))
241 {
242 fprintf(stderr, "Cannot read ethernet settings\n");
243 exit(1);
244 }
245 if (!findkey(kv, "CONFIG_TYPE", configtype))
246 {
247 fprintf(stderr, "Cannot read CONFIG_TYPE\n");
248 exit(1);
249 }
250 findkey(kv, "RED_TYPE", redtype);
251
252
253 /* Loop through the config file to find physical interface that will accept IPSEC */
254 int enable_red=0; // states 0: not used
255 int enable_green=0; // 1: error condition
256 int enable_orange=0; // 2: good
257 int enable_blue=0;
258 char if_red[STRING_SIZE] = "";
259 char if_green[STRING_SIZE] = "";
260 char if_orange[STRING_SIZE] = "";
261 char if_blue[STRING_SIZE] = "";
262 char s[STRING_SIZE];
263
264 // when RED is up, find interface name in special file
265 FILE *ifacefile = NULL;
266 if ((ifacefile = fopen(CONFIG_ROOT "/red/iface", "r"))) {
267 if (fgets(if_red, STRING_SIZE, ifacefile)) {
268 if (if_red[strlen(if_red) - 1] == '\n')
269 if_red[strlen(if_red) - 1] = '\0';
270 }
271 fclose (ifacefile);
272
273 if (VALID_DEVICE(if_red))
274 enable_red++;
275 }
276
277 // Check if GREEN is enabled.
278 findkey(kv, "GREEN_DEV", if_green);
279 if (VALID_DEVICE(if_green))
280 enable_green++;
281
282 // Check if ORANGE is enabled.
283 findkey(kv, "ORANGE_DEV", if_orange);
284 if (VALID_DEVICE(if_orange))
285 enable_orange++;
286
287 // Check if BLUE is enabled.
288 findkey(kv, "BLUE_DEV", if_blue);
289 if (VALID_DEVICE(if_blue))
290 enable_blue++;
291
292 freekeyvalues(kv);
293
294 // exit if nothing to do
295 if ((enable_red+enable_green+enable_orange+enable_blue) == 0)
296 exit(0);
297
298 // open needed ports
299 if (enable_red > 0)
300 open_physical(if_red, 4500);
301
302 if (enable_green > 0)
303 open_physical(if_green, 4500);
304
305 if (enable_orange > 0)
306 open_physical(if_orange, 4500);
307
308 if (enable_blue > 0)
309 open_physical(if_blue, 4500);
310
311 // start the system
312 if ((argc == 2) && strcmp(argv[1], "S") == 0) {
313 safe_system("/usr/sbin/ipsec restart >/dev/null");
314 exit(0);
315 }
316
317 // it is a selective start or stop
318 // second param is only a number 'key'
319 if ((argc == 2) || strspn(argv[2], NUMBERS) != strlen(argv[2])) {
320 fprintf(stderr, "Bad arg: %s\n", argv[2]);
321 usage();
322 exit(1);
323 }
324
325 // search the vpn pointed by 'key'
326 if (!(file = fopen(CONFIG_ROOT "/vpn/config", "r"))) {
327 fprintf(stderr, "Couldn't open vpn settings file");
328 exit(1);
329 }
330 while (fgets(s, STRING_SIZE, file) != NULL) {
331 char *key;
332 char *name;
333 char *type;
334 if (!decode_line(s,&key,&name,&type))
335 continue;
336
337 // is it the 'key' requested ?
338 if (strcmp(argv[2], key) != 0)
339 continue;
340
341 // Start or Delete this Connection
342 if (strcmp(argv[1], "S") == 0)
343 turn_connection_on (name, type);
344 else if (strcmp(argv[1], "D") == 0)
345 turn_connection_off (name);
346 else {
347 fprintf(stderr, "Bad command\n");
348 exit(1);
349 }
350 }
351 fclose(file);
352
353 return 0;
354 }