]>
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");
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 return values from the vpn config file or false if not 'on'
58 int decode_line (char *s
,
68 if (s
[strlen(s
) - 1] == '\n')
69 s
[strlen(s
) - 1] = '\0';
71 char *result
= strsep(&s
, ",");
75 if ((count
== 1) && strcmp(result
, "on") != 0)
76 return 0; // a disabled line
82 result
= strsep(&s
, ",");
89 if (strspn(*name
, LETTERS_NUMBERS
) != strlen(*name
)) {
90 fprintf(stderr
, "Bad connection name: %s\n", *name
);
94 if (! (strcmp(*type
, "host") == 0 || strcmp(*type
, "net") == 0)) {
95 fprintf(stderr
, "Bad connection type: %s\n", *type
);
99 //it's a valid & active line
104 issue ipsec commmands to turn on connection 'name'
106 void turn_connection_on(char *name
, char *type
) {
108 * To bring up a connection, we need to reload the configuration
109 * and issue ipsec up afterwards. To make sure the connection
110 * is not established from the start, we bring it down in advance.
112 char command
[STRING_SIZE
];
114 // Bring down the connection (if established).
115 snprintf(command
, STRING_SIZE
- 1,
116 "/usr/sbin/ipsec down %s >/dev/null", name
);
117 safe_system(command
);
119 // Reload the IPsec firewall policy
120 safe_system("/usr/lib/firewall/ipsec-policy >/dev/null");
122 // Create or destroy interfaces
123 safe_system("/usr/local/bin/ipsec-interfaces >/dev/null");
125 // Reload the configuration into the daemon (#10339).
128 // Bring the connection up again.
129 snprintf(command
, STRING_SIZE
- 1,
130 "/usr/sbin/ipsec stroke up-nb %s >/dev/null", name
);
131 safe_system(command
);
135 issue ipsec commmands to turn off connection 'name'
137 void turn_connection_off (char *name
) {
139 * To turn off a connection, all SAs must be turned down.
140 * After that, the configuration must be reloaded.
142 char command
[STRING_SIZE
];
144 // Reload, so the connection is dropped.
147 // Bring down the connection.
148 snprintf(command
, STRING_SIZE
- 1,
149 "/usr/sbin/ipsec down %s >/dev/null", name
);
150 safe_system(command
);
152 // Reload the IPsec firewall policy
153 safe_system("/usr/lib/firewall/ipsec-policy >/dev/null");
155 // Create or destroy interfaces
156 safe_system("/usr/local/bin/ipsec-interfaces >/dev/null");
159 int main(int argc
, char *argv
[]) {
160 struct keyvalue
*kv
= NULL
;
171 if (strcmp(argv
[1], "I") == 0) {
172 safe_system("/usr/sbin/ipsec status");
176 if (strcmp(argv
[1], "R") == 0) {
181 /* FIXME: workaround for pclose() issue - still no real idea why
182 * this is happening */
183 signal(SIGCHLD
, SIG_DFL
);
185 /* handle operations that doesn't need start the ipsec system */
187 if (strcmp(argv
[1], "D") == 0) {
188 safe_system("/usr/sbin/ipsec stop >/dev/null 2>&1");
189 safe_system("/usr/lib/firewall/ipsec-policy >/dev/null");
190 safe_system("/usr/local/bin/ipsec-interfaces >/dev/null");
195 /* read vpn config */
197 if (!readkeyvalues(kv
, CONFIG_ROOT
"/vpn/settings"))
199 fprintf(stderr
, "Cannot read vpn settings\n");
203 /* check is the vpn system is enabled */
206 findkey(kv
, "ENABLED", s
);
208 if (strcmp (s
, "on") != 0)
215 if ((argc
== 2) && strcmp(argv
[1], "S") == 0) {
216 safe_system("/usr/lib/firewall/ipsec-policy >/dev/null");
217 safe_system("/usr/local/bin/ipsec-interfaces >/dev/null");
218 safe_system("/usr/sbin/ipsec restart >/dev/null");
222 // it is a selective start or stop
223 // second param is only a number 'key'
224 if ((argc
== 2) || strspn(argv
[2], NUMBERS
) != strlen(argv
[2])) {
225 fprintf(stderr
, "Bad arg: %s\n", argv
[2]);
230 // search the vpn pointed by 'key'
231 if (!(file
= fopen(CONFIG_ROOT
"/vpn/config", "r"))) {
232 fprintf(stderr
, "Couldn't open vpn settings file");
235 while (fgets(s
, STRING_SIZE
, file
) != NULL
) {
239 if (!decode_line(s
,&key
,&name
,&type
))
242 // is it the 'key' requested ?
243 if (strcmp(argv
[2], key
) != 0)
246 // Start or Delete this Connection
247 if (strcmp(argv
[1], "S") == 0)
248 turn_connection_on (name
, type
);
249 else if (strcmp(argv
[1], "D") == 0)
250 turn_connection_off (name
);
252 fprintf(stderr
, "Bad command\n");