#!/bin/bash
#
# Copyright (C) 2026 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 General Public License
# along with GnuTLS; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

SERV="${SERV:-../src/ocserv}"
srcdir=${srcdir:-.}
NO_NEED_ROOT=1

. `dirname $0`/common.sh

require_oc_client

eval "${GETPORT}"

echo "Testing cert-group auto-selection (#692)"

# Reproducer for #692: when cert-group-oid is configured and the client
# certificate carries exactly one group, and no "select-group" list is
# configured (so there is nothing to disambiguate), the server must not
# force an extra "select your group" round trip before completing
# authentication.

function finish {
  set +e
  echo " * Cleaning up..."
  test -n "${CONFIG}" && rm -f ${CONFIG} >/dev/null 2>&1
  cleanup
}
trap finish EXIT

SINGLE_GROUP_CERT=${srcdir}/certs/user-group-single-cert.pem
MULTI_GROUP_CERT=${srcdir}/certs/user-group-cert.pem

# cert_init CERT [GROUP]: starts a login with the client certificate CERT,
# proposing GROUP if given
cert_init() {
	cert=$1
	shift
	oc_run --client openconnect \
		--cert ${cert} --key ${srcdir}/certs/user-group-key.pem \
		init ${1:+--group "$1"} || oc_fail "Starting the login failed"
}

# ocserv always sends a Set-Cookie: webvpncontext= header, even to clear
# a non-existing one (webvpncontext=; expires=...1970...). Only a
# non-empty cookie value indicates a completed session.
SESSION_COOKIE='Set-Cookie: webvpncontext=[^;[:space:]]'
GROUP_PROMPT='Message: Please select your group'

update_config test-group-cert-autoselect.config
launch_simple_sr_server -d 1 -f -c ${CONFIG}
PID=$!
wait_server $PID

echo " * Sanity check: explicit group-select with a single certificate group"

# Sanity check: explicitly selecting the (only) certificate group works
# and completes authentication immediately.
cert_init ${SINGLE_GROUP_CERT} onlygroup
oc_expect 'Auth: success$' \
	"Explicit group selection did not complete authentication"
oc_expect 'Set-Cookie: webvpncontext=' \
	"Explicit group selection did not result in a session cookie"

echo " * OK: explicit group selection completed authentication"

echo " * Reproducer: single eligible certificate group, no group-select sent"

# The actual reproducer: the client certificate has exactly one group
# (OU=onlygroup) and the server has no "select-group" list configured
# (so the single certificate group is unambiguous). Authenticate without
# sending a <group-select> at all, as a first-contact AnyConnect client
# would. Authentication must complete directly, without an extra
# "Please select your group" prompt.
cert_init ${SINGLE_GROUP_CERT}
oc_expect_not "${GROUP_PROMPT}" \
	"Server asked to select a group despite the certificate containing a single, unambiguous group and no configured select-group list (issue #692)"
oc_expect 'Auth: success$' \
	"Authentication did not complete with the certificate's sole group automatically selected"
oc_expect 'Set-Cookie: webvpncontext=' \
	"Automatic group selection did not result in a session cookie"

echo " * OK: certificate's sole group was auto-selected, no prompt sent"

echo " * Ambiguous case: multiple eligible certificate groups, no group-select sent"

# Ambiguous case: the client certificate carries multiple groups
# (OU=group1, group2, group3, group4) and the server has no
# "select-group" list configured, so all of them are eligible and there
# is no single unambiguous group to auto-select. The server must still
# prompt the client to select a group.
cert_init ${MULTI_GROUP_CERT}
oc_expect "${GROUP_PROMPT}" \
	"Server did not ask to select a group despite the certificate containing multiple, ambiguous groups and no configured select-group list"
oc_expect_not "${SESSION_COOKIE}" \
	"Ambiguous group selection unexpectedly resulted in a session cookie"

echo " * OK: ambiguous certificate groups still require group selection"

# -----------------------------------------------------------------------
# Restart the server with select-group = onlygroup to exercise the inner
# filtering loop in auto_select_cert_group().
# -----------------------------------------------------------------------
cleanup
mkdir -p $SOCKDIR

echo "Testing cert-group auto-selection with select-group filter (#692)"

update_config test-group-cert-autoselect-filter.config
launch_simple_sr_server -d 1 -f -c ${CONFIG}
PID=$!
wait_server $PID

echo " * Filtered match: cert group is in select-group list, no group-select sent"

# select-group = onlygroup is configured; the client certificate carries
# exactly one group (OU=onlygroup) which is in the list.  The eligible set
# has exactly one entry, so the server must auto-select it and complete
# authentication without an extra round trip.
cert_init ${SINGLE_GROUP_CERT}
oc_expect_not "${GROUP_PROMPT}" \
	"Server prompted for group despite cert's sole group being in the configured select-group list (select-group filter path)"
oc_expect 'Auth: success$' \
	"Authentication did not complete with cert group auto-selected from the configured select-group list"
oc_expect 'Set-Cookie: webvpncontext=' \
	"Filtered auto-selection did not result in a session cookie"

echo " * OK: cert group in select-group list was auto-selected"

echo " * Filtered non-match: cert groups not in select-group list, no group-select sent"

# select-group = onlygroup is configured; the client certificate carries
# four groups (OU=group1..4), none of which match "onlygroup".  The eligible
# set is empty, so the server must fall back to the group selection prompt.
cert_init ${MULTI_GROUP_CERT}
oc_expect "${GROUP_PROMPT}" \
	"Server did not prompt for group despite none of the cert's groups matching the configured select-group list"
oc_expect_not "${SESSION_COOKIE}" \
	"Non-matching cert groups unexpectedly resulted in a session cookie"

echo " * OK: cert groups not in select-group list still require group selection"

exit 0
