Skip to content

Commit

Permalink
Support a script to be executed when the device goes up
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
da-x authored and DimitriPapadopoulos committed Nov 6, 2023
1 parent 505af3a commit 1398fa1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ int load_config(struct vpn_config *cfg, const char *filename)
} else if (strcmp(key, "realm") == 0) {
strncpy(cfg->realm, val, REALM_SIZE);
cfg->realm[REALM_SIZE] = '\0';
} else if (strcmp(key, "ifup-script") == 0) {
strncpy(cfg->ifup_script, val, MAXPATHLEN - 1);
cfg->ifup_script[MAXPATHLEN] = '\0';
} else if (strcmp(key, "set-dns") == 0) {
int set_dns = strtob(val);

Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef OPENFORTIVPN_CONFIG_H
#define OPENFORTIVPN_CONFIG_H

#include <sys/param.h>
#include <netinet/in.h>
#include <net/if.h>

Expand Down Expand Up @@ -97,6 +98,7 @@ struct vpn_config {
char *pinentry;
char iface_name[IF_NAMESIZE];
char realm[REALM_SIZE + 1];
char ifup_script[MAXPATHLEN + 1];

int set_routes;
int set_dns;
Expand Down
37 changes: 35 additions & 2 deletions src/tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,39 @@ static int ofv_append_varr(struct ofv_varr *p, const char *x)
return 0;
}

static int ipv4_run_ifup_script(struct tunnel *tunnel)
{
char ns[32];

setenv("NET_DEVICE", tunnel->ppp_iface, 0);

ns[0] = '\0';

if (tunnel->ipv4.ns1_addr.s_addr != 0)
strncat(ns, inet_ntoa(tunnel->ipv4.ns1_addr), 15);

if (tunnel->ipv4.ns2_addr.s_addr != 0) {
strcpy(ns, " ");
strncat(ns, inet_ntoa(tunnel->ipv4.ns2_addr), 15);
}

setenv("DNS_SERVERS", ns, 0);

if (tunnel->ipv4.dns_suffix != NULL)
setenv("DNS_SUFFIX", tunnel->ipv4.dns_suffix, 0);
else
setenv("DNS_SUFFIX", "", 0);

return system(tunnel->config->ifup_script);
}

static int on_ppp_if_up(struct tunnel *tunnel)
{
int ret;

log_info("Interface %s is UP.\n", tunnel->ppp_iface);

if (tunnel->config->set_routes) {
int ret;

log_info("Setting new routes...\n");

ret = ipv4_set_tunnel_routes(tunnel);
Expand All @@ -125,6 +151,13 @@ static int on_ppp_if_up(struct tunnel *tunnel)
ipv4_add_nameservers_to_resolv_conf(tunnel);
}

if (tunnel->config->ifup_script) {
log_info("Running `ifup` script...\n");
ret = ipv4_run_ifup_script(tunnel);
if (ret != 0)
log_warn("The `ifup` script failed. Please check your logs.\n");
}

log_info("Tunnel is up and running.\n");

#if HAVE_SYSTEMD
Expand Down

0 comments on commit 1398fa1

Please sign in to comment.