-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathinstall.sh
executable file
·153 lines (136 loc) · 5.78 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
_msg() {
printf %s\\n "$1" 1>&2
}
_mkdir() {
# $1 = dir to be made; $2 = chmod hex; $3 = owner:group
if [ ! -d "$1" ]; then
mkdir -p -m "$2" "$1"
chown "$3" "$1"
fi
}
_install() {
#$1 = source; $2 = destination; $3 = chmod hex $4 = owner:group
sed -e "s|%PREFIX%|$PREFIX|g" -e "s|%SYSTEMCTLPATH%|$SYSTEMCTLPATH|g" -e "s|%SHPATH%|$SHPATH|g" -e "s|%_HOME%|$_HOME|g" "$1" > "$2"
chmod "$3" "$2"
chown "$4" "$2"
}
# Parameters
SRCDIR="${SRCDIR:-./}" # Assuming we are running this script from the root directory of our source code
PREFIX="${PREFIX:-/usr}" # Default installation of hostsblock.sh under /usr/bin/
# If these paths are not asserted via environment variables, autodetect them
if [ ! -d "$SYSTEMD_DIR" ]; then
if [ -d /usr/lib/systemd/system ]; then
SYSTEMD_DIR="/usr/lib/systemd/system"
elif [ -d /lib/systemd/system ]; then
SYSTEMD_DIR="/lib/systemd/system/"
elif [ -d /etc/systemd/system ]; then
SYSTEMD_DIR="/etc/systemd/system"
else
SYSTEMD_DIR="$PREFIX"/lib/systemd/system
fi
fi
if [ ! -x "$SYSTEMCTLPATH" ]; then
SYSTEMCTLPATH=$(command -v systemctl)
if [ ! -x "$SYSTEMCTLPATH" ]; then
if [ -x /usr/bin/systemctl ]; then
SYSTEMCTLPATH="/usr/bin/systemctl"
elif [ -x /bin/systemctl ]; then
SYSTEMCTLPATH="/bin/systemctl"
else
SYSTEMCTLPATH="$PREFIX"/bin/systemctl
fi
fi
fi
if [ ! -x "$SHPATH" ]; then
SHPATH=$(command -v sh)
if [ ! -x "$SHPATH" ]; then
if [ -x /usr/bin/sh ]; then
SHPATH="/usr/bin/sh"
elif [ -x /bin/sh ]; then
SHPATH="/bin/sh"
else
SHPATH="$PREFIX"/bin/sh
fi
fi
fi
_getent_home=$(getent passwd hostsblock | cut -f6 -d:)
if printf %s "$_getent_home" | grep -q "[[:alnum:]]"; then
_HOME="${_HOME:-$_getent_home}"
else
_HOME="${_HOME:-/var/lib/hostsblock}"
fi
if [ "$1" != "install" ]; then
# Warning
_msg "WARNING: This script will install hostblock and its configuration files with
exclusive permissions for their owner, the user 'hostsblock'. If this user does
not yet exist, it will create it with a home directory under
/var/lib/hostsblock. If you do not want the home directory here, please create
user hostsblock and set its home directory yourself before running this script,
or install hostsblock manually.
Variables effecting installation:
\$SRCDIR (currently $SRCDIR): the root directory of the source code
\$PREFIX (currently $PREFIX): parent directory of bin folder into which
hostsblock.sh installs
\$SYSTEMD_DIR (currently $SYSTEMD_DIR): where systemd unit files
will install
\$SYSTEMCTLPATH (currently $SYSTEMCTLPATH): where systemd unit files
will look for the systemctl executable
\$SHPATH (currently $SHPATH): where hostsblock and its systemd unit
files will look for the shell command. (Hint: Point this to dash instead
of bash if you want a performance boost)
\$_HOME (currently $_HOME): the home folder for user hostsblock. If a
hostsblock user already exists with a different home directory, the
new directory will be created designated as the hostsblock user's
home directory, abandoning the previous directory
If you are read to install, execute '$0 install'"
exit 0
fi
# Check if this script is running as root.
if [ "$(id -un)" != "root" ]; then
_msg "Run this script as root or via sudo, e.g. sudo $0 install"
exit 2
fi
# Dependency check for both this installation script and host block
for _dep in groupadd useradd gpasswd chown tr mkdir cksum curl touch rm sed grep file sort tee cut cp mv chmod find xargs id wc; do
if ! command -v $_dep >/dev/null 2>&1; then
_msg "Dependency $_dep missing. Please install."
exit 3
fi
done
# Opt dependency checks
if ! command -v unzip >/dev/null 2>&1; then
_msg "WARNING: Optional dependency unzip missing. You will not be able to extract zipped block files without it."
fi
if ! ( command -v 7zr >/dev/null 2>&1 || command -v 7za >/dev/null 2>&1 || command -v 7z >/dev/null 2>&1 ); then
_msg "WARNING: Optional dependency p7zip missing. You will not be able to extract 7zipped block files without it."
fi
# Check for/create hostsblock user
if ! getent passwd hostsblock >/dev/null 2>&1 && ! getent group hostsblock >/dev/null 2>&1; then
useradd -m -d "$_HOME" -s /bin/sh -U hostsblock
elif ! getent passwd hostsblock >/dev/null 2>&1; then
useradd -m -d "$_HOME" -s /bin/sh -G hostsblock hostsblock
elif ! getent group hostsblock >/dev/null 2>&1; then
groupadd hostsblock
fi
if ! getent group hostsblock | cut -d: -f4 | grep -q "\bhostsblock\b"; then
gpasswd -a hostsblock hostsblock
fi
if [ "$(getent passwd hostsblock | cut -d: -f6 )" != "$_HOME" ]; then
usermod -d "$_HOME" hostsblock
fi
# Install config examples into home directory
_mkdir "$_HOME" 755 hostsblock:hostsblock
_install "$SRCDIR"/src/hostsblock.sh "$PREFIX"/lib/hostsblock.sh 500 hostsblock:hostsblock
_install "$SRCDIR"/src/hostsblock-wrapper.sh "$PREFIX"/bin/hostsblock 550 hostsblock:hostsblock
_mkdir "$_HOME"/config.examples 700 hostsblock:hostsblock
for _conffile in hostsblock.conf black.list white.list hosts.head block.urls redirect.urls; do
_install "$SRCDIR"/conf/"$_conffile" "$_HOME"/config.examples/"$_conffile" 600 hostsblock:hostsblock
done
_mkdir "$SYSTEMD_DIR" 755 root:root
for _sysdfile in hostsblock.service hostsblock.timer hostsblock-dnsmasq-restart.path hostsblock-dnsmasq-restart.service hostsblock-hosts-clobber.path hostsblock-hosts-clobber.service; do
_install "$SRCDIR"/systemd/"$_sysdfile" "$SYSTEMD_DIR"/"$_sysdfile" 444 root:root
done
systemctl daemon-reload
_msg "hostsblock installed. To configure it, please follow the
directions here: https://github.com/gaenserich/hostsblock#config"