This repository has been archived by the owner on Oct 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
install.sh
executable file
·265 lines (195 loc) · 6.92 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env bash
: '
%file install.sh
%author Sam Freeside (@snovvcrash) <snovvcrash@protonmail[.]ch>
%date 2018-05-28
%brief usbrip installer.
%license
Copyright (C) 2020 Sam Freeside
This file is part of usbrip.
usbrip 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 3 of the License, or
(at your option) any later version.
usbrip 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 usbrip. If not, see <http://www.gnu.org/licenses/>.
%endlicense
'
# Usage: sudo -H bash installers/install.sh [-l/--local] [-s/--storages]
shopt -s expand_aliases
# --------------- Check for root privileges ----------------
if [[ "${EUID}" -ne 0 ]]; then
/usr/bin/printf "${R}>>>>${NC} Please run as root:\nsudo -H %s [-l/--local] [-s/--storages]\n" "${0}"
exit 1
fi
# ----------------------- Constants ------------------------
if [[ -z "${SUDO_USER}" ]]; then
SUDO_USER="root"
fi
USER_HOME=`getent passwd ${SUDO_USER} | cut -d: -f6`
CONFIG="${USER_HOME}/.config/usbrip"
OPT="/opt/usbrip"
VAR_OPT="/var/opt/usbrip"
SYMLINK="/usr/local/bin/usbrip"
G="\033[1;32m" # GREEN
Y="\033[1;33m" # YELLOW
R="\033[1;31m" # RED
W="\033[1;37m" # WHITE
NC="\033[0m" # NO COLOR
# ----------------------- Functions ------------------------
create_directory() {
/usr/bin/printf "${W}>>>>${NC} Creating directory: '$1'\n"
if [[ -d "$1" ]]; then
/usr/bin/printf "${R}>>>>${NC} $1 already exists. First run:\n%s\n" \
"sudo uninstall.sh --all"
exit 1
fi
if /bin/mkdir -p "$1"; then
/usr/bin/printf "${G}>>>>${NC} Successfully created directory: '$1'\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create directory: '$1'\n"
exit 1
fi
}
# --------------------- usbrip aliases ---------------------
alias createHistoryStorage="${OPT}/venv/bin/usbrip storage create history -e"
alias generateAuthorizedDeviceList="${OPT}/venv/bin/usbrip events genauth /var/opt/usbrip/trusted/auth.json -e -a vid pid"
alias createViolationsStorage="${OPT}/venv/bin/usbrip storage create violations /var/opt/usbrip/trusted/auth.json -e -a vid pid"
# -------------------- Handle switches ---------------------
LOCAL=false
STORAGES=false
if [[ "$1" == "-l" ]] || [[ "$1" == "--local" ]]; then
LOCAL=true
elif [[ "$1" == "-s" ]] || [[ "$1" == "--storages" ]]; then
STORAGES=true
fi
if [[ "$2" == "-l" ]] || [[ "$2" == "--local" ]]; then
LOCAL=true
elif [[ "$2" == "-s" ]] || [[ "$2" == "--storages" ]]; then
STORAGES=true
fi
# -------------- Check for required packages ---------------
# python3-venv
if ! /usr/bin/dpkg-query -W -f='${Status}' python3-venv 2>&1 | /bin/grep "ok installed" > /dev/null; then
/usr/bin/printf "${Y}>>>>${NC} Unresolved dependency: python3-venv. Do you want to install this package as:\n%s\n" \
"\"sudo apt install python3-venv -y\"?"
select YN in "Yes" "No"; do
case ${YN} in
"Yes" )
apt install "python3-venv" -y
break
;;
"No" )
exit 1
;;
esac
done
fi
# p7zip-full
if ! /usr/bin/dpkg-query -W -f='${Status}' p7zip-full 2>&1 | /bin/grep "ok installed" > /dev/null && ${STORAGES}; then
/usr/bin/printf "${Y}>>>>${NC} Unresolved dependency: p7zip-full. Do you want to install this package as:\n%s\n" \
"\"sudo apt install p7zip-full -y\"?"
select YN in "Yes" "No"; do
case ${YN} in
"Yes" )
apt install "p7zip-full" -y
break
;;
"No" )
exit 1
;;
esac
done
fi
# ------------------- Create directories -------------------
# OPT
create_directory "${OPT}"
# LOG
create_directory "${VAR_OPT}/log"
# STORAGE
create_directory "${VAR_OPT}/storage"
# CONFIG
create_directory "${CONFIG}"
# -------------------- Fix permissions ---------------------
chmod -R 600 "${VAR_OPT}/log"
# ------------ Build Python virtual environment ------------
/usr/bin/printf "${W}>>>>${NC} Building Python virtual environment\n"
if /usr/bin/python3 -m venv "${OPT}/venv"; then
/usr/bin/printf "${G}>>>>${NC} Successfully built Python virtual environment\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to build Python virtual environment\n"
exit 1
fi
# ------------------------ Install -------------------------
/usr/bin/printf "${W}>>>>${NC} Installing usbrip\n"
if $LOCAL; then
if ${OPT}/venv/bin/python "${PWD}/setup.py" install; then
/usr/bin/printf "${G}>>>>${NC} Successfully installed usbrip using local dependencies\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to install usbrip using local dependencies\n"
exit 1
fi
else
if ${OPT}/venv/bin/pip install "${PWD}"; then
/usr/bin/printf "${G}>>>>${NC} Successfully installed usbrip using PyPI dependencies\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to install usbrip using PyPI dependencies\n"
exit 1
fi
fi
${OPT}/venv/bin/python "${PWD}/setup.py" clean
echo
# ----------------------- Copy files -----------------------
if cp "${PWD}/usbrip/usb_ids/usb.ids" "${USER_HOME}/.config/usbrip/usb.ids"; then
/usr/bin/printf "${G}>>>>${NC} Successfully copied usb.ids database\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to copy usb.ids database\n"
exit 1
fi
if cp "${PWD}/usbrip/cron/usbrip.cron" "${USER_HOME}/.config/usbrip/usbrip.cron"; then
/usr/bin/printf "${G}>>>>${NC} Successfully copied usbrip cron job example\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to copy usbrip cron job example\n"
exit 1
fi
chown -R ${SUDO_USER} ${USER_HOME}/.config
# --------------------- Create symlink ---------------------
if [[ -e "${SYMLINK}" ]]; then
/bin/rm "${SYMLINK}"
fi
if /bin/ln -s "${OPT}/venv/bin/usbrip" "${SYMLINK}"; then
/usr/bin/printf "${G}>>>>${NC} Created symlink: '${SYMLINK}'\n"
fi
# ----------------- Create usbrip storages -----------------
if ${STORAGES}; then
# History
/usr/bin/printf "${W}>>>>${NC} Creating usbrip history storage\n"
if createHistoryStorage; then
/usr/bin/printf "${G}>>>>${NC} Successfully created usbrip history storage\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create usbrip history storage\n"
exit 1
fi
# GenAuth
/usr/bin/printf "${W}>>>>${NC} Generating authorized device list\n"
if generateAuthorizedDeviceList; then
/usr/bin/printf "${G}>>>>${NC} Successfully generated authorized device list\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to generate authorized device list\n"
exit 1
fi
# Violations
/usr/bin/printf "${W}>>>>${NC} Creating usbrip violations storage\n"
if createViolationsStorage; then
/usr/bin/printf "${G}>>>>${NC} Successfully created usbrip violations storage\n\n"
else
/usr/bin/printf "${R}>>>>${NC} Failed to create usbrip violations storage\n"
exit 1
fi
fi
# -------------------------- Done --------------------------
/usr/bin/printf "${G}>>>>${NC} Done.\n"