-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·113 lines (88 loc) · 2.75 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
#!/bin/sh
#
# This file is the installation script for the sftp-chroot project – https://github.com/mle86/sftp-chroot/
cd "$(dirname -- "$0")"
MOUNT_TO='/jail'
autofs_cfg='jails.autofs'
autofs_dest='/etc/auto.master.d/'
jailmount_script='autofs-sftp-jails.sh'
jailmount_dest='/etc/'
sshd_append='sshd_config.add'
sshd_appendto='/etc/ssh/sshd_config'
new_group='sftp'
################################################################################
ansi_failure='[1;31m'
ansi_highlight='[1m'
ansi_reset='[0m'
ansi_info='[1;35m'
#ansi_success='[1;32m'
# fail [exitStatus=1] errorMessage
# Exits the script with an error message and an optional exit status.
fail () {
local status=1
if [ -n "$2" ]; then
status="$1"
shift
fi
printf '%s%s%s\n' "$ansi_failure" "$*" "$ansi_reset" >&2
exit $status
}
# info infoMessage
# Prints an informational message on stderr.
info () {
printf '%s%s%s\n' "$ansi_info" "$*" "$ansi_reset" >&2
}
# ask prompt [defaultInput=n]
# Asks the user for a choice, showing the $prompt and waiting for input.
# Defaults to $defaultInput in case of empty input.
# Exits with status 1 in case of EOF (Ctrl-D).
ask () {
local prompt="$1"
local default="${2:-n}"
ANSWER=
read -p "$ansi_highlight $prompt >$ansi_reset " ANSWER || fail # eof, exit with newline
[ -n "$ANSWER" ] || ANSWER="$default"
true
}
is_yes () { [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ] || [ "$ANSWER" = "j" ] || [ "$ANSWER" = "J" ] || [ "$ANSWER" = "yes" ]; }
################################################################################
[ "$(id -u)" = "0" ] || fail 1 "This installation script can only be run by root."
ask "Install autofs? [Y/n]" 'y'
if is_yes; then
apt-get update && apt-get -y install autofs
fi
ask "Copy $autofs_cfg to $autofs_dest? [Y/n]" 'y'
if is_yes; then
mkdir -vp -- "$autofs_dest"
cp -vi -- "$autofs_cfg" "$autofs_dest"
fi
ask "Copy $jailmount_script to $jailmount_dest? [Y/n]" 'y'
if is_yes; then
cp -vi -- "$jailmount_script" "$jailmount_dest"
fi
ask "Create user group $new_group? [Y/n]" 'y'
if is_yes; then
groupadd --force --system -- "$new_group"
if ! grp="$(getent group -- "$new_group")"; then
fail "group not found: $new_group"
else
info "group entry: $grp"
fi
fi
ask "Append $sshd_append to $sshd_appendto? [Y/n]" 'y'
if is_yes; then
[ -s "$sshd_appendto" ] || fail 2 "$sshd_appendto does not exist or is empty!"
cat -- "$sshd_append" >> "$sshd_appendto"
info "If your sshd_config contains an 'AllowGroups' directive,"
info "don't forget to add the '$new_group' group to it!"
fi
ask "Prepare empty $MOUNT_TO base dir? [Y/n]" 'y'
if is_yes; then
mkdir -vp -- "$MOUNT_TO"
chmod -v 0700 -- "$MOUNT_TO"
fi
ask "Reload automount and sshd? [Y/n]" 'y'
if is_yes; then
/etc/init.d/autofs reload
/etc/init.d/ssh reload
fi