Repository navigation
Expand file tree
/
Copy pathsetup.sh
More file actions
271 lines (242 loc) · 9.33 KB
/
Copy pathsetup.sh
File metadata and controls
271 lines (242 loc) · 9.33 KB
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
266
267
268
269
270
271
#!/usr/bin/env bash
set -Eeuo pipefail
readonly SHARE_NAME="PiFiles"
readonly SMB_CONFIG="/etc/samba/smb.conf"
readonly SHARE_CONFIG="/etc/samba/pifiles-share.conf"
readonly SMB_USER_FILE="/etc/pifiles-smb-user"
readonly SMB_PASSWORD_FILE="/etc/pifiles-smb-password"
readonly DEFAULT_SMB_USER="pifiles"
readonly DEFAULT_SMB_PASSWORD="PiFiles123"
readonly ALLOWED_SUBNET="192.168.0.0/24"
readonly APP_DIR="/opt/pifiles"
readonly SERVICE_NAME="pifiles-dashboard"
readonly UPDATE_COMMAND="/usr/local/sbin/pifiles-update"
readonly DASHBOARD_PORT="8765"
readonly LEGACY_NGINX_SITE_LINK="/etc/nginx/sites-enabled/zzzzzz-pifiles"
if [[ "${EUID}" -ne 0 ]]; then
printf 'Run this script with sudo, for example: sudo bash ./setup.sh\n' >&2
exit 1
fi
if [[ "$#" -gt 1 ]]; then
printf 'Usage: sudo %s [shared-directory]\n' "$0" >&2
exit 2
fi
if [[ ! -r /etc/os-release ]]; then
printf 'Cannot identify the operating system; expected Raspberry Pi OS or Debian.\n' >&2
exit 1
fi
. /etc/os-release
if [[ "${ID:-}" != "raspbian" && "${ID:-}" != "debian" && " ${ID_LIKE:-} " != *" debian "* ]]; then
printf 'This setup script supports Raspberry Pi OS and Debian.\n' >&2
exit 1
fi
repo_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
saved_data_dir=""
if [[ -r /etc/pifiles-data-dir ]]; then
saved_data_dir="$(cat /etc/pifiles-data-dir)"
fi
data_dir="${1:-${saved_data_dir:-/srv/pifiles}}"
if [[ "$data_dir" != /* ]]; then
data_dir="$repo_dir/$data_dir"
fi
data_dir="$(realpath -m -- "$data_dir")"
repo_dir="$(realpath -m -- "$repo_dir")"
if [[ "$data_dir" == "/" || "$data_dir" == "/srv" || "$data_dir" == "/mnt" || "$data_dir" == "/media" ]]; then
printf 'Choose a dedicated directory for the share, not a filesystem root or mount-point directory.\n' >&2
exit 2
fi
if [[ "$data_dir" == *'"'* || "$data_dir" == *$'\n'* ]]; then
printf 'The shared-directory path cannot contain a double quote or newline.\n' >&2
exit 2
fi
if [[ "$data_dir" == "$repo_dir" || "$data_dir" == "$repo_dir/"* || "$repo_dir" == "$data_dir/"* ]]; then
printf 'Keep the shared directory separate from the repository so updates cannot affect stored files.\n' >&2
exit 2
fi
if [[ ! -f "$repo_dir/app.py" || ! -f "$repo_dir/LICENSE.txt" || ! -f "$repo_dir/scripts/update.sh" || ! -d "$repo_dir/static" ]]; then
printf 'Run setup from the complete PiFiles repository checkout.\n' >&2
exit 1
fi
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y python3 samba
smb_user="${PIFILES_SMB_USER:-}"
smb_password="${PIFILES_SMB_PASSWORD:-}"
if [[ -z "$smb_user" && -r "$SMB_USER_FILE" ]]; then
smb_user="$(cat "$SMB_USER_FILE")"
fi
if [[ -z "$smb_password" && -r "$SMB_PASSWORD_FILE" ]]; then
smb_password="$(cat "$SMB_PASSWORD_FILE")"
fi
smb_user="${smb_user:-$DEFAULT_SMB_USER}"
smb_password="${smb_password:-$DEFAULT_SMB_PASSWORD}"
if [[ ! "$smb_user" =~ ^[a-zA-Z_][a-zA-Z0-9_.-]{0,31}$ ]]; then
printf 'The SMB username must be 1-32 letters, digits, underscores, dots, or hyphens, and start with a letter or underscore.\n' >&2
exit 2
fi
if [[ "$smb_password" == *$'\n'* || "$smb_password" == *$'\r'* ]]; then
printf 'The SMB password cannot contain a newline.\n' >&2
exit 2
fi
if [[ ! -f "$SMB_CONFIG" ]]; then
printf 'Samba did not create its configuration file at %s.\n' "$SMB_CONFIG" >&2
exit 1
fi
if ! testparm -s "$SMB_CONFIG" >/dev/null; then
printf 'The existing Samba configuration is invalid; fix it before installing this share.\n' >&2
exit 1
fi
mkdir -p -- "$data_dir"
if command -v mountpoint >/dev/null 2>&1 && mountpoint -q -- "$data_dir"; then
printf 'The shared directory is itself a mount point. Choose a directory inside the mounted filesystem.\n' >&2
exit 2
fi
chown nobody:nogroup "$data_dir"
chmod 2775 "$data_dir"
printf '%s\n' "$data_dir" >/etc/pifiles-data-dir
chmod 0644 /etc/pifiles-data-dir
if ! id pifiles >/dev/null 2>&1; then
useradd --system --no-create-home --shell /usr/sbin/nologin pifiles
fi
if ! id "$smb_user" >/dev/null 2>&1; then
useradd --system --no-create-home --shell /usr/sbin/nologin "$smb_user"
fi
install -o root -g pifiles -m 0640 /dev/null "$SMB_USER_FILE"
printf '%s\n' "$smb_user" >"$SMB_USER_FILE"
install -o root -g pifiles -m 0640 /dev/null "$SMB_PASSWORD_FILE"
printf '%s\n' "$smb_password" >"$SMB_PASSWORD_FILE"
config_tmp="$(mktemp "${SHARE_CONFIG}.XXXXXX")"
main_tmp=""
share_backup=""
share_existed=0
cleanup() {
rm -f -- "$config_tmp"
if [[ -n "$main_tmp" ]]; then
rm -f -- "$main_tmp"
fi
if [[ -n "$share_backup" ]]; then
rm -f -- "$share_backup"
fi
}
trap cleanup EXIT
if [[ -e "$SHARE_CONFIG" ]]; then
share_backup="$(mktemp "${SHARE_CONFIG}.backup.XXXXXX")"
cp -p -- "$SHARE_CONFIG" "$share_backup"
share_existed=1
fi
cat >"$config_tmp" <<EOF
[$SHARE_NAME]
path = "$data_dir"
browseable = yes
read only = no
guest ok = no
valid users = $smb_user
force user = nobody
force group = nogroup
create mask = 0664
directory mask = 2775
hosts allow = $ALLOWED_SUBNET 127.
EOF
install -o root -g root -m 0644 "$config_tmp" "$SHARE_CONFIG"
include_line="include = $SHARE_CONFIG"
if ! grep -Fqx -- "$include_line" "$SMB_CONFIG"; then
main_tmp="$(mktemp "${SMB_CONFIG}.XXXXXX")"
cp -p -- "$SMB_CONFIG" "$main_tmp"
printf '\n%s\n' "$include_line" >>"$SMB_CONFIG"
fi
if ! testparm -s "$SMB_CONFIG" >/dev/null; then
if [[ -n "$main_tmp" ]]; then
cp -p -- "$main_tmp" "$SMB_CONFIG"
fi
if [[ "$share_existed" -eq 1 ]]; then
cp -p -- "$share_backup" "$SHARE_CONFIG"
else
rm -f -- "$SHARE_CONFIG"
fi
printf 'The resulting Samba configuration is invalid; the previous main configuration was restored.\n' >&2
exit 1
fi
if pdbedit -L | cut -d: -f1 | grep -Fx -- "$smb_user" >/dev/null; then
printf '%s\n%s\n' "$smb_password" "$smb_password" | smbpasswd -s "$smb_user"
else
printf '%s\n%s\n' "$smb_password" "$smb_password" | smbpasswd -s -a "$smb_user"
fi
smbpasswd -e "$smb_user"
systemctl enable --now smbd
systemctl reload smbd
if ! id pifiles >/dev/null 2>&1; then
useradd --system --no-create-home --shell /usr/sbin/nologin pifiles
fi
install -d -o root -g root -m 0755 "$APP_DIR"
install -o root -g root -m 0644 "$repo_dir/app.py" "$APP_DIR/app.py"
install -o root -g root -m 0644 "$repo_dir/index.html" "$APP_DIR/index.html"
install -o root -g root -m 0644 "$repo_dir/LICENSE.txt" "$APP_DIR/LICENSE.txt"
install -o root -g root -m 0644 "$repo_dir/static/app.js" "$APP_DIR/app.js"
install -o root -g root -m 0644 "$repo_dir/static/light.css" "$APP_DIR/light.css"
install -o root -g root -m 0644 "$repo_dir/static/dark.css" "$APP_DIR/dark.css"
install -d -o root -g root -m 0755 "$APP_DIR/static"
install -o root -g root -m 0644 "$repo_dir/static/favicon.svg" "$APP_DIR/static/favicon.svg"
install -d -o root -g root -m 0755 "$APP_DIR/static/icons"
for icon in server storage folder processor memory temperature clock refresh auto-clean age-clean; do
install -o root -g root -m 0644 "$repo_dir/static/icons/$icon.svg" "$APP_DIR/static/icons/$icon.svg"
done
install -o root -g root -m 0755 "$repo_dir/scripts/update.sh" "$UPDATE_COMMAND"
install -d -o pifiles -g pifiles -m 0750 /var/lib/pifiles
rm -f /etc/pifiles-update-token
printf '%s\n' "$repo_dir" >/etc/pifiles-update-repo
chmod 0644 /etc/pifiles-update-repo
cat >/etc/sudoers.d/pifiles-dashboard <<'EOF'
pifiles ALL=(root) NOPASSWD: /usr/local/sbin/pifiles-update ""
EOF
chmod 0440 /etc/sudoers.d/pifiles-dashboard
visudo -cf /etc/sudoers.d/pifiles-dashboard
cat >/etc/systemd/system/pifiles-dashboard.service <<EOF
[Unit]
Description=PiFiles system dashboard
After=network.target
[Service]
Type=simple
User=pifiles
Group=pifiles
SupplementaryGroups=nogroup
WorkingDirectory=$APP_DIR
Environment="PIFILES_DATA_DIR=$data_dir"
Environment=PIFILES_UPDATE_COMMAND=$UPDATE_COMMAND
Environment=PIFILES_HOST=0.0.0.0
Environment=PIFILES_PORT=$DASHBOARD_PORT
ExecStart=/usr/bin/python3 $APP_DIR/app.py
Restart=on-failure
RestartSec=3
PrivateTmp=true
ProtectSystem=full
ProtectHome=read-only
ReadWritePaths="$data_dir"
[Install]
WantedBy=multi-user.target
EOF
legacy_nginx_config=0
if [[ -e "$LEGACY_NGINX_SITE_LINK" || -L "$LEGACY_NGINX_SITE_LINK" || -e /etc/nginx/sites-enabled/pifiles || -e /etc/nginx/sites-available/pifiles ]]; then
rm -f -- "$LEGACY_NGINX_SITE_LINK" /etc/nginx/sites-enabled/pifiles /etc/nginx/sites-available/pifiles
legacy_nginx_config=1
fi
if [[ "$legacy_nginx_config" -eq 1 ]] && systemctl is-active --quiet nginx; then
nginx -t
systemctl reload nginx
fi
systemctl disable --now pifiles-mdns.service 2>/dev/null || true
rm -f -- /etc/systemd/system/pifiles-mdns.service /usr/local/sbin/pifiles-publish-mdns
systemctl daemon-reload
systemctl enable --now "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
ip_address="$(hostname -I | awk '{print $1}')"
if [[ -z "$ip_address" ]]; then
printf 'Could not determine the Pi LAN address for the dashboard URL.\n' >&2
exit 1
fi
printf '\nShare is ready: \\\\%s\\%s\n' "$ip_address" "$SHARE_NAME"
printf 'SMB username: %s\n' "$smb_user"
printf 'SMB password is saved in %s (read it with sudo).\n' "$SMB_PASSWORD_FILE"
printf 'Shared directory: %s\n' "$data_dir"
printf 'Allowed client subnet: %s\n' "$ALLOWED_SUBNET"
printf 'Storage uses the free space on the filesystem containing the shared directory.\n'
printf 'Dashboard: http://%s:%s/\n' "$ip_address" "$DASHBOARD_PORT"