Skip to content

Commit a1f8641

Browse files
da-xDimitriPapadopoulos
authored andcommitted
Support a script to be executed when the device goes up
This adds the `ifup_script` config option. The script receives the following environment variables as input: - NET_DEVICE: The name of the network device of the VPN. - DNS_SUFFIX: DNS domain search prefix, if provided by the VPN server. - DNS_SERVERS: A list of the DNS server addresses if provided by the VPN server.
1 parent 0141147 commit a1f8641

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ int load_config(struct vpn_config *cfg, const char *filename)
288288
} else if (strcmp(key, "realm") == 0) {
289289
strncpy(cfg->realm, val, REALM_SIZE);
290290
cfg->realm[REALM_SIZE] = '\0';
291+
} else if (strcmp(key, "ifup-script") == 0) {
292+
strncpy(cfg->ifup_script, val, MAXPATHLEN - 1);
293+
cfg->ifup_script[MAXPATHLEN] = '\0';
291294
} else if (strcmp(key, "set-dns") == 0) {
292295
int set_dns = strtob(val);
293296

src/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef OPENFORTIVPN_CONFIG_H
1919
#define OPENFORTIVPN_CONFIG_H
2020

21+
#include <sys/param.h>
2122
#include <netinet/in.h>
2223
#include <net/if.h>
2324

@@ -97,6 +98,7 @@ struct vpn_config {
9798
char *pinentry;
9899
char iface_name[IF_NAMESIZE];
99100
char realm[REALM_SIZE + 1];
101+
char ifup_script[MAXPATHLEN + 1];
100102

101103
int set_routes;
102104
int set_dns;

src/tunnel.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,39 @@ static int ofv_append_varr(struct ofv_varr *p, const char *x)
105105
return 0;
106106
}
107107

108+
static int ipv4_run_ifup_script(struct tunnel *tunnel)
109+
{
110+
char ns[32];
111+
112+
setenv("NET_DEVICE", tunnel->ppp_iface, 0);
113+
114+
ns[0] = '\0';
115+
116+
if (tunnel->ipv4.ns1_addr.s_addr != 0)
117+
strncat(ns, inet_ntoa(tunnel->ipv4.ns1_addr), 15);
118+
119+
if (tunnel->ipv4.ns2_addr.s_addr != 0) {
120+
strcpy(ns, " ");
121+
strncat(ns, inet_ntoa(tunnel->ipv4.ns2_addr), 15);
122+
}
123+
124+
setenv("DNS_SERVERS", ns, 0);
125+
126+
if (tunnel->ipv4.dns_suffix != NULL)
127+
setenv("DNS_SUFFIX", tunnel->ipv4.dns_suffix, 0);
128+
else
129+
setenv("DNS_SUFFIX", "", 0);
130+
131+
return system(tunnel->config->ifup_script);
132+
}
133+
108134
static int on_ppp_if_up(struct tunnel *tunnel)
109135
{
136+
int ret;
137+
110138
log_info("Interface %s is UP.\n", tunnel->ppp_iface);
111139

112140
if (tunnel->config->set_routes) {
113-
int ret;
114-
115141
log_info("Setting new routes...\n");
116142

117143
ret = ipv4_set_tunnel_routes(tunnel);
@@ -125,6 +151,13 @@ static int on_ppp_if_up(struct tunnel *tunnel)
125151
ipv4_add_nameservers_to_resolv_conf(tunnel);
126152
}
127153

154+
if (tunnel->config->ifup_script) {
155+
log_info("Running `ifup` script...\n");
156+
ret = ipv4_run_ifup_script(tunnel);
157+
if (ret != 0)
158+
log_warn("The `ifup` script failed. Please check your logs.\n");
159+
}
160+
128161
log_info("Tunnel is up and running.\n");
129162

130163
#if HAVE_SYSTEMD

0 commit comments

Comments
 (0)