Skip to content

Commit 97cafca

Browse files
authored
Merge pull request #46 from panubo/jrd-rssh
Feature: Add rssh to restrict scp and rsync
2 parents 34c49c2 + deb1b35 commit 97cafca

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM alpine:3.10
22

33
RUN apk update && \
4-
apk add bash git openssh rsync augeas shadow && \
4+
apk add bash git openssh rsync augeas shadow rssh && \
55
deluser $(getent passwd 33 | cut -d: -f1) && \
66
delgroup $(getent group 33 | cut -d: -f1) 2>/dev/null || true && \
77
mkdir -p ~root/.ssh /etc/authorized_keys && chmod 700 ~root/.ssh/ && \

README.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,34 @@ Minimal Alpine Linux Docker image with `sshd` exposed and `rsync` installed.
66

77
Configure the container with the following environment variables or optionally mount a custom sshd config at `/etc/ssh/sshd_config`:
88

9+
### General Options
10+
911
- `SSH_USERS` list of user accounts and uids/gids to create. eg `SSH_USERS=www:48:48,admin:1000:1000`
1012
- `SSH_ENABLE_ROOT` if "true" unlock the root account
1113
- `SSH_ENABLE_PASSWORD_AUTH` if "true" enable password authentication (disabled by default)
1214
- `MOTD` change the login message
13-
- `SFTP_MODE` if "true" sshd will only accept sftp connections
14-
- `SFTP_CHROOT` if in sftp only mode sftp will be chrooted to this directory. Default "/data"
15+
16+
### SSH Options
17+
1518
- `GATEWAY_PORTS` if "true" sshd will allow gateway ports
1619
- `TCP_FORWARDING` if "true" sshd will allow TCP forwarding
1720

21+
The following three optional modes, SFTP, SCP and Rsync are mutually exclusive. Only one can be
22+
enabled at a time:
23+
24+
### SFTP Only
25+
26+
- `SFTP_MODE` if "true" sshd will only accept sftp connections
27+
- `SFTP_CHROOT` if in sftp only mode sftp will be chrooted to this directory. Default "/data"
28+
29+
### SCP Only
30+
31+
- `SCP_MODE` if "true" sshd will only accept scp connections (uses rssh)
32+
33+
### Rsync Only
34+
35+
- `RSYNC_MODE` if "true" sshd will only accept rsync connections (uses rssh)
36+
1837
## SSH Host Keys
1938

2039
SSH uses host keys to identify the server. To avoid receiving security warning the host keys should be mounted on an external volume.
@@ -34,10 +53,16 @@ uid/gid and user specified in `SSH_USERS`.
3453

3554
## SFTP mode
3655

37-
When in sftp only mode (activated by setting `SFTP_MODE=true` the container will only accept sftp connections. All sftp actions will be chrooted to the `SFTP_CHROOT` directory which defaults to "/data".
56+
When in sftp only mode (activated by setting `SFTP_MODE=true`) the container will only accept sftp connections. All sftp actions will be chrooted to the `SFTP_CHROOT` directory which defaults to "/data".
3857

3958
Please note that all components of the pathname in the ChrootDirectory directive must be root-owned directories that are not writable by any other user or group (see `man 5 sshd_config`).
4059

60+
## SCP mode
61+
62+
When in scp only mode (activated by setting `SCP_MODE=true`) the container will only accept scp connections. No chroot provided.
63+
64+
This is provided using [rssh](http://www.pizzashack.org/rssh/) restricted shell.
65+
4166
## Custom Scripts
4267

4368
Executable shell scripts and binaries can be mounted or copied in to `/etc/entrypoint.d`. These will be run when the container is launched but before sshd is started. These can be used to customise the behaviour of the container.

entry.sh

+17-1
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ fi
129129

130130
# SFTP only mode
131131
if [[ "${SFTP_MODE}" == "true" ]]; then
132+
echo "INFO: configuring sftp only mode"
132133
: ${SFTP_CHROOT:='/data'}
133134
chown 0:0 ${SFTP_CHROOT}
134135
chmod 755 ${SFTP_CHROOT}
135-
136136
printf '%s\n' \
137137
'set /files/etc/ssh/sshd_config/Subsystem/sftp "internal-sftp"' \
138138
'set /files/etc/ssh/sshd_config/AllowTCPForwarding no' \
@@ -141,6 +141,22 @@ if [[ "${SFTP_MODE}" == "true" ]]; then
141141
'set /files/etc/ssh/sshd_config/ForceCommand internal-sftp' \
142142
"set /files/etc/ssh/sshd_config/ChrootDirectory ${SFTP_CHROOT}" \
143143
| augtool -s 1> /dev/null
144+
elif [[ "${SCP_MODE}" == "true" ]]; then
145+
echo "INFO: configuring scp only mode"
146+
USERS=$(echo $SSH_USERS | tr "," "\n")
147+
for U in $USERS; do
148+
_NAME=$(echo "${U}" | cut -d: -f1)
149+
usermod -s '/usr/bin/rssh' ${_NAME}
150+
done
151+
(grep '^[a-zA-Z]' /etc/rssh.conf.default; echo "allowscp") > /etc/rssh.conf
152+
elif [[ "${RSYNC_MODE}" == "true" ]]; then
153+
echo "INFO: configuring rsync only mode"
154+
USERS=$(echo $SSH_USERS | tr "," "\n")
155+
for U in $USERS; do
156+
_NAME=$(echo "${U}" | cut -d: -f1)
157+
usermod -s '/usr/bin/rssh' ${_NAME}
158+
done
159+
(grep '^[a-zA-Z]' /etc/rssh.conf.default; echo "allowrsync") > /etc/rssh.conf
144160
else
145161
# Enable AllowTcpForwarding
146162
if [[ "${TCP_FORWARDING}" == "true" ]]; then

0 commit comments

Comments
 (0)