#!/bin/bash
### MODUL-NR. 001 ### <- Hier Nummer einfügen!
# (c) August 2008 Stefan Schäfer / FSP Computer & Netzwerke
# (c) 2009-2024  Stefan Schäfer / invis-server.org / stefan@invis-server.org
# (c) 2013,2014 Dimitri Asarowski / invis-server.org / dimitri@invis-server.org
# (c) 2013-2017 Ingo Göppert / invis-server.org / ingo@invis-server.org

# License: GPLv3
# Questions: info@invis-server.org

# Installation der OpenID Provider Software Keycloak

# Insert short description of this module here

# Dieses Programm ist freie Software. Sie können es unter den Bedingungen der 
# GNU General Public License, wie von der Free Software Foundation veröffentlicht,
# weitergeben und/oder modifizieren, entweder gemäß Version 3 der Lizenz oder
# (nach Ihrer Option) jeder späteren Version.

# Die Veröffentlichung dieses Programms erfolgt in der Hoffnung, daß es Ihnen
# von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die implizite 
# Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. 
# Details finden Sie in der GNU General Public License.

# Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem 
# Programm erhalten haben. Falls nicht, siehe <http://www.gnu.org/licenses/>. 

# This program 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 3 of the License, or
# (at your option) any later version.

# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

# include functions
source $SINEINCLUDEDIR/functions

modulename=`basename $0`

clear
windowtitle="Java und Keykloak"
pgsubtitle="Keycloak, OpenID Provider wird installiert"

# Konfigurationsvariablen lesen
domain=`getconfdata "Domain" "2"`
fqdn=`getconfdata "FQDN" "2"`

# temporaer Benutzer und Gruppe keycloak anlegen, bis rpm gefixt ist.
groupadd -r $modulename
useradd -r -g $modulename -d "/var/lib/$modulename" -s "/sbin/nologin" -c "$modulename user" $modulename 

# Paket installieren
pgsubtitle="Software wird installiert"
zypper refresh 2>&1 | tee -a $LOGFILE | pgbox
zypper -n install keycloak 2>&1 | tee -a $LOGFILE | pgbox
zyppercheck ${PIPESTATUS[0]}

# .profile für Benutzer Root erweitern
cat $TEMPLATEDIR/$modulename/profile >> /root/.profile

# Java Umgebung CA-Cert in keystore packen
openssl x509 -in /etc/easy-rsa/$domain/ca.crt -inform pem -out /etc/easy-rsa/$domain/ca.der -outform der 2>&1 | tee -a $LOGFILE | pgbox
keytool -importcert -alias invis-Server-CA -cacerts -storepass changeit -file ca.der 2>&1 | tee -a $LOGFILE | pgbox

# Testen, ob der Import geklappt hat
keytool -cacerts -storepass changeit -list|grep invis 2>&1
if [[ $? == 1 ]]; then
    # Infor Ausgabe
    msgbox "Import des CA-Zertifikats in den Java-Keystore ist fehlgeschlagen und muss manuell wiederholt werden." "$windowtitle"
fi

# sysctl UDP Send- und Receive Buffer erhoehen
cat $TEMPLATEDIR/$modulename/kc-sysctl >> /etc/sysctl.d/98.invis.conf

# Datenbank anlegen
kcdbpass=`mkdbsilent keycloak kcdbuser a`

# User keycloak in Gruppe pkeys aufnehmen
usermod -a -G pkeys keycloak 2>&1| tee -a $LOGFILE | pgbox

# Konfigurationen kopieren und anpassen
cp $TEMPLATEDIR/$modulename/keycloak /etc/sysconfig/keycloak
cp $TEMPLATEDIR/$modulename/keycloak.service /etc/systemd/system/
cp $TEMPLATEDIR/$modulename/keycloak.conf /usr/lib64/jboss/keycloak/conf/

# kcadmin Passwort genereieren
kcpassword=`pwgen -sBn 12 1`

# Environment-File Keycloak anpassen
path="/etc/sysconfig"
filename="keycloak"
string="kcpassword%$kcpassword"
changevalues $path $file "$string"

# Keycloak Konfigurationsdatei
path="/usr/lib64/jboss/keycloak/conf/"
filename="keycloak.conf"
string="kcdbpass%$kcdbpass"
changevalues $path $file "$string"
string="invis.invis-net.loc%$fqdn"
changevalues $path $file "$string"

# Script kc.sh build ausfuehren
/usr/lib64/jboss/keycloak/bin/kc.sh build 2>&1| tee -a $LOGFILE | pgbox

# Keycloak als Dienst starten und fuer Autostart vorsehen
systemctl daemon-reload
chkservice "keycloak"
