forked from mbentley/docker-timemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·431 lines (380 loc) · 13.4 KB
/
entrypoint.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/bin/sh
# set default values
LOG_LEVEL="${LOG_LEVEL:-info}"
SET_PERMISSIONS="${SET_PERMISSIONS:-false}"
SHARE_NAME="${SHARE_NAME:-TimeMachine}"
CUSTOM_AFP_CONF="${CUSTOM_AFP_CONF:-false}"
CUSTOM_SMB_CONF="${CUSTOM_SMB_CONF:-false}"
SMB_PORT="${SMB_PORT:-445}"
CUSTOM_USER="${CUSTOM_USER:-false}"
TM_USERNAME="${TM_USERNAME:-timemachine}"
TM_GROUPNAME="${TM_GROUPNAME:-timemachine}"
TM_UID="${TM_UID:-1000}"
TM_GID="${TM_GID:-${TM_UID}}"
VOLUME_SIZE_LIMIT="${VOLUME_SIZE_LIMIT:-0}"
WORKGROUP="${WORKGROUP:-WORKGROUP}"
EXTERNAL_CONF="${EXTERNAL_CONF:-}"
HIDE_SHARES="${HIDE_SHARES:-no}"
# common functions
password_var_or_file() {
# check PASSWORD and PASSWORD_FILE are both not set
if [ -n "${PASSWORD}" ] && [ -n "${PASSWORD_FILE}" ]
then
echo "ERROR: PASSWORD and PASSWORD_FILE can not both be set. Please choose 1"
exit 1
fi
# check to see if if PASSWORD_FILE is set
if [ -n "${PASSWORD_FILE}" ]
then
# cat the password file to save the contents to the env var
PASSWORD=$(cat "${PASSWORD_FILE}")
else
# if no password file passed; set the password from the env var
PASSWORD="${PASSWORD:-timemachine}"
fi
}
set_password() {
# check to see what the password should be set to
if [ "${PASSWORD}" = "timemachine" ]
then
echo "INFO: Using default password: timemachine"
else
echo "INFO: Setting password from environment variable"
fi
# set the password
printf "INFO: "
echo "${TM_USERNAME}":"${PASSWORD}" | chpasswd
}
samba_user_setup() {
# set up user in Samba
printf "INFO: Samba - Created "
smbpasswd -L -a -n "${TM_USERNAME}"
printf "INFO: Samba - "
smbpasswd -L -e -n "${TM_USERNAME}"
printf "INFO: Samba - setting password\n"
printf "%s\n%s\n" "${PASSWORD}" "${PASSWORD}" | smbpasswd -L -s "${TM_USERNAME}"
}
create_user_directory() {
# create user directory if needed
if [ ! -d "/opt/${TM_USERNAME}" ]
then
mkdir "/opt/${TM_USERNAME}"
fi
}
createdir() {
# create directory if needed
if [ ! -d "${1}" ]; then
echo "Creating ${1}"
mkdir -p "${1}"
fi
if [ -n "${2}" ]; then
chmod "${2}" "${1}"
fi
}
create_smb_user() {
# validate that none of the required environment variables are empty
if [ -z "${TM_USERNAME}" ] || [ -z "${TM_GROUPNAME}" ] || [ -z "${PASSWORD}" ] || [ -z "${SHARE_NAME}" ] || [ -z "${TM_UID}" ] || [ -z "${TM_GID}" ]
then
echo "ERROR: Missing one or more of the following variables; unable to create user"
echo " Hint: Is the variable missing or not set in ${USER_FILE}?"
echo " TM_USERNAME=${TM_USERNAME}"
echo " TM_GROUPNAME=${TM_GROUPNAME}"
echo " PASSWORD=$(if [ -n "${PASSWORD}" ]; then printf "<value reddacted but present>";fi)"
echo " SHARE_NAME=${SHARE_NAME}"
echo " TM_UID=${TM_UID}"
echo " TM_GID=${TM_GID}"
exit 1
fi
# create custom user, group, and directories if CUSTOM_USER is not true
if [ "${CUSTOM_USER}" != "true" ]
then
# check to see if group exists; if not, create it
if grep -q -E "^${TM_GROUPNAME}:" /etc/group > /dev/null 2>&1
then
echo "INFO: Group ${TM_GROUPNAME} exists; skipping creation"
else
# make sure the group doesn't already exist with a different name
if awk -F ':' '{print $3}' /etc/group | grep -q "^${TM_GID}$"
then
EXISTING_GROUP="$(grep ":${TM_GID}:" /etc/group | awk -F ':' '{print $1}')"
echo "INFO: Group already exists with a different name; renaming '${EXISTING_GROUP}' to '${TM_GROUPNAME}'..."
sed -i "s/^${EXISTING_GROUP}:/${TM_GROUPNAME}:/g" /etc/group
else
echo "INFO: Group ${TM_GROUPNAME} doesn't exist; creating..."
# create the group
addgroup -g "${TM_GID}" "${TM_GROUPNAME}"
fi
fi
# check to see if user exists; if not, create it
if id -u "${TM_USERNAME}" > /dev/null 2>&1
then
echo "INFO: User ${TM_USERNAME} exists; skipping creation"
else
echo "INFO: User ${TM_USERNAME} doesn't exist; creating..."
# create the user
adduser -u "${TM_UID}" -G "${TM_GROUPNAME}" -h "/opt/${TM_USERNAME}" -s /bin/false -D "${TM_USERNAME}"
# set the user's password if necessary
set_password
fi
# create user directory if necessary
create_user_directory
else
echo "INFO: CUSTOM_USER=true; skipping user, group, and data directory creation; using pre-existing values in /etc/passwd, /etc/group, and /etc/shadow"
fi
# write smb.conf if CUSTOM_SMB_CONF is not true
if [ "${CUSTOM_SMB_CONF}" != "true" ]
then
echo "INFO: CUSTOM_SMB_CONF=false; generating [${SHARE_NAME}] section of /etc/samba/smb.conf..."
echo "
[${SHARE_NAME}]
fruit:aapl = yes
fruit:time machine = yes
fruit:time machine max size = ${VOLUME_SIZE_LIMIT}
path = /opt/${TM_USERNAME}
valid users = ${TM_USERNAME}
browseable = yes
writable = yes
vfs objects = fruit streams_xattr" >> /etc/samba/smb.conf
else
# CUSTOM_SMB_CONF was specified; make sure the file exists
if [ -f "/etc/samba/smb.conf" ]
then
echo "INFO: CUSTOM_SMB_CONF=true; skipping generating smb.conf and using provided /etc/samba/smb.conf"
else
# there is no /etc/samba/smbp.conf; exit
echo "ERROR: CUSTOM_SMB_CONF=true but you did not bind mount a config to /etc/samba/smb.conf; exiting."
exit 1
fi
fi
# set up user in Samba
samba_user_setup
# set user permissions
set_permissions
}
set_permissions() {
# set ownership and permissions, if requested
if [ "${SET_PERMISSIONS}" = "true" ]
then
# set the ownership of the directory time machine will use
printf "INFO: "
chown -v "${TM_USERNAME}":"${TM_GROUPNAME}" "/opt/${TM_USERNAME}"
# change the permissions of the directory time machine will use
printf "INFO: "
chmod -v 770 "/opt/${TM_USERNAME}"
else
echo "INFO: SET_PERMISSIONS=false; not setting ownership and permissions for /opt/${TM_USERNAME}"
fi
}
write_avahi_adisk_service() {
# $1 = DK_NUMBER, $2 = SHARE_NAME
echo "INFO: Avahi - adding the 'dk${1}', '${2}' share txt-record to /etc/avahi/services/smbd.service..."
# write the '_adisk._tcp' service definition
echo " <txt-record>dk${1}=adVN=${2},adVF=0x82</txt-record>" >> /etc/avahi/services/smbd.service
}
# check to see if the password should be set from a file (secret) or env var
password_var_or_file
# check to see if if we are using the alpine or debian base images (debian version uses AFP; alpine uses SMB)
# this is needed because of differences in syntax for adding groups and users
if [ -z "${NETATALK_VERSION}" ]
then
# this is the SMB version running alpine
# set default version for timecapsule w/SMB
MIMIC_MODEL="${MIMIC_MODEL:-TimeCapsule8,119}"
# write global smb.conf if CUSTOM_SMB_CONF is not true
if [ "${CUSTOM_SMB_CONF}" != "true" ]
then
echo "INFO: CUSTOM_SMB_CONF=false; generating [global] section of /etc/samba/smb.conf..."
echo "[global]
min protocol = SMB2
server role = standalone server
workgroup = ${WORKGROUP}
smb ports = ${SMB_PORT}
#unix password sync = yes
log file = /var/log/samba/log.%m
logging = file
max log size = 1000
security = user
load printers = no
access based share enum = ${HIDE_SHARES}
hide unreadable = ${HIDE_SHARES}
vfs objects = fruit streams_xattr
fruit:delete_empty_adfiles = yes
fruit:metadata = stream
fruit:model = ${MIMIC_MODEL}
fruit:posix_rename = yes
fruit:veto_appledouble = no
fruit:wipe_intentionally_left_blank_rfork = yes" > /etc/samba/smb.conf
fi
# mkdir if needed
createdir /var/lib/samba/private 700
createdir /var/log/samba/cores 700
# write avahi config file (smbd.service) to customize services advertised
echo "INFO: Avahi - generating base configuration in /etc/avahi/services/smbd.service..."
echo "<?xml version=\"1.0\" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM \"avahi-service.dtd\">
<service-group>
<name replace-wildcards=\"yes\">%h</name>
<service>
<type>_smb._tcp</type>
<port>${SMB_PORT}</port>
</service>
<service>
<type>_device-info._tcp</type>
<port>9</port>
<txt-record>model=${MIMIC_MODEL}</txt-record>
</service>
<service>
<type>_adisk._tcp</type>
<port>9</port>
<txt-record>sys=adVF=0x100</txt-record>" > /etc/avahi/services/smbd.service
# check to see if we should create one or many users
if [ -z "${EXTERNAL_CONF}" ]
then
# write the individual share info for avahi discovery
write_avahi_adisk_service 0 "${SHARE_NAME}"
# EXTERNAL_CONF not set; assume we are creating one user; create user
create_smb_user
else
# EXTERNAL_CONF is set; assume we are creating multiple users
if [ ! -d "${EXTERNAL_CONF}" ]
then
echo "ERROR: The value of EXTERNAL_CONF should be a directory mounted inside the container; ${EXTERNAL_CONF} was not found"
exit 1
fi
# initialize the DK_NUMBER variable at 0
DK_NUMBER=0
# loop through each user file in the EXTERNAL_CONF directory to load the variables
for USER_FILE in "${EXTERNAL_CONF}"/*.conf
do
echo "INFO: Loading values from ${USER_FILE}"
# source the variable file
# shellcheck disable=SC1090
. "${USER_FILE}"
# write the individual share info for avahi discovery
write_avahi_adisk_service "${DK_NUMBER}" "${SHARE_NAME}"
# create the user with the specified values
create_smb_user
# make sure we clear any previously set variables after a loop
unset TM_USERNAME TM_GROUPNAME PASSWORD SHARE_NAME VOLUME_SIZE_LIMIT TM_UID TM_GID
# increment DK_NUMBER
DK_NUMBER=$((DK_NUMBER+1))
done
fi
# finish writing the avahi discovery file
echo "INFO: Avahi - completing the configuration in /etc/avahi/services/smbd.service..."
echo " </service>
</service-group>" >> /etc/avahi/services/smbd.service
# cleanup PID files
for PIDFILE in nmbd smbd
do
if [ -f /run/samba/${PIDFILE}.pid ]
then
echo "INFO: ${PIDFILE} PID exists; removing..."
rm -v /run/samba/${PIDFILE}.pid
fi
done
# cleanup dbus PID file
if [ -f /run/dbus.pid ]
then
echo "INFO: dbus PID exists; removing..."
rm -v /run/dbus.pid
fi
# cleanup avahi PID file
if [ -f /run/avahi-daemon/pid ]
then
echo "INFO: avahi PID exists; removing..."
rm -v /run/avahi-daemon/pid
fi
else
# this is the AFP version running debian
# set default version for timecapsule w/AFP
MIMIC_MODEL="${MIMIC_MODEL:-TimeCapsule6,106}"
# create custom user, group, and directories if CUSTOM_USER is not true
if [ "${CUSTOM_USER}" != "true" ]
then
# check to see if group exists; if not, create it
if grep -q -E "^${TM_GROUPNAME}:" /etc/group > /dev/null 2>&1
then
echo "INFO: Group exists; skipping creation"
else
# make sure the group doesn't already exist with a different name
if awk -F ':' '{print $3}' /etc/group | grep -q "^${TM_GID}$"
then
EXISTING_GROUP="$(grep ":${TM_GID}:" /etc/group | awk -F ':' '{print $1}')"
echo "INFO: Group already exists with a different name; renaming '${EXISTING_GROUP}' to '${TM_GROUPNAME}'..."
groupmod -n "${TM_GROUPNAME}" "${EXISTING_GROUP}"
else
echo "INFO: Group doesn't exist; creating..."
# create the group
groupadd -g "${TM_GID}" "${TM_GROUPNAME}"
fi
fi
# check to see if user exists; if not, create it
if id -u "${TM_USERNAME}" > /dev/null 2>&1
then
echo "INFO: User exists; skipping creation"
else
echo "INFO: User doesn't exist; creating..."
# create the user
useradd -u "${TM_UID}" -g "${TM_GROUPNAME}" -d "/opt/${TM_USERNAME}" -s /bin/false "${TM_USERNAME}"
# set the user's password if necessary
set_password
fi
# create user directory if necessary
create_user_directory
else
echo "INFO: CUSTOM_USER=true; skipping user, group, and data directory creation; using pre-existing values in /etc/passwd, /etc/group, and /etc/shadow"
fi
# mkdir if needed
createdir /etc/netatalk
createdir /var/netatalk/CNID
# write afp.conf if CUSTOM_AFP_CONF is not true
if [ "${CUSTOM_AFP_CONF}" != "true" ]
then
echo "INFO: CUSTOM_AFP_CONF=false; generating /etc/netatalk/afp.conf..."
echo "[Global]
mimic model = ${MIMIC_MODEL}
log level = default:${LOG_LEVEL}
log file = /dev/stdout
zeroconf = yes
[${SHARE_NAME}]
path = /opt/${TM_USERNAME}
valid users = ${TM_USERNAME}
time machine = yes
# the max size of the data folder (in MiB)
vol size limit = ${VOLUME_SIZE_LIMIT}" > /etc/netatalk/afp.conf
else
# CUSTOM_AFP_CONF was specified; make sure the file exists
if [ -f "/etc/netatalk/afp.conf" ]
then
echo "INFO: CUSTOM_AFP_CONF=true; skipping generating afp.conf and using provided /etc/netatalk/afp.conf"
else
# there is no /etc/netatalk/afp.conf; exit
echo "ERROR: CUSTOM_AFP_CONF=true but you did not bind mount a config to /etc/netatalk/afp.conf; exiting."
exit 1
fi
fi
# set user permissions
set_permissions
# cleanup dbus PID file
if [ -f /var/run/dbus/pid ]
then
echo "INFO: dbus PID exists; removing..."
rm -v /var/run/dbus/pid
fi
# cleanup netatalk PID file
if [ -f /var/run/netatalk.pid ]
then
echo "INFO: netatalk PID exists; removing..."
rm -v /var/run/netatalk.pid
fi
# cleanup avahi-daemon PID file
if [ -f /var/run/avahi-daemon/pid ]
then
echo "INFO: avahi-daemon PID exists; removing..."
rm -v /var/run/avahi-daemon/pid
fi
fi
# run CMD
echo "INFO: entrypoint complete; executing '${*}'"
exec "${@}"