Skip to content

Commit

Permalink
Clean up default vpnc-script handling for Windows
Browse files Browse the repository at this point in the history
We automatically prepend the current executable's path to the script
name if it's relative, and run it in 'cscript.exe'.

Perhaps it would be nicer if we determined whether the path was relative
or not in autoconf, rather than at runtime.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 13, 2014
1 parent 929d88d commit 18115d5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
44 changes: 26 additions & 18 deletions configure.ac
Expand Up @@ -72,27 +72,35 @@ AC_ARG_WITH([vpnc-script],
[default location of vpnc-script helper])])

if test "$with_vpnc_script" = "yes" || test "$with_vpnc_script" = ""; then
with_vpnc_script=/etc/vpnc/vpnc-script
if ! test -x "$with_vpnc_script"; then
AC_MSG_ERROR([${with_vpnc_script} does not seem to be executable.]
[OpenConnect will not function correctly without a vpnc-script.]
[See http://www.infradead.org/openconnect/vpnc-script.html for more details.]
[]
[If you are building a distribution package, please ensure that your]
[packaging is correct, and that a vpnc-script will be installed when the]
[user installs your package. You should provide a --with-vpnc-script=]
[argument to this configure script, giving the full path where the script]
[will be installed.]
[]
[The standard location is ${with_vpnc_script}. To bypass this error and]
[build OpenConnect to use the script from this location, even though it's]
[not present at the time you are building OpenConnect, pass the argument]
["--with-vpnc-script=${with_vpnc_script}"])
fi
if test "$have_win" = "yes"; then
with_vpnc_script=vpnc-script-win.js
else
with_vpnc_script=/etc/vpnc/vpnc-script
if ! test -x "$with_vpnc_script"; then
AC_MSG_ERROR([${with_vpnc_script} does not seem to be executable.]
[OpenConnect will not function correctly without a vpnc-script.]
[See http://www.infradead.org/openconnect/vpnc-script.html for more details.]
[]
[If you are building a distribution package, please ensure that your]
[packaging is correct, and that a vpnc-script will be installed when the]
[user installs your package. You should provide a --with-vpnc-script=]
[argument to this configure script, giving the full path where the script]
[will be installed.]
[]
[The standard location is ${with_vpnc_script}. To bypass this error and]
[build OpenConnect to use the script from this location, even though it's]
[not present at the time you are building OpenConnect, pass the argument]
["--with-vpnc-script=${with_vpnc_script}"])
fi
fi
elif test "$with_vpnc_script" = "no"; then
AC_ERROR([You cannot disable vpnc-script.]
[OpenConnect will not function correctly without it.]
[See http://www.infradead.org/openconnect/vpnc-script.html])
elif test "$have_win" = "yes"; then
# Oh Windows how we hate thee. If user specifies a vpnc-script and it contains
# backslashes, double them all up to survive escaping.
with_vpnc_script="$(echo "${with_vpnc_script}" | sed s/\\\\/\\\\\\\\/g)"
fi

AC_DEFINE_UNQUOTED(DEFAULT_VPNCSCRIPT, "${with_vpnc_script}")
Expand Down Expand Up @@ -149,7 +157,7 @@ AC_SUBST(WFLAGS, [$WFLAGS])

if test "$have_win" = yes; then
# Checking "properly" for __attribute__((dllimport,stdcall)) functions is non-trivial
LIBS="$LIBS -lws2_32"
LIBS="$LIBS -lws2_32 -lshlwapi"
else
AC_CHECK_FUNC(socket, [], AC_CHECK_LIB(socket, socket, [], AC_ERROR(Cannot find socket() function)))
fi
Expand Down
33 changes: 30 additions & 3 deletions main.c
Expand Up @@ -288,6 +288,31 @@ static void print_build_opts(void)
#endif
}

#ifndef _WIN32
static const char default_vpncscript[] = DEFAULT_VPNCSCRIPT;
#else
#include <shlwapi.h>
static const char *default_vpncscript;
static void set_default_vpncscript(void)
{
if (PathIsRelative(DEFAULT_VPNCSCRIPT)) {
char *c = strrchr(_pgmptr, '\\');
if (!c) {
fprintf(stderr, _("Cannot process this executable path \"%s\""),
_pgmptr);
exit(1);
}
if (asprintf((char **)&default_vpncscript, "cscript %.*s%s",
(c - _pgmptr + 1), _pgmptr, DEFAULT_VPNCSCRIPT) < 0) {
fprintf(stderr, _("Allocation for vpnc-script path failed\n"));
exit(1);
}
} else {
default_vpncscript = "cscript " DEFAULT_VPNCSCRIPT;
}
}
#endif

static void usage(void)
{
printf(_("Usage: openconnect [options] <server>\n"));
Expand Down Expand Up @@ -332,7 +357,7 @@ static void usage(void)
printf(" -q, --quiet %s\n", _("Less output"));
printf(" -Q, --queue-len=LEN %s\n", _("Set packet queue limit to LEN pkts"));
printf(" -s, --script=SCRIPT %s\n", _("Shell command line for using a vpnc-compatible config script"));
printf(" %s: \"%s\"\n", _("default"), DEFAULT_VPNCSCRIPT);
printf(" %s: \"%s\"\n", _("default"), default_vpncscript);
#ifndef _WIN32
printf(" -S, --script-tun %s\n", _("Pass traffic to 'script' program, not tun"));
#endif
Expand Down Expand Up @@ -610,7 +635,9 @@ int main(int argc, char **argv)
}

vpninfo->cbdata = vpninfo;
#ifndef _WIN32
#ifdef _WIN32
set_default_vpncscript();
#else
if (!uname(&utsbuf)) {
free(vpninfo->localname);
vpninfo->localname = xstrdup(utsbuf.nodename);
Expand Down Expand Up @@ -1005,7 +1032,7 @@ int main(int argc, char **argv)
}

if (!vpnc_script)
vpnc_script = xstrdup(DEFAULT_VPNCSCRIPT);
vpnc_script = xstrdup(default_vpncscript);
#ifndef _WIN32
if (script_tun) {
if (openconnect_setup_tun_script(vpninfo, vpnc_script)) {
Expand Down
10 changes: 7 additions & 3 deletions openconnect.8.in
Expand Up @@ -230,9 +230,13 @@ compatible with the
.B vpnc\-script
which is shipped with the "vpnc" VPN client. See
.I http://www.infradead.org/openconnect/vpnc-script.html
for more information. This version of OpenConnect is configured to use
.B @DEFAULT_VPNCSCRIPT@
by default.
for more information. This version of OpenConnect is configured to
use \fB@DEFAULT_VPNCSCRIPT@\fR by default.

On Windows, a relative directory for the default script will be handled as
starting from the directory that the openconnect executable is running from,
rather than the current directory. The script will be invoked with the
command-based script host \fBcscript.exe\fR.
.TP
.B \-S,\-\-script\-tun
Pass traffic to 'script' program over a UNIX socket, instead of to a kernel
Expand Down

0 comments on commit 18115d5

Please sign in to comment.