forked from panubo/docker-sshd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.sh
More file actions
executable file
·82 lines (71 loc) · 1.95 KB
/
entry.sh
File metadata and controls
executable file
·82 lines (71 loc) · 1.95 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
#!/usr/bin/env bash
set -e
[ "$DEBUG" == 'true' ] && set -x
DAEMON=sshd
# Copy default config from cache
if [ ! "$(ls -A /etc/ssh)" ]; then
cp -a /etc/ssh.cache/* /etc/ssh/
fi
# Generate Host keys, if required
if ! ls /etc/ssh/ssh_host_* 1> /dev/null 2>&1; then
ssh-keygen -A
fi
# Fix permissions, if writable
if [ -w ~/.ssh ]; then
chown root:root ~/.ssh && chmod 700 ~/.ssh/
fi
if [ -w ~/.ssh/authorized_keys ]; then
chown root:root ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
fi
if [ -w /etc/authorized_keys ]; then
chown root:root /etc/authorized_keys
chmod 755 /etc/authorized_keys
find /etc/authorized_keys/ -type f -exec chmod 644 {} \;
fi
# Add users if SSH_USERS=user:uid:gid set
if [ -n "${SSH_USERS}" ]; then
USERS=$(echo $SSH_USERS | tr "," "\n")
for U in $USERS; do
IFS=':' read -ra UA <<< "$U"
_NAME=${UA[0]}
_UID=${UA[1]}
_GID=${UA[2]}
echo ">> Adding user ${_NAME} with uid: ${_UID}, gid: ${_GID}."
if [ ! -e "/etc/authorized_keys/${_NAME}" ]; then
echo "WARNING: No SSH authorized_keys found for ${_NAME}!"
fi
addgroup -g ${_GID} ${_NAME}
adduser -D -u ${_UID} -G ${_NAME} -s '' ${_NAME}
done
else
# Warn if no authorized_keys
if [ ! -e ~/.ssh/authorized_keys ] && [ ! $(ls -A /etc/authorized_keys) ]; then
echo "WARNING: No SSH authorized_keys found!"
fi
fi
# Update MOTD
if [ -v MOTD ]; then
echo -e "$MOTD" > /etc/motd
fi
stop() {
echo "Received SIGINT or SIGTERM. Shutting down $DAEMON"
# Get PID
pid=$(cat /var/run/$DAEMON/$DAEMON.pid)
# Set TERM
kill -SIGTERM "${pid}"
# Wait for exit
wait "${pid}"
# All done.
echo "Done."
}
echo "Running $@"
if [ "$(basename $1)" == "$DAEMON" ]; then
trap stop SIGINT SIGTERM
$@ &
pid="$!"
mkdir -p /var/run/$DAEMON && echo "${pid}" > /var/run/$DAEMON/$DAEMON.pid
wait "${pid}" && exit $?
else
exec "$@"
fi