Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
88 changes: 88 additions & 0 deletions setup/09-install-hosts-redirect.everytime.sh
Original file line number Diff line number Diff line change
@@ -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