#!/usr/bin/sh #---------------------------------------------------------------------------------- # /var/install/bin/nextcloud-pre-edit - edit Nextcloud configuration file # # Copyright (c) 2017-2025 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2017-06-26 jed # Last Update: $Id$ # # 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 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------------- # read eislib etc. . /var/install/include/eislib pgmname=`basename $0` module_name="`echo "${pgmname}" | cut -d- -f1 | cut -d. -f1`" #exec 2> /tmp/${module_name}-pre-edit-trace$$.log #set -x sql_package_list=/var/run/${module_name}-sql-package-list configfile=/etc/config.d/${module_name} nextcloud_path=/var/${module_name} tmp_file=/var/tmp/${pgmname}.$$ # create list of available MySQL/MariaDB packages { for SERVER in `{ /usr/bin/eisman search --fields=name --installed "mariadb" /usr/bin/eisman search --fields=name --installed "mysql" /usr/bin/eisman search --fields=name --installed "postgresql" } | grep -v '^-----' | sed 's/^name *//' | grep -E "^mariadb-client$|^mariadb$|^mariadb[0-9]+$|^mysql$|^mysql[0-9]+$|^postgresql[0-9]+$"` do mysql_basedir='' my_path='' my_port='' echo "${SERVER}" | grep -q "^postgresql" if [ $? -ne 0 ] then # MySQL / MariaDB if [ -f /etc/config.d/${SERVER} ] then my_path=`echo ${SERVER} | sed 's|[^[:digit:]]||g'` if [ -n "${my_path}" ] then # load new dynamic configuration . /srv/mysql/defaults.info else # load old, static configuration . /var/lib/mysql/defaults.info fi my_port=`grep "^.*_CONNECT_PORT" /etc/config.d/${SERVER} | sed 's/^.*_CONNECT_PORT=.\([0-9]*\).*$/\1/'` fi fi # mariadb100:3306:/usr/lib/mysql/100/bin: # postgresql96::: echo "${SERVER}:${my_port}:${mysql_basedir}:" done } > ${sql_package_list} # read parameters if [ -f ${configfile} ] then { grep -E "^NEXTCLOUD_DATA_DIR|^NEXTCLOUD_INSTANCE_ID|NEXTCLOUD_INSTANCE_SECRET|NEXTCLOUD_PASSWORD_SALT" ${configfile} } > ${tmp_file} . ${tmp_file} rm -d ${tmp_file} if [ -n "${NEXTCLOUD_DATA_DIR}" ] then # remove trailing slashes nextcloud_path="`echo "${NEXTCLOUD_DATA_DIR}" | sed 's#[/ ]*$##'`" fi ### instance id ### if [ -z "${NEXTCLOUD_INSTANCE_ID}" -a -z "`grep ^NEXTCLOUD_INSTANCE_ID ${configfile}`" ] then # add empty parameter if it doesn't exist echo "NEXTCLOUD_INSTANCE_ID=''" >> ${configfile} fi if [ -z "${NEXTCLOUD_INSTANCE_ID}" ] then # instance id not yet set /usr/bin/php -r "error_reporting(0); include('${nextcloud_path}/config/config.php'); print(\$CONFIG['instanceid']);" 2>/dev/null | \ grep -vi "array" > ${tmp_file} instance_id="`cat ${tmp_file} | sed 's/ *//g'`" rm -f ${tmp_file} if [ -n "${instance_id}" ] then # value found in Nextcloud configuration, save it ... mecho -n "writing instance id to configuration ..." # echo "INSTANCE-ID:${instance_id}" # check if parameter is followed by a comment grep -q "^NEXTCLOUD_INSTANCE_ID=.*#" ${configfile} if [ $? -eq 0 ] then # parameter with comment sed "s|^\(NEXTCLOUD_INSTANCE_ID\)=['\"]['\"]\( *#.*\)$|\1='${instance_id}' \2|" ${configfile} >${configfile}.tmp else # parameter without comment sed "s|^\(NEXTCLOUD_INSTANCE_ID\)=['\"]['\"].*$|\1='${instance_id}'|" ${configfile} >${configfile}.tmp fi cp ${configfile}.tmp ${configfile} rm -f ${configfile}.tmp echo --info "done." fi fi ### instance secret ### if [ -z "${NEXTCLOUD_INSTANCE_SECRET}" -a -z "`grep ^NEXTCLOUD_INSTANCE_SECRET ${configfile}`" ] then # add empty parameter if it doesn't exist echo "NEXTCLOUD_INSTANCE_SECRET=''" >> ${configfile} fi if [ -z "${NEXTCLOUD_INSTANCE_SECRET}" ] then # secret not yet set /usr/bin/php -r "error_reporting(0); include('${nextcloud_path}/config/config.php'); print(\$CONFIG['secret']);" 2>/dev/null | \ grep -vi "array" > ${tmp_file} instance_secret="`cat ${tmp_file} | sed 's/ *//g'`" rm -f ${tmp_file} if [ -n "${instance_secret}" ] then # value found in Nextcloud configuration, save it ... mecho -n "writing instance secret to configuration ..." # echo "SECRET:${_uro_secret}" # check if parameter is followed by a comment grep -q "^NEXTCLOUD_INSTANCE_SECRET=.*#" ${configfile} if [ $? -eq 0 ] then # parameter with comment sed "s|^\(NEXTCLOUD_INSTANCE_SECRET\)=['\"]['\"]\( *# .*\)$|\1='${instance_secret}' \2|" ${configfile} >${configfile}.tmp else # parameter without comment sed "s|^\(NEXTCLOUD_INSTANCE_SECRET\)=['\"]['\"].*$|\1='${instance_secret}'|" ${configfile} >${configfile}.tmp fi cp ${configfile}.tmp ${configfile} rm -f ${configfile}.tmp echo --info "done." fi fi ### password salt ### if [ -z "${NEXTCLOUD_PASSWORD_SALT}" -a -z "`grep ^NEXTCLOUD_PASSWORD_SALT ${configfile}`" ] then # add empty parameter if it doesn't exist echo "NEXTCLOUD_PASSWORD_SALT=''" >> ${configfile} fi if [ -z "${NEXTCLOUD_PASSWORD_SALT}" ] then # secret not yet set /usr/bin/php -r "error_reporting(0); include('${nextcloud_path}/config/config.php'); print(\$CONFIG['passwordsalt']);" 2>/dev/null | \ grep -vi "array" > ${tmp_file} password_salt="`cat ${tmp_file} | sed 's/ *//g'`" rm -f ${tmp_file} if [ -n "${password_salt}" ] then # value found in Nextcloud configuration, save it ... mecho -n "writing password salt to configuration ..." # echo "SECRET:${password_salt}" # check if parameter is followed by a comment grep -q "^NEXTCLOUD_PASSWORD_SALT=.*#" ${configfile} if [ $? -eq 0 ] then # parameter with comment sed "s|^\(NEXTCLOUD_PASSWORD_SALT\)=['\"]['\"]\( *# .*\)$|\1='${password_salt}' \2|" ${configfile} >${configfile}.tmp else # parameter without comment sed "s|^\(NEXTCLOUD_PASSWORD_SALT\)=['\"]['\"].*$|\1='${password_salt}'|" ${configfile} >${configfile}.tmp fi cp ${configfile}.tmp ${configfile} rm -f ${configfile}.tmp echo --info "done." fi fi chmod 0600 ${configfile} sleep 3 fi exit 0