Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks for when link goes up and down #986

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ int load_config(struct vpn_config *cfg, const char *filename)
} else if (strcmp(key, "check-virtual-desktop") == 0) {
free(cfg->check_virtual_desktop);
cfg->check_virtual_desktop = strdup(val);
} else if (strcmp(key, "up-hook") == 0) {
free(cfg->up_hook);
cfg->up_hook = strdup(val);
} else if (strcmp(key, "down-hook") == 0) {
free(cfg->down_hook);
cfg->down_hook = strdup(val);
} else {
log_warn("Bad key in configuration file: \"%s\".\n", key);
goto err_free;
Expand Down Expand Up @@ -600,4 +606,12 @@ void merge_config(struct vpn_config *dst, struct vpn_config *src)
dst->hostcheck = src->hostcheck;
if (src->check_virtual_desktop != invalid_cfg.check_virtual_desktop)
dst->check_virtual_desktop = src->check_virtual_desktop;
if (src->up_hook) {
free(dst->up_hook);
dst->up_hook = src->up_hook;
}
if (src->down_hook) {
free(dst->down_hook);
dst->down_hook = src->down_hook;
}
}
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ struct vpn_config {
char *user_agent;
char *hostcheck;
char *check_virtual_desktop;
char *up_hook;
char *down_hook;
};

int add_trusted_cert(struct vpn_config *cfg, const char *digest);
Expand Down
23 changes: 21 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ PPPD_USAGE \
" " RESOLVCONF_USAGE "[--ca-file=<file>]\n" \
" [--user-cert=<file>] [--user-key=<file>]\n" \
" [--use-syslog] [--trusted-cert=<digest>]\n" \
" [--persistent=<interval>] [-c <file>] [-v|-q]\n" \
" [--persistent=<interval>] [--up-hook=<file>]\n"\
" [--down-hook=<file>] [-c <file>] [-v|-q]\n" \
" openfortivpn --help\n" \
" openfortivpn --version\n" \
"\n"
Expand Down Expand Up @@ -143,7 +144,11 @@ PPPD_USAGE \
" certificate will be matched against this value.\n" \
" <digest> is the X509 certificate's sha256 sum.\n" \
" This option can be used multiple times to trust\n" \
" several certificates.\n"
" several certificates.\n" \
" --up-hook=<file> Run this script when the link goes up, and wait for\n" \
" completion.\n" \
" --down-hook=<file> Run this script when the link goes down, and wait\n" \
" for completion.\n"

#define help_options_part2 \
" --insecure-ssl Do not disable insecure SSL protocols/ciphers.\n" \
Expand Down Expand Up @@ -238,6 +243,8 @@ int main(int argc, char **argv)
.cert_whitelist = NULL,
.use_engine = 0,
.user_agent = NULL,
.up_hook = NULL,
.down_hook = NULL,
};
struct vpn_config cli_cfg = invalid_cfg;

Expand Down Expand Up @@ -270,6 +277,8 @@ int main(int argc, char **argv)
{"cipher-list", required_argument, NULL, 0},
{"min-tls", required_argument, NULL, 0},
{"seclevel-1", no_argument, &cli_cfg.seclevel_1, 1},
{"up-hook", required_argument, NULL, 0},
{"down-hook", required_argument, NULL, 0},
#if HAVE_USR_SBIN_PPPD
{"pppd-use-peerdns", required_argument, NULL, 0},
{"pppd-no-peerdns", no_argument, &cli_cfg.pppd_use_peerdns, 0},
Expand Down Expand Up @@ -509,6 +518,16 @@ int main(int argc, char **argv)
cli_cfg.set_dns = set_dns;
break;
}
if (strcmp(long_options[option_index].name,
"up-hook") == 0) {
cli_cfg.up_hook = strdup(optarg);
break;
}
if (strcmp(long_options[option_index].name,
"down-hook") == 0) {
cli_cfg.down_hook = strdup(optarg);
break;
}
goto user_error;
case 'h':
printf("%s%s%s%s%s%s%s", usage, summary,
Expand Down
12 changes: 12 additions & 0 deletions src/tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ static int ofv_append_varr(struct ofv_varr *p, const char *x)
return 0;
}

static void run_hook(const char* hook) {
log_info("Running hook %s.\n", hook);
system(hook);
}

static int on_ppp_if_up(struct tunnel *tunnel)
{
log_info("Interface %s is UP.\n", tunnel->ppp_iface);
Expand All @@ -133,6 +138,9 @@ static int on_ppp_if_up(struct tunnel *tunnel)
#if HAVE_SYSTEMD
sd_notify(0, "READY=1");
#endif
if (tunnel->config->up_hook) {
run_hook(tunnel->config->up_hook);
}

return 0;
}
Expand All @@ -155,6 +163,10 @@ static int on_ppp_if_down(struct tunnel *tunnel)
ipv4_del_nameservers_from_resolv_conf(tunnel);
}

if (tunnel->config->down_hook) {
run_hook(tunnel->config->down_hook);
}

return 0;
}

Expand Down