+# LinOTP - the open source solution for two factor authentication
+# Copyright (C) 2010 - 2018 KeyIdentity GmbH
+# This file is part of LinOTP server.
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public
+# License, version 3, as published by the Free Software Foundation.
+# 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 Affero General Public License for more details.
+# You should have received a copy of the
+# GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# E-mail: support@keyidentity.com
+# Website: http://www.keyidentity.com
+LINOTP_CONF_DIR=/etc/linotp2
+LINOTP_INI=$LINOTP_CONF_DIR/linotp.ini
+ echo "Missing parameters! Usage: $0 <rds hostname> <db usernmae> <db password>"
+echo "DB_HOST=$DB_HOST DB_USERNAME=$DB_USERNAME DB_PASSWORD=$DB_PASSWORD"
+function print_support_information {
+ echo -e "Please contact ${BOLD}support@keyidentity.com${DEFAULT} or ${BOLD}+49615186086115${DEFAULT} for assistance."
+ y|Y|yes|Yes|YES) reply=y ;;
+ n|N|no|No|NO) reply=n ;;
+echo -e "${BOLD}Welcome to the database setup script for LinOTP!${DEFAULT}"
+echo "Please be aware that you have to install LinOTP before you start this script."
+echo "------------------------------------------------------------------------------------------"
+ echo "This script has to be executed as root user." 1>&2
+ echo "Exiting script....." 1>&2
+echo -e "${GREEN}Starting MariaDB setup...${DEFAULT}"
+#Check if the database key exists as a nonempty file and create one in case it is not present.
+if ! [ -s /etc/linotp2/encKey ]
+ #echo "Generiere Datenbankschlüssel - dies kann eine Weile dauern, bitte haben Sie Geduld."
+ echo "Create database key - this may take a while. Please be patient."
+ echo "--------------------------------------------------"
+ if ! ( dd if=/dev/urandom of="$LINOTP_CONF_DIR/$ENCKEY" bs=1 count=128 && chown linotp "$LINOTP_CONF_DIR/$ENCKEY" && chmod 640 "$LINOTP_CONF_DIR/$ENCKEY" )
+ echo -e "${RED}Creating of database key failed. Exiting script...${DEFAULT}" 1>&2
+ echo -e "Database key was ${GREEN}successfully generated.${DEFAULT}"
+DB_PASS=$(pwgen -cnsB 32 1)
+ echo -e "${RED}Password could not be generated" 1>&2
+ echo -e "Exiting program${DEFAULT}" 1>&2
+echo "Generated password is $DB_PASS"
+echo "--------------------------------------------------"
+echo -e "${BOLD}Creating database and database users...${DEFAULT}"
+echo "Connect will be done with MariaDB account: $DB_USERNAME"
+mysql -u $DB_USERNAME -p$DB_PASSWORD -e "CREATE DATABASE IF NOT EXISTS $DB_NAME; grant all privileges on $DB_NAME.* to $DB_USER@'%' identified by '$DB_PASS'; flush privileges"
+ echo -e "${RED}Database initialization failed. Exiting script...${DEFAULT}" 1>&2
+echo "--------------------------------------------------"
+echo "Preparing linotp.ini for initial setup...."
+DATE=$(date +%Y%m%d-%H%M%S)
+if [ -e /etc/linotp2/linotp.ini ]
+ echo "$LINOTP_INI already exists. A backup is created..."
+ cp -a "$LINOTP_INI" "$LINOTP_INI.backup.$DATE"
+echo "Creating $LINOTP_INI from $LINOTP_CONF_DIR/linotp.ini.example for initial setup..."
+cp -a $LINOTP_CONF_DIR/linotp.ini.example $LINOTP_INI
+sed -i -re "s%^sqlalchemy.url =.*%sqlalchemy.url = mysql://$DB_USER:$DB_PASS@$DB_HOST/$DB_NAME%" $LINOTP_INI
+echo "--------------------------------------------------"
+echo -e "${BOLD}Setup for LinOTP ${GREEN}succeeded.${DEFAULT}"
+echo "Please proceed with the web server setup"