This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
/
configure
executable file
·237 lines (210 loc) · 7.08 KB
/
configure
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
#
# Copyright (c) 2015-2020 Pablo Cases,
# Distributed under the MIT License.
# (See accompanying file LICENSE file or
# copy at http://opensource.org/licenses/MIT)
#
if (( $(echo $BASH_VERSION | cut -d '.' -f1) < 4)); then
echo "This script requires BASH 4.x or later to work properly. Abort."
exit 1
fi
# Assume that all subscripts are located in the same directory as this script
path=$(dirname $(readlink -f $0))
read_path() {
str=$1
var=$2
read -e -p "$str" $var
# replace ~ for $HOME as "tilde expansion" does not work for read().
# http://www.gnu.org/software/bash/manual/bashref.html#Tilde-Expansion
# Known limitation: only input where ~ as a replacement for $HOME works
# not the other variants: ~fred/foo, ~+/foo, ~-/foo, ~N, ~+N, ~-N
# The extra ! is a bash4 specific and adds an extra level of indirection
eval $var=${!var/#\~/$HOME}
}
get_encrypt_path() {
read_path "Path to the root of the encrypted filesystem: " encrypt_path
if ! [ -d $encrypt_path ]; then
echo "ERROR: The directory '$encrypt_path' is not found. Try again."
get_encrypt_path
fi
}
get_decrypt_path() {
read_path "Path to the decrypted mount point: " decrypt_path
if ! [ -d $decrypt_path ]; then
while : ; do
read -p "The direcory '$decrypt_path' does not exist. Create it? [N/y]: " -n 1 create_dir
case "$create_dir" in
[Yy]* )
echo ""
mkdir -p $decrypt_path
if [ "$?" -ne 0 ]; then
sudo -k mkdir -p $decrypt_path
fi
break
;;
[Nn]* )
echo ""
;& #fall through construct for bash4
"" )
get_decrypt_path
break
;;
* )
# unsupported character, try again.
echo ""
;;
esac
done
fi
}
get_passphrase() {
read -p "The passphrase for decrypting: " ecryptfs_passphrase
}
get_configuration_id() {
read -p "The id for this configuration will be [$(basename $decrypt_path)]:" conf_id
if [ -z "$config_id" ]; then
conf_id=$(basename $decrypt_path)
fi
}
get_gendsession() {
version="1.1"
tagname="v$version"
gendsession_path=$path/gendsession-$version
if ! [ -d $gendsession_path ]; then
wget -q https://github.com/backloop/gendsession/archive/${tagname}.tar.gz && tar -xf ${tagname}.tar.gz && rm ${tagname}.tar.gz
if (( $? != 0 )); then
echo "Error occurred while downloading gendsession helper. Abort."
exit 1
fi
fi
}
configure () {
# Config file is used by the mounting script
conffile=~/.automount-$conf_id.conf
touch $conffile
chmod 600 $conffile
cat << EOF >> $conffile
encrypt_path=$encrypt_path
decrypt_path=$decrypt_path
EOF
echo "Created '$conffile'"
# Keeping the passwd file separate for easy use of mount command
passwdfile=~/.automount-$conf_id.ecryptfs
touch $passwdfile
chmod 600 $passwdfile
cat << EOF >> $passwdfile
passwd=$ecryptfs_passphrase
EOF
echo "Created '$passwdfile'"
# Create wrapper script that is autostarted at login
cmdfile=~/automount-$conf_id.sh
touch $cmdfile
chmod 700 $cmdfile
cat << EOF >> $cmdfile
#!/bin/sh
# These actions will be executed during login
$path/mount-ecryptfs.sh ~/.automount-$conf_id.conf ~/.automount-$conf_id.ecryptfs
# This starts a listener for logout signalling and executes the actions during logout
$gendsession_path/gendsession.py $path/umount-ecryptfs.sh ~/.automount-$conf_id.conf
EOF
echo "Created '$cmdfile'"
# Allow execution of some commands without asking for password
sudoersfile=/etc/sudoers.d/01_automount-$conf_id
sudo -k tee $sudoersfile > /dev/null << EOF
$USER ALL=NOPASSWD:/bin/mount -t ecryptfs $encrypt_path $decrypt_path *,/bin/umount $decrypt_path,/usr/bin/ecryptfs-add-passphrase --fnek
EOF
echo "Created '$sudoersfile'"
# create .desktop file for Startup Applications
#http://askubuntu.com/questions/63407/where-are-startup-commands-stored
autostartfile=~/.config/autostart/automount-$conf_id.desktop
cat << EOF >> $autostartfile
[Desktop Entry]
Type=Application
Exec=$HOME/automount-$conf_id.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=Automount eCryptfs '$conf_id' configuration
Name=Automount eCryptfs '$conf_id' configuration
Comment[en_US]=Decrypted filesystem available at '$decrypt_path'
Comment=Decrypted filesystem available at '$decrypt_path'
EOF
echo "Created '$autostartfile'"
}
deconfigure() {
conffile=~/.automount-$conf_id.conf
if [ -e $conffile ]; then
rm $conffile
echo "Removed '$conffile'"
fi
passwdfile=~/.automount-$conf_id.ecryptfs
if [ -e $passwdfile ]; then
rm $passwdfile
echo "Removed '$passwdfile'"
fi
cmdfile=~/automount-$conf_id.sh
if [ -e $cmdfile ]; then
rm $cmdfile
echo "Removed '$cmdfile'"
fi
sudoersfile=/etc/sudoers.d/01_automount-$conf_id
if [ -e $sudoersfile ]; then
sudo -k rm $sudoersfile
echo "Removed '$sudoersfile'"
fi
autostartfile=~/.config/autostart/automount-$conf_id.desktop
if [ -e $autostartfile ]; then
rm $autostartfile
echo "Removed '$autostartfile'"
fi
echo "Done."
}
usage() {
echo ""
echo "Usage: $0 [-d conf_id]"
echo ""
exit 1
}
#
# Main
#
if (( $# == 2 )); then
if [ "$1" == "-d" ]; then
conf_id=$2
deconfigure
else
usage
fi
elif (( $# == 1 )); then
if [ "$1" == "-h" ]; then
usage
else
usage
fi
# currently does not matter if -h or other option
usage
elif (( $# == 0 )); then
get_encrypt_path
get_decrypt_path
get_passphrase
get_configuration_id
# register handler when we have enough info and before
# we're creating objects outside this directory
trap "deconfigure; exit 1" SIGHUP SIGINT SIGQUIT SIGTERM SIGTSTP
get_gendsession
configure
echo ""
echo "################################################################################"
echo "# NOTE: #"
echo "# Execution of other Startup Applications that depend on the existence of the #"
echo "# decrypted filesystem need to be delayed. Please add the following to those #"
echo "# Startup Applications' command line: #"
echo "# #"
echo " bash -c \"while ! mount | grep $decrypt_path; do sleep 2; done; <original command>\""
echo "# #"
echo "################################################################################"
echo "Done."
else
usage
fi