diff --git a/Makefile.am b/Makefile.am index 6c0b802be..330166170 100644 --- a/Makefile.am +++ b/Makefile.am @@ -248,6 +248,7 @@ ipc_meta_setupdir = $(datadir)/@PACKAGE@/setup/ ipc_meta_setup_SCRIPTS = \ $(top_srcdir)/setup/01-bios-eula.sh \ $(top_srcdir)/setup/02-migrate-users-bios-script.sh \ + $(top_srcdir)/setup/09-install-hosts-redirect.everytime.sh \ $(top_srcdir)/setup/09-install-swap-memory.everytime.sh \ $(top_srcdir)/setup/10-bios-gpio.sh \ $(top_srcdir)/setup/11-forget-bios-devsvcs-1.sh \ @@ -273,6 +274,7 @@ ipc_meta_setup_SCRIPTS = \ EXTRA_DIST += \ $(top_srcdir)/setup/01-bios-eula.sh \ $(top_srcdir)/setup/02-migrate-users-bios-script.sh \ + $(top_srcdir)/setup/09-install-hosts-redirect.everytime.sh \ $(top_srcdir)/setup/09-install-swap-memory.everytime.sh \ $(top_srcdir)/setup/10-bios-gpio.sh \ $(top_srcdir)/setup/11-forget-bios-devsvcs-1.sh \ diff --git a/setup/09-install-hosts-redirect.everytime.sh b/setup/09-install-hosts-redirect.everytime.sh new file mode 100644 index 000000000..b3733f2c8 --- /dev/null +++ b/setup/09-install-hosts-redirect.everytime.sh @@ -0,0 +1,88 @@ +#!/bin/sh + +# +# Copyright (c) 2020 Eaton +# +# This file is part of the Eaton 42ity project. +# +# 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. +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +#! \file 09-install-hosts-redirect.everytime.sh +# \brief Complete original /etc/hosts file with redirection addons + +HOSTS="/etc/hosts" +HOSTS_BACKUP="/etc/hosts.ipm.backup" +HOSTS_ADDONS="/tmp/hosts.ipm.addons" # editable by admin user + +RET=0 + +if [ ! -f $HOSTS ]; then + echo "ERROR: $HOSTS not found" + RET=1 +elif [ ! -f $HOSTS_ADDONS ]; then + # hosts exists and no host addons is given + # restore hosts file from backup (if exists) + if [ -f $HOSTS_BACKUP ]; then + cp $HOSTS_BACKUP $HOSTS + if [ $? -eq 0 ]; then + echo "INFO: restore $HOSTS from $HOSTS_BACKUP" + # delete backup (no error) + rm -f $HOSTS_BACKUP || true + else + echo "ERROR: restore $HOSTS from $HOSTS_BACKUP failed" + RET=1 + fi + fi +else + # hosts exists and hosts addons is given + + # backup hosts file *once* (hosts original, valid) + if [ ! -f $HOSTS_BACKUP ]; then + cp $HOSTS $HOSTS_BACKUP + if [ $? -eq 0 ]; then + echo "INFO: $HOSTS_BACKUP creation" + else + echo "ERROR: $HOSTS_BACKUP creation failed" + RET=1 + fi + fi + + if [ $RET -eq 0 ]; then + # backup is ok + + # concat HOSTS_BACKUP & HOSTS_ADDONS in a tmpfile + TMPFILE="$(mktemp)" + echo "# Generated by $(basename "$0")" >> $TMPFILE + echo "# $(date)" >> $TMPFILE + echo "$(cat $HOSTS_BACKUP)" >> $TMPFILE + echo "$(cat $HOSTS_ADDONS)" >> $TMPFILE + + # replace hosts file by tmpfile (new hosts) + cp $TMPFILE $HOSTS + if [ $? -eq 0 ]; then + echo "INFO: $HOSTS_ADDONS processing succeeded" + else + echo "ERROR: $HOSTS_ADDONS processing failed" + RET=1 + # restore hosts file from backup (no error) + cp $HOSTS_BACKUP $HOSTS || true + fi + + # cleanup tmpfile (no error) + rm -f $TMPFILE || true + fi +fi + +exit $RET