#!/bin/sh
#
# Copyright © 2021 Daniel Lenski
#
# This file is part of openconnect.
#
# This is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# This library 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
# Lesser 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/>

# This test uses LD_PRELOAD
PRELOAD=1
srcdir=${srcdir:-.}
top_builddir=${top_builddir:-..}

. `dirname $0`/common.sh

FINGERPRINT="--servercert=d66b507ae074d03b02eafca40d35f87dd81049d3"
CERT=$certdir/server-cert.pem
KEY=$certdir/server-key.pem

echo "Testing GlobalProtect auth against fake server ..."

OCSERV=${srcdir}/fake-gp-server.py
launch_simple_sr_server $ADDRESS 443 $CERT $KEY >/dev/null 2>&1
PID=$!
wait_server $PID

echo -n "Authenticating with username/password via portal... "
( echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp -q $ADDRESS:443/portal -u test $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
    fail $PID "Could not receive cookie from fake GlobalProtect server"

echo ok

echo -n "Authenticating with username/password, and selecting gateway, via portal... "
( echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp -q "$ADDRESS:443/global-protect/testconfig.esp?gateways=foo,bar,baz" --authgroup=bar -u test $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
    fail $PID "Could not receive cookie from fake GlobalProtect server"

echo ok

echo -n "Authenticating with username/password via gateway... "
( echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp -q $ADDRESS:443/gateway -u test $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
    fail $PID "Could not receive cookie from fake GlobalProtect server"

echo ok

echo -n "Authenticating with username/password/token via portal, then using portal-userauthcookie to continue through gateway... "
( echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp -q "$ADDRESS:443/global-protect/testconfig.esp?portal_2fa=1&gw_2fa=1&portal_cookie=portal-userauthcookie" -u test --token-mode=totp --token-secret=FAKE $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
    fail $PID "Could not receive cookie from fake GlobalProtect server"

echo ok

echo -n "Simulating completed SAML to portal, then using portal-userauthcookie to continue through SAML-requiring gateway... "
( echo "prelogin-cookie" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp --disable-ipv6 -q "$ADDRESS:443/global-protect/testconfig.esp?portal_cookie=portal-userauthcookie&portal_saml=prelogin-cookie&gateway_saml=prelogin-cookie:prelogin-cookie" -u test $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
test $? = 2 || # what OpenConnect returns when server rejects cookie upon tunnel connection, as the fake server does
    fail $PID "Something went wrong in fake GlobalProtect server (other than the expected rejection of cookie)"

echo ok

echo -n "Simulating completed SAML to gateway... "
( echo "prelogin-cookie" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp --disable-ipv6 -q "$ADDRESS:443/ssl-vpn/testconfig.esp?gateway_saml=prelogin-cookie:prelogin-cookie" -u test $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
test $? = 2 || # what OpenConnect returns when server rejects cookie upon tunnel connection, as the fake server does
    fail $PID "Something went wrong in fake GlobalProtect server (other than the expected rejection of cookie)"

echo ok

echo -n "Authenticating with username/password via portal, then +token via gateway... "
( echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp -q "$ADDRESS:443/global-protect/testconfig.esp?gw_2fa=1" -u test --token-mode=totp --token-secret=FAKE $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
    fail $PID "Could not receive cookie from fake GlobalProtect server"

echo ok

echo -n "Authenticating with username/password/token via gateway... "
( echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp -q "$ADDRESS:443/ssl-vpn/testconfig.esp?gw_2fa=1" -u test --token-mode=totp --token-secret=FAKE $FINGERPRINT --cookieonly >/dev/null 2>&1) ||
    fail $PID "Could not receive cookie from fake GlobalProtect server"

echo ok

echo -n "Authenticating with username/password via portal, then proceeding to tunnel stage... "
echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp -q $ADDRESS:443/portal -u test $FINGERPRINT >/dev/null 2>&1
test $? = 2 || # what OpenConnect returns when server rejects cookie upon tunnel connection, as the fake server does
    fail $PID "Something went wrong in fake GlobalProtect server (other than the expected rejection of cookie)"

echo ok

echo -n "Authenticating with username/password via portal, then proceeding to tunnel stage (with IPv6 disabled)... "
echo "test" | LD_PRELOAD=libsocket_wrapper.so $OPENCONNECT --protocol=gp --disable-ipv6 -q $ADDRESS:443/portal -u test $FINGERPRINT >/dev/null 2>&1
test $? = 2 || # what OpenConnect returns when server rejects cookie upon tunnel connection, as the fake server does
    fail $PID "Something went wrong in fake GlobalProtect server (other than the expected rejection of cookie)"

echo ok

cleanup

exit 0
