diff --git a/openconnect-internal.h b/openconnect-internal.h index d63c9bef..e5700c96 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -451,11 +451,11 @@ int win32_setup_tun(struct openconnect_info *vpninfo); /* script.c */ int setenv_int(const char *opt, int value); void set_script_env(struct openconnect_info *vpninfo); +int script_config_tun(struct openconnect_info *vpninfo, const char *reason); /* tun.c */ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout); void shutdown_tun(struct openconnect_info *vpninfo); -int script_config_tun(struct openconnect_info *vpninfo, const char *reason); /* dtls.c */ unsigned char unhex(const char *data); diff --git a/script.c b/script.c index 89baa5bf..34a1a722 100644 --- a/script.c +++ b/script.c @@ -304,3 +304,45 @@ void set_script_env(struct openconnect_info *vpninfo) setenv_cstp_opts(vpninfo); } +int script_config_tun(struct openconnect_info *vpninfo, const char *reason) +{ + int ret; + + if (!vpninfo->vpnc_script || vpninfo->script_tun) + return 0; + + setenv("reason", reason, 1); + ret = system(vpninfo->vpnc_script); + if (ret == -1) { + int e = errno; + vpn_progress(vpninfo, PRG_ERR, + _("Failed to spawn script '%s' for %s: %s\n"), + vpninfo->vpnc_script, reason, strerror(e)); + return -e; + } +#ifdef _WIN32 + if (ret == 0x2331) { + /* This is what cmd.exe returns for unrecognised commands */ + vpn_progress(vpninfo, PRG_ERR, + _("Failed to spawn script '%s' for %s: %s\n"), + vpninfo->vpnc_script, reason, strerror(ENOENT)); + return -ENOENT; + } +#else + if (!WIFEXITED(ret)) { + vpn_progress(vpninfo, PRG_ERR, + _("Script '%s' exited abnormally (%x)\n"), + vpninfo->vpnc_script, ret); + return -EIO; + } + + ret = WEXITSTATUS(ret); +#endif + if (ret) { + vpn_progress(vpninfo, PRG_ERR, + _("Script '%s' returned error %d\n"), + vpninfo->vpnc_script, ret); + return -EIO; + } + return 0; +} diff --git a/tun.c b/tun.c index b663874e..c3e83baf 100644 --- a/tun.c +++ b/tun.c @@ -91,48 +91,6 @@ static int set_tun_mtu(struct openconnect_info *vpninfo) #endif return 0; } -int script_config_tun(struct openconnect_info *vpninfo, const char *reason) -{ - int ret; - - if (!vpninfo->vpnc_script || vpninfo->script_tun) - return 0; - - setenv("reason", reason, 1); - ret = system(vpninfo->vpnc_script); - if (ret == -1) { - int e = errno; - vpn_progress(vpninfo, PRG_ERR, - _("Failed to spawn script '%s' for %s: %s\n"), - vpninfo->vpnc_script, reason, strerror(e)); - return -e; - } -#ifdef _WIN32 - if (ret == 0x2331) { - /* This is what cmd.exe returns for unrecognised commands */ - vpn_progress(vpninfo, PRG_ERR, - _("Failed to spawn script '%s' for %s: %s\n"), - vpninfo->vpnc_script, reason, strerror(ENOENT)); - return -ENOENT; - } -#else - if (!WIFEXITED(ret)) { - vpn_progress(vpninfo, PRG_ERR, - _("Script '%s' exited abnormally (%x)\n"), - vpninfo->vpnc_script, ret); - return -EIO; - } - - ret = WEXITSTATUS(ret); -#endif - if (ret) { - vpn_progress(vpninfo, PRG_ERR, - _("Script '%s' returned error %d\n"), - vpninfo->vpnc_script, ret); - return -EIO; - } - return 0; -} #ifdef __sun__ static int link_proto(int unit_nr, const char *devname, uint64_t flags)