From 379876f4f6f1e127c8170025d14154ab457717e2 Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Thu, 15 Sep 2022 14:31:45 +0200 Subject: [PATCH] add support for creation of user/groups --- README.md | 8 ++++++++ start.sh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/README.md b/README.md index d583ab5..9398910 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ * Debian slim based image * OpenSSH server +* User/group creation on startup # Supported tags and respective `Dockerfile` links @@ -14,6 +15,9 @@ | Key | Format | Description | | --- | --- | --- | | `ROOT_AUTHORIZED_KEYS` | `/path/to/file` | Path to file that contains the public SSH keys that can be used for root user authentication. This file will be copied to `/root/.ssh/authorized_keys` | +| `USERADD` | `{username}:{uid}:{gid};...` | Create user account(s) on startup | +| `GROUPADD` | `{groupname}:{gid};...` | Create group account(s) on startup | +| `BASE_DIR` | `/home` | Basedir used for user account creation (Default: `/home`) | ## docker run @@ -22,6 +26,8 @@ $ docker run -it \ --name jumpbox \ -v $(pwd)/jumpbox:/var/lib/jumpbox \ -e "ROOT_AUTHORIZED_KEYS=/var/lib/jumpbox/authorized_keys" + -e "USERADD=jumpbox:1000:1000" + -e "GROUPADD=jumpbox:1000" -p 1022:22 \ pommib/jumpbox:latest ``` @@ -41,4 +47,6 @@ services: - '${PWD}/jumpbox:/var/lib/jumpbox' environment: ROOT_AUTHORIZED_KEYS: /var/lib/jumpbox/authorized_keys + USERADD: jumpbox:1000:1000 + GROUPADD: jumpbox:1000 ``` diff --git a/start.sh b/start.sh index 7053446..1e8c195 100755 --- a/start.sh +++ b/start.sh @@ -1,5 +1,7 @@ #!/bin/sh +set -ex + mkdir -p /run/sshd if [ -n "$ROOT_AUTHORIZED_KEYS" ]; then @@ -10,4 +12,37 @@ if [ -n "$ROOT_AUTHORIZED_KEYS" ]; then fi fi +if [ -z "$BASE_DIR" ]; then + BASE_DIR="/home" +fi + +if [ ! -d "$BASE_DIR" ]; then + echo "BASE_DIR $BASE_DIR does not exist, creating..." + mkdir -p $BASE_DIR +fi + +# GROUPADD="group1:1000;group2:1001;group3:1002" +while [ "$GROUPADD" != "$i" ] ;do + i=${GROUPADD%%;*} + GROUPADD="${GROUPADD#$i;}" + + GROUP_NAME=${i%%:*} + GROUP_GID="${i#$GROUP_NAME:}" + + groupadd --gid "$GROUP_GID" "$GROUP_NAME" +done + +# USERADD="user1:1000:1000;user2:1001:1000;user3:1002:1002" +while [ "$USERADD" != "$i" ] ;do + i=${USERADD%%;*} + USERADD="${USERADD#$i;}" + + USER_NAME=${i%%:*} + UID_GID="${i#$USER_NAME:}" + USER_UID="${UID_GID%%:*}" + USER_GID="${UID_GID#$USER_UID:}" + + useradd --home-dir "$BASE_DIR/$USER_NAME" --uid "$USER_UID" --gid "$USER_GID" "$USER_NAME" +done + exec /usr/sbin/sshd -D -e