#!/bin/bash

if [ $# -ne 5 ]; then
    echo "create-bundle: argument error" >&2
    echo "usage: create-bundle <uid> <server> <addr> <port> <proto>" >&2
    exit 1
fi

uid=$1
server=$2
addr=$3
port=$4
proto=$5

cacrt=/etc/openvpn/o4uCA/ca.crt
ssl=/etc/openvpn/o4uCA/users

# create certificate for user
ls -al "${ssl}/$uid" 2>/dev/null
if [ ! -d "${ssl}/$uid" ]; then
	/usr/lib/openvpn-int/o4uCert_new "$uid"
fi

ovpncfg="${ssl}/${uid}/${server}.ovpn"
ovpncfg_old="${ssl}/${uid}/${server}-pre_2_5.ovpn"

rdy2gobas="/var/www/readytogo"
readytogo="${rdy2gobas}/${uid}/"

if [ -f ${readytogo}/qrcode.png ]; then
    otpsupport='static-challenge "TOTP pin: " 0'
else
    otpsupport=''
fi

# prepare a matching client config
(
  echo '<ca>'; cat "$cacrt"; echo -e '</ca>\n<cert>';
  cat "${ssl}/${uid}/cert.pem"; echo -e '</cert>\n<key>';
  cat "${ssl}/${uid}/private.key"; echo '</key>'

cat <<-ENDOFCLIENTCONFIG
	# client config for server ${server}

	client
	dev tun
	proto ${proto}
	remote ${addr} ${port}
	verify-x509-name ${server} name-prefix
	resolv-retry infinite
	nobind
	persist-key
	persist-tun
	verb 3
	auth-user-pass
	auth-nocache
	${otpsupport}
ENDOFCLIENTCONFIG

) > "$ovpncfg"
cp "$ovpncfg" "$ovpncfg_old"

echo "cipher AES-256-GCM" >> "$ovpncfg_old"


eval "$(ucr shell)"

# zip all files and put a copy into download directory for user
umask 037
mkdir -p "${readytogo}"

pw=$(cat /etc/ldapper-m.secret) 

cat > "${readytogo}/.htaccess" <<-ENDOFHTACCESS
AuthBasicProvider ldap
AuthType Basic
AuthName "secret"
AuthLDAPURL "ldap://$ldap_server_name:$ldap_server_port/$ldap_base?uid"
AuthLDAPBindDN "uid=ldapper-m-$hostname,cn=users,$ldap_base"
AuthLDAPBindPassword $pw
Require ldap-user ${uid}

ENDOFHTACCESS
chgrp www-data "${readytogo}/.htaccess" "${rdy2gobas}" "${rdy2gobas}/.htaccess" "${rdy2gobas}/notfound.html" "${readytogo}"
chmod 640 "${readytogo}/.htaccess"
chmod 750 "${rdy2gobas}" "${rdy2gobas}/.htaccess" "${rdy2gobas}/notfound.html" "${readytogo}"

if [ ! -f /etc/apache2/mods-enabled/ldap.load ] || [ ! -f /etc/apache2/mods-enabled/authnz_ldap.load ]; then
    a2enmod ldap
    a2enmod authnz_ldap
    /etc/init.d/apache2 restart
fi

bundle="${readytogo}/openvpn-${server}-${uid}.zip"
umask 037

rm -f "$bundle"
zip -j "$bundle" \
    "$ovpncfg" \
    "$ovpncfg_old"
chown "$uid" "$bundle"
chgrp www-data "$bundle"

# insert a list of all available packages for the user into download.html
umask 037
templates=/usr/lib/openvpn-int/templates
rm -f "${readytogo}/download.html"
cp ${templates}/download.head "${readytogo}/download.html"
for rtgp in $(cd "${readytogo}" && echo "openvpn-"*"-${uid}.zip"); do
    echo "      <p><a href=\"/readytogo/${uid}/${rtgp}\">${rtgp}</a><hr/></p>"
done >> "${readytogo}/download.html"

if [ -n "$otpsupport" ]; then
    otpsecret=$(awk -F: "/^${uid}:/{print \$2}" /etc/openvpn/mfa/secrets)

cat <<-ENDOFTOTPPART
      <p class="triggerText" onclick="otp_popup()">display TOTP QR code and secret (for 10s)<hr/></p>
      <div class="otpPopup" id="otppopup">
        <img class="otpQrcode" src="/readytogo/${uid}/qrcode.png" alt="QR Code">
        <p class="otpSecret">${otpsecret}</p>
      </div>
ENDOFTOTPPART

fi >> "${readytogo}/download.html"

cat ${templates}/download.tail >> "${readytogo}/download.html"
chgrp www-data "${readytogo}/download.html"
chmod 640 "${readytogo}/download.html"

exit 0
