tests: introduced test program to check basic vhost functionality

This checks whether connecting to different virtual hosts
with different authentication methods works.

Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
This commit is contained in:
Nikos Mavrogiannopoulos
2018-02-11 22:07:05 +01:00
parent ba6921ed9a
commit 5d0205332d
8 changed files with 407 additions and 10 deletions

View File

@@ -235,12 +235,14 @@ fi
PKG_CHECK_MODULES([CWRAP], [uid_wrapper, socket_wrapper], have_cwrap=yes, have_cwrap=no)
PKG_CHECK_MODULES([CWRAP_PAM], [pam_wrapper], have_cwrap_pam=yes, have_cwrap_pam=no)
PKG_CHECK_MODULES([CWRAP_NSS], [nss_wrapper], have_cwrap_nss=yes, have_cwrap_nss=no)
if test "$have_cwrap" != no;then
AC_DEFINE([HAVE_CWRAP], 1, [Enable testing with cwrap])
fi
AM_CONDITIONAL(HAVE_CWRAP, test "x$have_cwrap" != xno)
AM_CONDITIONAL(HAVE_CWRAP_PAM, test "x$have_cwrap_pam" != xno && test "$have_liboath" = yes && test "$pam_enabled" = yes)
AM_CONDITIONAL(HAVE_CWRAP_ALL, test "x$have_cwrap_nss" != xno && test "x$have_cwrap_pam")
AC_ARG_WITH(radius,
AS_HELP_STRING([--without-radius], [do not include Radius support]),
@@ -564,6 +566,7 @@ Summary of build options:
CFlags: ${CFLAGS}
CWrap testing: ${have_cwrap}
CWrap PAM testing: ${have_cwrap_pam}
CWrap NSS testing: ${have_cwrap_nss}
PAM auth backend: ${pam_enabled}
Radius auth backend: ${radius_enabled}

View File

@@ -4,7 +4,7 @@ EXTRA_DIST = certs/ca-key.pem certs/ca.pem common.sh certs/server-cert.pem \
certs/server-key.pem data/test1.config data/pam/nss-group.in data/pam/nss-passwd.in \
data/pam/users.oath.templ data/test-pam-noauth.config data/test-pam.passwd \
data/test1.passwd data/test-user-cert.config certs/user-cert.pem certs/user-key.pem \
data/test3.config data/test-iroute.config data/test-pam.config \
data/test3.config data/test-iroute.config data/test-pam.config data/test-vhost2.passwd \
user-config/test user-config-opt/test data/test-pass-script.config data/test-multi-cookie.config \
data/test-stress.config certs/user-cert-wrong.pem connect-script data/test-group.passwd \
data/test-group-pass.config certs/user-group-cert.pem certs/user-group-key.pem \
@@ -19,13 +19,13 @@ EXTRA_DIST = certs/ca-key.pem certs/ca.pem common.sh certs/server-cert.pem \
certs/user-cert-testuser.pem test-stress data/test-user-config.config user-config/testuser \
data/test-sighup-key-change.config data/test-sighup-key-change.config user-config/testipnet \
certs/user-cert-testipnet.pem certs/user-cert-invalid.pem certs/server-cert-ca.pem \
data/test-san-cert.config certs/user-san-cert.pem \
data/test-san-cert.config certs/user-san-cert.pem data/test-vhost3.passwd \
certs/server-cert-ed25519.pem certs/server-key-ed25519.pem data/test-ed25519.config \
certs/server-cert-rsa-pss.pem certs/server-key-rsa-pss.pem data/test-rsa-pss.config \
data/test-otp-cert.config data/test-otp.oath test-otp-cert data/test-otp.passwd \
data/test-otp.config data/test-cert-opt-pass.config data/test-gssapi-opt-pass.config \
certs/server-key-secp521r1.pem certs/server-cert-secp521r1.pem
certs/server-key-secp521r1.pem certs/server-cert-secp521r1.pem data/test-vhost-pass-cert.config \
data/vhost.hosts
SUBDIRS = docker-ocserv docker-kerberos
@@ -50,6 +50,10 @@ dist_check_SCRIPTS += test-iroute test-multi-cookie test-pass-script \
endif
if HAVE_CWRAP
if HAVE_CWRAP_ALL
dist_check_SCRIPTS += test-vhost
endif
dist_check_SCRIPTS += test-pass test-pass-cert test-cert test-group-pass \
test-pass-group-cert test-pass-group-cert-no-pass test-sighup \
test-enc-key test-sighup-key-change test-get-cert test-san-cert \

View File

@@ -39,7 +39,9 @@ else
mkdir -p $SOCKDIR
export SOCKET_WRAPPER_DIR=$SOCKDIR
export SOCKET_WRAPPER_DEFAULT_IFACE=2
export NSS_WRAPPER_HOSTS="${srcdir}/data/vhost.hosts"
ADDRESS=127.0.0.$SOCKET_WRAPPER_DEFAULT_IFACE
RAW_OPENCONNECT="${OPENCONNECT}"
OPENCONNECT="eval LD_PRELOAD=libsocket_wrapper.so ${OPENCONNECT}"
fi
@@ -66,7 +68,7 @@ fail() {
launch_server() {
if test -n "${VERBOSE}" && test "${VERBOSE}" -ge 1;then
$SERV $* &
$SERV $* -d 3 &
else
$SERV $* >/dev/null 2>&1 &
fi
@@ -82,7 +84,7 @@ launch_server() {
launch_sr_server() {
if test -n "${VERBOSE}" && test "${VERBOSE}" -ge 1;then
LD_PRELOAD=libsocket_wrapper.so:libuid_wrapper.so UID_WRAPPER=1 UID_WRAPPER_ROOT=1 $SERV $* &
LD_PRELOAD=libsocket_wrapper.so:libuid_wrapper.so UID_WRAPPER=1 UID_WRAPPER_ROOT=1 $SERV $* -d 3 &
else
LD_PRELOAD=libsocket_wrapper.so:libuid_wrapper.so UID_WRAPPER=1 UID_WRAPPER_ROOT=1 $SERV $* >/dev/null 2>&1 &
fi
@@ -100,9 +102,9 @@ launch_sr_pam_server() {
mkdir -p "data/$PAMDIR/"
test -f "${srcdir}/data/$PAMDIR/users.oath.templ" && cp "${srcdir}/data/$PAMDIR/users.oath.templ" "data/$PAMDIR/users.oath"
test -f "${srcdir}/data/$PAMDIR/passdb.templ" && cp "${srcdir}/data/$PAMDIR/passdb.templ" "data/$PAMDIR/passdb"
export NSS_WRAPPER_PASSWD=./data/pam/nss-passwd
export NSS_WRAPPER_GROUP=./data/pam/nss-group
export PAM_WRAPPER_SERVICE_DIR=pam.$$.tmp
export NSS_WRAPPER_PASSWD=${srcdir}/data/pam/nss-passwd
export NSS_WRAPPER_GROUP=${srcdir}/data/pam/nss-group
if test -n "${VERBOSE}" && test "${VERBOSE}" -ge 1;then
LD_PRELOAD=libnss_wrapper.so:libpam_wrapper.so:libsocket_wrapper.so:libuid_wrapper.so PAM_WRAPPER_SERVICE_DIR="data/$PAMDIR" PAM_WRAPPER=1 UID_WRAPPER=1 UID_WRAPPER_ROOT=1 $SERV $* &
else
@@ -122,7 +124,7 @@ launch_sr_pam_server() {
launch_simple_sr_server() {
if test -n "${VERBOSE}" && test "${VERBOSE}" -ge 1;then
LD_PRELOAD=libsocket_wrapper.so:libuid_wrapper.so UID_WRAPPER=1 UID_WRAPPER_ROOT=1 $SERV $* &
LD_PRELOAD=libsocket_wrapper.so:libuid_wrapper.so UID_WRAPPER=1 UID_WRAPPER_ROOT=1 $SERV $* -d 3 &
else
LD_PRELOAD=libsocket_wrapper.so:libuid_wrapper.so UID_WRAPPER=1 UID_WRAPPER_ROOT=1 $SERV $* >/dev/null 2>&1 &
fi

View File

@@ -0,0 +1,216 @@
# User authentication method. Could be set multiple times and in that case
# all should succeed.
# Options: certificate, pam.
#enable-auth = "certificate"
auth = "plain[passwd=@SRCDIR@/data/test1.passwd]"
#auth = "pam"
# A banner to be displayed on clients
#banner = "Welcome"
# Use listen-host to limit to specific IPs or to the IPs of a provided hostname.
#listen-host = [IP|HOSTNAME]
use-dbus = no
# Limit the number of clients. Unset or set to zero for unlimited.
#max-clients = 1024
max-clients = 16
# Limit the number of client connections to one every X milliseconds
# (X is the provided value). Set to zero for no limit.
#rate-limit-ms = 100
max-ban-score = 0
# Limit the number of identical clients (i.e., users connecting multiple times)
# Unset or set to zero for unlimited.
max-same-clients = 2
# TCP and UDP port number
tcp-port = 443
udp-port = 443
# Keepalive in seconds
keepalive = 32400
# Dead peer detection in seconds
dpd = 440
# MTU discovery (DPD must be enabled)
try-mtu-discovery = false
# The key and the certificates of the server
# The key may be a file, or any URL supported by GnuTLS (e.g.,
# tpmkey:uuid=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx;storage=user
# or pkcs11:object=my-vpn-key;object-type=private)
#
# There may be multiple certificate and key pairs and each key
# should correspond to the preceding certificate.
server-cert = @SRCDIR@/certs/server-cert.pem
server-key = @SRCDIR@/certs/server-key.pem
# Diffie-Hellman parameters. Only needed if you require support
# for the DHE ciphersuites (by default this server supports ECDHE).
# Can be generated using:
# certtool --generate-dh-params --outfile /path/to/dh.pem
#dh-params = /path/to/dh.pem
# If you have a certificate from a CA that provides an OCSP
# service you may provide a fresh OCSP status response within
# the TLS handshake. That will prevent the client from connecting
# independently on the OCSP server.
# You can update this response periodically using:
# ocsptool --ask --load-cert=your_cert --load-issuer=your_ca --outfile response
# Make sure that you replace the following file in an atomic way.
#ocsp-response = /path/to/ocsp.der
# In case PKCS #11 or TPM keys are used the PINs should be available
# in files. The srk-pin-file is applicable to TPM keys only (It's the storage
# root key).
#pin-file = /path/to/pin.txt
#srk-pin-file = /path/to/srkpin.txt
# The Certificate Authority that will be used
# to verify clients if certificate authentication
# is set.
ca-cert = @SRCDIR@/certs/ca.pem
# The object identifier that will be used to read the user ID in the client certificate.
# The object identifier should be part of the certificate's DN
# Useful OIDs are:
# CN = 2.5.4.3, UID = 0.9.2342.19200300.100.1.1
cert-user-oid = 0.9.2342.19200300.100.1.1
# The object identifier that will be used to read the user group in the client
# certificate. The object identifier should be part of the certificate's DN
# Useful OIDs are:
# OU (organizational unit) = 2.5.4.11
#cert-group-oid = 2.5.4.11
# A revocation list of ca-cert is set
#crl = /path/to/crl.pem
# GnuTLS priority string
tls-priorities = "PERFORMANCE:%SERVER_PRECEDENCE:%COMPAT"
# To enforce perfect forward secrecy (PFS) on the main channel.
#tls-priorities = "NORMAL:%SERVER_PRECEDENCE:%COMPAT:-RSA"
# The time (in seconds) that a client is allowed to stay connected prior
# to authentication
auth-timeout = 40
# The time (in seconds) that a client is not allowed to reconnect after
# a failed authentication attempt.
#min-reauth-time = 2
# Cookie validity time (in seconds)
# Once a client is authenticated he's provided a cookie with
# which he can reconnect. This option sets the maximum lifetime
# of that cookie.
cookie-validity = 172800
# Script to call when a client connects and obtains an IP
# Parameters are passed on the environment.
# REASON, USERNAME, GROUPNAME, HOSTNAME (the hostname selected by client),
# DEVICE, IP_REAL (the real IP of the client), IP_LOCAL (the local IP
# in the P-t-P connection), IP_REMOTE (the VPN IP of the client). REASON
# may be "connect" or "disconnect".
#connect-script = /usr/bin/myscript
#disconnect-script = /usr/bin/myscript
# UTMP
#use-utmp = true
# PID file
pid-file = ocserv-vhost.pid
# The default server directory. Does not require any devices present.
#chroot-dir = /path/to/chroot
# socket file used for IPC, will be appended with .PID
# It must be accessible within the chroot environment (if any)
socket-file = ocserv-socket-vhost
# The user the worker processes will be run as. It should be
# unique (no other services run as this user).
run-as-user = @USERNAME@
run-as-group = @GROUP@
# Network settings
device = vpns
# The default domain to be advertised
default-domain = example.com
ipv4-network = 192.168.1.0
ipv4-netmask = 255.255.255.0
# Use the keywork local to advertize the local P-t-P address as DNS server
ipv4-dns = 192.168.1.1
# The NBNS server (if any)
#ipv4-nbns = 192.168.2.3
#ipv6-address =
#ipv6-mask =
#ipv6-dns =
# Prior to leasing any IP from the pool ping it to verify that
# it is not in use by another (unrelated to this server) host.
ping-leases = false
# Leave empty to assign the default MTU of the device
# mtu =
route = 192.168.1.0/255.255.255.0
#route = 192.168.5.0/255.255.255.0
#
# The following options are for (experimental) AnyConnect client
# compatibility. They are only available if the server is built
# with --enable-anyconnect
#
# Client profile xml. A sample file exists in doc/profile.xml.
# This file must be accessible from inside the worker's chroot.
# The profile is ignored by the openconnect client.
#user-profile = profile.xml
# Unless set to false it is required for clients to present their
# certificate even if they are authenticating via a previously granted
# cookie. Legacy CISCO clients do not do that, and thus this option
# should be set for them.
#cisco-client-compat = true
#config-per-user = @SRCDIR@/user-config-opt/
# An example virtual host with different authentication methods serviced
# by this server.
[vhost: cert.example.com ]
auth = "certificate"
ca-cert = @SRCDIR@/certs/ca.pem
server-cert = @SRCDIR@/certs/server-cert-secp521r1.pem
server-key = @SRCDIR@/certs/server-key-secp521r1.pem
ipv4-network = 192.168.2.0
ipv4-netmask = 255.255.255.0
cert-user-oid = 0.9.2342.19200300.100.1.1
[vhost:pass2.example.com]
auth = plain[passwd=@SRCDIR@/data/test-vhost2.passwd]
ca-cert = @SRCDIR@/certs/ca.pem
server-cert = @SRCDIR@/certs/server-cert-secp521r1.pem
server-key = @SRCDIR@/certs/server-key-secp521r1.pem
ipv4-network = 192.168.3.0
ipv4-netmask = 255.255.255.0

View File

@@ -0,0 +1 @@
vhost:*:$5$CgIdsjkTgpEtnj6c$qxVF2qpiov/07yM3536PIPiUYAmMiDCvH5tXho9IzL8

View File

@@ -0,0 +1 @@
vhost2:*:$5$uuGCR6z8OIm4V6ye$pyygjTO1VVmzCfmkgoO1f6djioe1JZZ8hPysXUFI2U/

7
tests/data/vhost.hosts Normal file
View File

@@ -0,0 +1,7 @@
127.0.0.2 pass2.example.com
127.0.0.2 default.example.com
127.0.0.2 cert.example.com
127.0.0.2 pass3.example.com
fd00::5357:5f02 pass2.example.com
fd00::5357:5f02 default.example.com
fd00::5357:5f02 cert.example.com

163
tests/test-vhost Executable file
View File

@@ -0,0 +1,163 @@
#!/bin/sh
#
# Copyright (C) 2018 Nikos Mavrogiannopoulos
#
# This file is part of ocserv.
#
# ocserv is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# ocserv is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
PKG_CONFIG="${PKG_CONFIG:-/usr/bin/pkg-config}"
SERV="${SERV:-../src/ocserv}"
srcdir=${srcdir:-.}
PORT=443
NO_NEED_ROOT=1
TMPFILE=pid.$$.tmp
VERBOSE=1
. `dirname $0`/common.sh
connect()
{
vhost=$1:$PORT
opts=$2
pass=$3
certhash=$4
COOKIE=''
echo $pass | LD_PRELOAD="libnss_wrapper.so:libsocket_wrapper.so" ${RAW_OPENCONNECT} ${vhost} -q $opts --servercert=${certhash} --authenticate --passwd-on-stdin >$TMPFILE 2>&1
grep COOKIE= $TMPFILE >/dev/null 2>&1
if test $? != 0;then
cat $TMPFILE
return 1
fi
return 0
}
echo "Testing vhosts backend with two username-password hosts and a certificate one... "
${OPENCONNECT} --version|grep 'Using OpenSSL' >/dev/null 2>&1
if test $? = 0;then
# openconnect doesn't support server_name in these cases
${PKG_CONFIG} --atleast-version=1.0.2 openssl
test $? != 0 && exit 77
fi
update_config test-vhost-pass-cert.config
launch_simple_sr_server -p $TMPFILE -d 1 -f -c "${CONFIG}"
PID=$!
wait_server $PID
echo -n "Connecting to default host to obtain cookie (user without certificate)... "
connect "default.example.com" "-u test" "test" "d66b507ae074d03b02eafca40d35f87dd81049d3"
if test $? != 0;then
fail $PID "Failed to connect with user without certificate!"
fi
echo ok
echo -n "Connecting to pass2 host to obtain cookie with username from default... "
connect "pass2.example.com" "-u test" "test" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? = 0;then
fail $PID "Connected to wrong host!"
fi
echo ok
echo -n "Connecting to pass2 host to obtain cookie with username... "
connect "pass2.example.com" "-u vhost" "vhost" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? != 0;then
fail $PID "Could not connect to pass2!"
fi
echo ok
echo -n "Connecting to certificate host to obtain cookie with username from default... "
connect "cert.example.com" "-u test" "test" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? = 0;then
fail $PID "Connected to wrong host!"
fi
echo ok
echo -n "Connecting to certificate host to obtain cookie (with certificate)... "
connect "cert.example.com" "-u test --sslkey ./certs/user-key.pem -c ./certs/user-cert.pem" "" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? != 0;then
fail $PID "Failed to connect with certificate!"
fi
echo ok
echo -n "Connecting to certificate host to obtain cookie (with incorrect certificate)... "
connect "cert.example.com" "-u test --sslkey ./certs/user-key.pem -c ./certs/user-cert-wrong.pem" "" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? = 0;then
fail $PID "Should not have connected with wrong certificate!"
fi
echo ok
echo -n "Connecting to pass2 host to obtain cookie (with certificate)... "
connect "pass2.example.com" "-u vhost --sslkey ./certs/user-key.pem -c ./certs/user-cert.pem" "" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? = 0;then
fail $PID "Connected to wrong host with certificate!"
fi
echo ok
echo -n "Connecting to default host to obtain cookie (with certificate)... "
connect "default.example.com" "-u test --sslkey ./certs/user-key.pem -c ./certs/user-cert.pem" "" "d66b507ae074d03b02eafca40d35f87dd81049d3"
if test $? = 0;then
fail $PID "Connected to wrong host with certificate!"
fi
echo ok
echo "Adding new vhost and reloading..."
cat >>${CONFIG} <<_EOF
[vhost:pass3.example.com]
auth = plain[passwd=${srcdir}/data/test-vhost3.passwd]
ca-cert = ${srcdir}/certs/ca.pem
server-cert = ${srcdir}/certs/server-cert-secp521r1.pem
server-key = ${srcdir}/certs/server-key-secp521r1.pem
ipv4-network = 192.168.67.0
ipv4-netmask = 255.255.255.0
_EOF
kill -HUP $PID
sleep 5
echo -n "Sanity check to default host..."
connect "default.example.com" "-u test" "test" "d66b507ae074d03b02eafca40d35f87dd81049d3"
if test $? != 0;then
fail $PID "Failed to connect with user without certificate!"
fi
echo ok
echo -n "Sanity check to cert host..."
connect "cert.example.com" "-u test --sslkey ./certs/user-key.pem -c ./certs/user-cert.pem" "" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? != 0;then
fail $PID "Failed to connect with certificate!"
fi
echo ok
echo -n "Connecting to pass3 host to obtain cookie with username... "
connect "pass3.example.com" "-u vhost2" "vhost2" "sha1:8e253ef2a87dd9188fe444702f2fe349d30af816"
if test $? != 0;then
fail $PID "Could not connect to pass3!"
fi
echo ok
kill $PID
wait
rm -f $TMPFILE
exit 0