forked from hbons/Dazzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dazzle.sh
249 lines (202 loc) · 7.57 KB
/
dazzle.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
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
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env bash
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
# Check if we're root, if not show a warning
if [[ $UID -ne 0 ]]; then
case $1 in
""|help) # You should be allowed to check the help without being root
;;
*)
echo "Sorry, but Dazzle needs to be run as root."
exit 1
;;
esac
fi
GIT=`which git`
# Define text styles
BOLD=`tput bold`
NORMAL=`tput sgr0`
show_help () {
echo "${BOLD}Dazzle, SparkleShare host setup script${NORMAL}"
echo "This script needs to be run as root"
echo
echo "Usage: dazzle [COMMAND]"
echo
echo " setup configures this machine to serve as a SparkleShare host"
echo " create PROJECT_NAME creates a SparkleShare project called PROJECT_NAME"
echo " create-encrypted PROJECT_NAME creates an encrypted SparkleShare project"
echo " link links a SparkleShare client to this host by entering a link code"
echo
}
create_account () {
STORAGE=`grep "^storage:" /etc/passwd | cut --bytes=-7`
if [ "$STORAGE" = "storage" ]; then
echo " -> Account already exists."
else
STORAGE=`grep "^storage:" /etc/group | cut --bytes=-7`
GIT_SHELL=`which git-shell`
if [ "$STORAGE" = "storage" ]; then
echo " -> useradd storage --create-home --shell $GIT_SHELL --password \"*\" --gid storage"
useradd storage --create-home --shell $GIT_SHELL --password "*" --gid storage
else
echo " -> useradd storage --create-home --shell $GIT_SHELL --password \"*\" --user-group"
useradd storage --create-home --shell $GIT_SHELL --password "*" --user-group
fi
fi
sleep 0.5
}
configure_ssh () {
echo " -> mkdir --parents /home/storage/.ssh"
mkdir --parents /home/storage/.ssh
echo " -> touch /home/storage/.ssh/authorized_keys"
touch /home/storage/.ssh/authorized_keys
echo " -> chmod 700 /home/storage/.ssh"
chmod 700 /home/storage/.ssh
echo " -> chmod 600 /home/storage/.ssh/authorized_keys"
chmod 600 /home/storage/.ssh/authorized_keys
# Disable the password for the "storage" user to force authentication using a key
CONFIG_CHECK=`grep "^# SparkleShare$" /etc/ssh/sshd_config`
if ! [ "$CONFIG_CHECK" = "# SparkleShare" ]; then
echo "" >> /etc/ssh/sshd_config
echo "# SparkleShare" >> /etc/ssh/sshd_config
echo "# Please do not edit the above comment as it's used as a check by Dazzle" >> /etc/ssh/sshd_config
echo "Match User storage" >> /etc/ssh/sshd_config
echo " PasswordAuthentication no" >> /etc/ssh/sshd_config
echo " PubkeyAuthentication yes" >> /etc/ssh/sshd_config
fi
sleep 0.5
}
reload_ssh_config () {
if [ -f "/etc/init.d/sshd" ]; then
echo " -> /etc/init.d/sshd reload"
/etc/init.d/sshd reload >/dev/null
elif [ -f "/etc/rc.d/sshd" ]; then
echo " -> /etc/rc.d/sshd reload"
/etc/rc.d/sshd reload >/dev/null
else
echo " -> /etc/init.d/ssh reload"
/etc/init.d/ssh reload >/dev/null
fi
}
install_git () {
if [ -n "$GIT" ]; then
GIT_VERSION=`$GIT --version | cut --bytes=13-`
echo " -> The Git package has already been installed (version $GIT_VERSION)."
else
if [ -f "/usr/bin/yum" ]; then
echo " -> yum --assumeyes install git"
yum --assumeyes --quiet install git
elif [ -f "/usr/bin/apt-get" ]; then
echo " -> apt-get --yes install git"
apt-get --yes --quiet install git
if [ $? -ne 0 ]; then
echo " -> apt-get --yes install git-core"
apt-get --yes --quiet install git-core
fi
elif [ -f "/usr/bin/zypper" ]; then
echo " -> zypper --yes install git-core"
zypper --yes --quiet install git-core
elif [ -f "/usr/bin/emerge" ]; then
echo " -> emerge dev-vcs/git"
emerge --quiet dev-vcs/git
else
echo "${BOLD}Could not install Git... Please install it manually before continuing.{$NORMAL}"
echo
exit 1
fi
fi
}
create_project () {
if [ -f "/home/storage/$1/HEAD" ]; then
echo " -> Project \"$1\" already exists."
echo
else
# Create the Git repository
echo " -> $GIT init --bare /home/storage/$1"
$GIT init --quiet --bare /home/storage/$1
# Don't allow force-pushing and data to get lost
echo " -> $GIT config --file /home/storage/$1/config receive.denyNonFastForwards true"
$GIT config --file /home/storage/$1/config receive.denyNonFastForwards true
# Add list of files that Git should not compress
EXTENSIONS="jpg jpeg png tiff gif flac mp3 ogg oga avi mov mpg mpeg mkv ogv ogx webm zip gz bz bz2 rpm deb tgz rar ace 7z pak iso"
for EXTENSION in $EXTENSIONS; do
sleep 0.05
echo -ne " -> echo \"*.$EXTENSION -delta\" >> /home/storage/$1/info/attributes \r"
echo "*.$EXTENSION -delta" >> /home/storage/$1/info/attributes
sleep 0.05
EXTENSION_UPPERCASE=`echo $EXTENSION | tr '[:lower:]' '[:upper:]'`
echo -ne " -> echo \"*.$EXTENSION_UPPERCASE -delta\" >> /home/storage/$1/info/attributes \r"
echo "*.$EXTENSION_UPPERCASE -delta" >> /home/storage/$1/info/attributes
done
echo ""
# Set the right permissions
echo " -> chown --recursive storage:storage /home/storage"
chown --recursive storage:storage /home/storage
sleep 0.5
echo
echo "${BOLD}Project \"$1\" was successfully created.${NORMAL}"
fi
# Fetch the external IP address
IP=`curl --silent http://ifconfig.me/ip`
PORT=`grep --max-count=1 "^Port " /etc/ssh/sshd_config | cut --bytes=6-`
# Display info to link with the created project to the user
echo "To link up a SparkleShare client, enter the following"
echo "details into the ${BOLD}\"Add Hosted Project...\"${NORMAL} dialog: "
echo
echo " Address: ${BOLD}ssh://storage@$IP:$PORT${NORMAL}"
echo " Remote Path: ${BOLD}/home/storage/$1${NORMAL}"
echo
echo "To link up (more) computers, use the \"dazzle link\" command."
echo
}
link_client () {
# Ask the user for the link code with a prompt
echo "Paste the contents of ${BOLD}\"~/SparkleShare/Your Name's link code.txt\"${NORMAL}"
echo "(found on the client) into the field below and press ${BOLD}<ENTER>${NORMAL}."
echo
echo -n " ${BOLD}Link code: ${NORMAL}"
read LINK_CODE
if [ ${#SHELL} > 256 ]; then
echo $LINK_CODE >> /home/storage/.ssh/authorized_keys
echo
echo "${BOLD}The client with this link code can now access projects.${NORMAL}"
echo "Repeat this step to link more clients."
echo
else
echo "${BOLD}Not a valid link code...${NORMAL}"
fi
}
# Parse the command line arguments
case $1 in
setup)
echo "${BOLD} 1/4 | Installing the Git package...${NORMAL}"
install_git
echo "${BOLD} 2/4 | Creating account \"storage\"...${NORMAL}"
create_account
echo "${BOLD} 3/4 | Configuring account \"storage\"...${NORMAL}"
configure_ssh
echo "${BOLD} 4/4 | Reloading the SSH config...${NORMAL}"
reload_ssh_config
echo
echo "${BOLD}Setup complete!${NORMAL}"
echo "To create a new project, run \"dazzle create PROJECT_NAME\"."
echo
;;
create)
echo "${BOLD}Creating project \"$2\"...${NORMAL}"
create_project $2
;;
create-encrypted)
echo "${BOLD}Creating encrypted project \"$2\"...${NORMAL}"
create_project $2-crypto
;;
link)
link_client $2
;;
*|help)
show_help
;;
esac