diff --git a/bin/ncp-provisioning.sh b/bin/ncp-provisioning.sh new file mode 100644 index 000000000..cd7fb1223 --- /dev/null +++ b/bin/ncp-provisioning.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +# this script runs at startup to provide an unique random passwords for each instance + +## redis provisioning + +CFG=/var/www/nextcloud/config/config.php +REDISPASS="$( grep "^requirepass" /etc/redis/redis.conf | cut -f2 -d' ' )" + +### IF redis password is the default one, generate a new one + +[[ "$REDISPASS" == "default" ]] && { + REDISPASS="$( openssl rand -base64 32 )" + echo Provisioning Redis password + sed -i -E "s|^requirepass .*|requirepass $REDISPASS|" /etc/redis/redis.conf + [[ "$DOCKERBUILD" != 1 ]] && systemctl restart redis +} + +### If there exists already a configuration adjust the password +[[ -f "$CFG" ]] && { + echo "Updating NextCloud config with Redis password" + sed -i "s|'password'.*|'password' => '$REDISPASS',|" "$CFG" +} + +## mariaDB provisioning + +DBADMIN=ncadmin +DBPASSWD=$( grep password /root/.my.cnf | sed 's|password=||' ) + +[[ "$DBPASSWD" == "default" ]] && { + DBPASSWD=$( openssl rand -base64 32 ) + echo Provisioning MariaDB password + echo -e "[client]\npassword=$DBPASSWD" > /root/.my.cnf + chmod 600 /root/.my.cnf + mysql <.*|'dbpassword' => '$DBPASSWD',|" "$CFG" +} + +## CPU core adjustment + +CURRENT_THREADS=$( grep "^pm.max_children" /etc/php/7.0/fpm/pool.d/www.conf | awk '{ print $3 }' ) + +CFG=/usr/local/etc/nextcloudpi-config.d/nc-limits.sh +PHPTHREADS=0 +[[ -f "$CFG" ]] && PHPTHREADS=$( grep "^PHPTHREADS_" "$CFG" | cut -d= -f2 ) + +[[ $PHPTHREADS -eq 0 ]] && PHPTHREADS=$( nproc ) + +[[ $PHPTHREADS -ne $CURRENT_THREADS ]] && { + + echo "PHP threads set to $PHPTHREADS" + + sed -i "s|pm.max_children =.*|pm.max_children = $PHPTHREADS|" /etc/php/7.0/fpm/pool.d/www.conf + sed -i "s|pm.max_spare_servers =.*|pm.max_spare_servers = $PHPTHREADS|" /etc/php/7.0/fpm/pool.d/www.conf + sed -i "s|pm.start_servers =.*|pm.start_servers = $PHPTHREADS|" /etc/php/7.0/fpm/pool.d/www.conf + + # need to restart php + bash -c " sleep 3 + systemctl stop php7.0-fpm + systemctl stop mysqld + sleep 0.5 + systemctl start php7.0-fpm + systemctl start mysqld + " &>/dev/null & +} + +exit 0 diff --git a/changelog.md b/changelog.md index 6d0557731..e4b4995df 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,7 @@ -[v0.46.29](https://github.com/nextcloud/nextcloudpi/commit/1804c51) (2018-02-22) ncp-web: fix overlay z-index +[v0.46.30](https://github.com/nextcloud/nextcloudpi/commit/b6ba190) (2018-02-22) add ncp-provisioning to SD card images + +[v0.46.29](https://github.com/nextcloud/nextcloudpi/commit/970a256) (2018-02-22) ncp-web: fix overlay z-index [v0.46.28](https://github.com/nextcloud/nextcloudpi/commit/c78cf60) (2018-02-22) wizard: fix logbox overflow diff --git a/docker-armhf/nextcloud/Dockerfile b/docker-armhf/nextcloud/Dockerfile index f2ba077e2..dc25a2c4a 100644 --- a/docker-armhf/nextcloud/Dockerfile +++ b/docker-armhf/nextcloud/Dockerfile @@ -37,5 +37,4 @@ echo -e "[client]\npassword=default" > /root/.my.cnf; \ chmod 600 /root/.my.cnf COPY docker-common/nextcloud/020nextcloud /etc/services-enabled.d/ -COPY docker-common/nextcloud/ncp-provisioning.sh /usr/local/bin/ -RUN chmod +x /usr/local/bin/ncp-provisioning.sh +COPY bin/ncp-provisioning.sh /usr/local/bin/ diff --git a/docker-common/nextcloud/020nextcloud b/docker-common/nextcloud/020nextcloud index 76299b7fd..3f5fa7610 100755 --- a/docker-common/nextcloud/020nextcloud +++ b/docker-common/nextcloud/020nextcloud @@ -18,7 +18,7 @@ OCC="$NCDIR/occ" } echo "Provisioning" -/usr/local/bin/ncp-provisioning.sh +bash /usr/local/bin/ncp-provisioning.sh echo "Starting Redis" mkdir -p /var/run/redis diff --git a/docker-common/nextcloud/ncp-provisioning.sh b/docker-common/nextcloud/ncp-provisioning.sh deleted file mode 100644 index 014bd8149..000000000 --- a/docker-common/nextcloud/ncp-provisioning.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -# this script runs at startup to provide an unique random passwords for each instance - -## redis provisioning - -REDISPASS="$( grep "^requirepass" /etc/redis/redis.conf | cut -f2 -d' ' )" - -### IF redis password is the default one, generate a new one - -[[ "$REDISPASS" == "default" ]] && { - REDISPASS="$( openssl rand -base64 32 )" - echo Provisioning Redis password - sed -i -E "s|^requirepass .*|requirepass $REDISPASS|" /etc/redis/redis.conf -} - -### If there exists already a configuration adjust the password -test -f /data/app/config/config.php && { - echo Updating NextCloud config with Redis password $REDISPASS - sed -i "s|'password'.*|'password' => '$REDISPASS',|" /data/app/config/config.php -} - -## mariaDB provisioning - -DBADMIN=ncadmin -DBPASSWD=$( grep password /root/.my.cnf | cut -d= -f2 ) -[[ "$DBPASSWD" == "default" ]] && { - DBPASSWD=$( openssl rand -base64 32 ) - echo Provisioning MariaDB password - echo -e "[client]\npassword=$DBPASSWD" > /root/.my.cnf - chmod 600 /root/.my.cnf - mysql <.*|'dbpassword' => '$DBPASSWD',|" /data/app/config/config.php -} - -exit 0 diff --git a/docker/nextcloud/Dockerfile b/docker/nextcloud/Dockerfile index f5e4390ea..f063831ff 100644 --- a/docker/nextcloud/Dockerfile +++ b/docker/nextcloud/Dockerfile @@ -37,5 +37,4 @@ echo -e "[client]\npassword=default" > /root/.my.cnf; \ chmod 600 /root/.my.cnf COPY docker-common/nextcloud/020nextcloud /etc/services-enabled.d/ -COPY docker-common/nextcloud/ncp-provisioning.sh /usr/local/bin/ -RUN chmod +x /usr/local/bin/ncp-provisioning.sh +COPY bin/ncp-provisioning.sh /usr/local/bin/ diff --git a/etc/library.sh b/etc/library.sh old mode 100755 new mode 100644 diff --git a/etc/nextcloudpi-config.d/nc-init.sh b/etc/nextcloudpi-config.d/nc-init.sh index b8474ec3a..725cd87f5 100644 --- a/etc/nextcloudpi-config.d/nc-init.sh +++ b/etc/nextcloudpi-config.d/nc-init.sh @@ -56,7 +56,7 @@ configure() } # workaround to emulate DROP USER IF EXISTS ..;) - local DBPASSWD=$( grep password /root/.my.cnf | cut -d= -f2 ) + local DBPASSWD=$( grep password /root/.my.cnf | sed 's|password=||' ) mysql < /usr/lib/systemd/system/nc-provisioning.service <<'EOF' +[Unit] +Description=Randomize passwords on first boot +Requires=network.target +After=mysql.service + +[Service] +ExecStart=/bin/bash /usr/local/bin/ncp-provisioning.sh + +[Install] +WantedBy=multi-user.target +EOF + + [[ "$DOCKERBUILD" != 1 ]] && systemctl enable nc-provisioning return 0 } @@ -168,7 +183,7 @@ configure() echo "Setting up database..." # workaround to emulate DROP USER IF EXISTS ..;) - local DBPASSWD=$( grep password /root/.my.cnf | cut -d= -f2 ) + local DBPASSWD=$( grep password /root/.my.cnf | sed 's|password=||' ) mysql < /root/.my.cnf chmod 600 /root/.my.cnf diff --git a/update.sh b/update.sh index e69deb89e..835adac20 100755 --- a/update.sh +++ b/update.sh @@ -120,86 +120,6 @@ done # not for image builds, only live updates [[ ! -f /.ncp-image ]] && { - # fix automount in latest images - test -f /etc/udev/rules.d/90-qemu.rules && { - rm -f /etc/udev/rules.d/90-qemu.rules - udevadm control --reload-rules && udevadm trigger - pgrep -c udiskie &>/dev/null && systemctl restart nc-automount - } - - # btrfs tools - type btrfs &>/dev/null || { - apt-get update - apt-get install -y --no-install-recommends btrfs-tools - } - - # harden security - - ## harden redis - REDIS_CONF=/etc/redis/redis.conf - REDISPASS=$( grep "^requirepass" /etc/redis/redis.conf | cut -d' ' -f2 ) - [[ "$REDISPASS" == "" ]] && REDISPASS=$( openssl rand -base64 32 ) - sed -i 's|# rename-command CONFIG ""|rename-command CONFIG ""|' $REDIS_CONF - sed -i "s|# requirepass .*|requirepass $REDISPASS|" $REDIS_CONF - - grep -q "'password'" /var/www/nextcloud/config/config.php || \ - sed -i "/timeout/a'password' => '$REDISPASS'," /var/www/nextcloud/config/config.php - - ## harden postfix - sed -i 's|^smtpd_banner .*|smtpd_banner = $myhostname ESMTP|' /etc/postfix/main.cf - sed -i 's|^disable_vrfy_command .*|disable_vrfy_command = yes|' /etc/postfix/main.cf - - ## harden SSH - sed -i 's|^#AllowTcpForwarding .*|AllowTcpForwarding no|' /etc/ssh/sshd_config - sed -i 's|^#ClientAliveCountMax .*|ClientAliveCountMax 2|' /etc/ssh/sshd_config - sed -i 's|^MaxAuthTries .*|MaxAuthTries 1|' /etc/ssh/sshd_config - sed -i 's|^#MaxSessions .*|MaxSessions 2|' /etc/ssh/sshd_config - sed -i 's|^#PermitRootLogin .*|PermitRootLogin no|' /etc/ssh/sshd_config - sed -i 's|^#TCPKeepAlive .*|TCPKeepAlive no|' /etc/ssh/sshd_config - sed -i 's|^X11Forwarding .*|X11Forwarding no|' /etc/ssh/sshd_config - sed -i 's|^#LogLevel .*|LogLevel VERBOSE|' /etc/ssh/sshd_config - sed -i 's|^#Compression .*|Compression no|' /etc/ssh/sshd_config - sed -i 's|^#AllowAgentForwarding .*|AllowAgentForwarding no|' /etc/ssh/sshd_config - - ## harden kernel - grep -q protected_hardlinks=1 /etc/sysctl.conf || cat >> /etc/sysctl.conf </dev/null - - # small tweaks - cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local - chmod go-x /usr/bin/arm-linux-gnueabihf-* &>/dev/null - sed -i "s|^UMASK.*|UMASK 027|" /etc/login.defs - - # secure mysql - DBPASSWD=$( grep password /root/.my.cnf | cut -d= -f2 ) - mysql_secure_installation &>/dev/null </dev/null install_script nc-backup.sh @@ -215,21 +135,6 @@ EOF grep -q '^ACTIVE_=yes$' nc-backup-auto.sh && activate_script nc-backup-auto.sh cd - &>/dev/null - # restore pip.conf after workaround - cat > /etc/pip.conf < /etc/cron.weekly/letsencrypt-ncp < /usr/lib/systemd/system/nc-provisioning.service <<'EOF' +[Unit] +Description=Randomize passwords on first boot +Requires=network.target +After=mysql.service + +[Service] +ExecStart=/bin/bash /usr/local/bin/ncp-provisioning.sh + +[Install] +WantedBy=multi-user.target +EOF + + systemctl enable nc-provisioning + + NEED_UPDATE=false + + MAJOR=0 MINOR=46 PATCH=30 + + MAJ=$( grep -oP "\d+\.\d+\.\d+" /usr/local/etc/ncp-version | cut -d. -f1 ) + MIN=$( grep -oP "\d+\.\d+\.\d+" /usr/local/etc/ncp-version | cut -d. -f2 ) + PAT=$( grep -oP "\d+\.\d+\.\d+" /usr/local/etc/ncp-version | cut -d. -f3 ) + + if [ "$MAJOR" -gt "$MAJ" ]; then + NEED_UPDATE=true + elif [ "$MAJOR" -eq "$MAJ" ] && [ "$MINOR" -gt "$MIN" ]; then + NEED_UPDATE=true + elif [ "$MAJOR" -eq "$MAJ" ] && [ "$MINOR" -eq "$MIN" ] && [ "$PATCH" -gt "$PAT" ]; then + NEED_UPDATE=true + fi + + [[ "$NEED_UPDATE" == "true" ]] && { + REDISPASS="default" + DBPASSWD="default" + sed -i -E "s|^requirepass .*|requirepass $REDISPASS|" /etc/redis/redis.conf + echo -e "[client]\npassword=$DBPASSWD" > /root/.my.cnf + chmod 600 /root/.my.cnf + systemctl start nc-provisioning + } + } # end - only live updates exit 0