|
6 | 6 | set -x |
7 | 7 | fi |
8 | 8 |
|
| 9 | +# Function to create user and group if needed |
| 10 | +create_user_and_group() { |
| 11 | + local uid="$1" |
| 12 | + local gid="$2" |
| 13 | + local username="openconext" |
| 14 | + local groupname="openconext" |
| 15 | + |
| 16 | + # Check if the group already exists (when gid is provided) |
| 17 | + if [ -n "$gid" ]; then |
| 18 | + if getent group "$groupname" > /dev/null 2>&1; then |
| 19 | + # Group exists, check if GID matches |
| 20 | + existing_gid=$(getent group "$groupname" | cut -d: -f3) |
| 21 | + if [ "$existing_gid" != "$gid" ]; then |
| 22 | + echo "ERROR: Group '$groupname' already exists with GID $existing_gid, but requested GID is $gid" >&2 |
| 23 | + echo " Please recreate the container with the updated gid" >&2 |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | + echo "Group '$groupname' already exists with correct GID $gid" |
| 27 | + else |
| 28 | + # Group doesn't exist, create it |
| 29 | + echo "Creating group '$groupname' with GID $gid" |
| 30 | + groupadd -g "$gid" "$groupname" |
| 31 | + fi |
| 32 | + fi |
| 33 | + |
| 34 | + # Check if the user already exists |
| 35 | + if getent passwd "$username" > /dev/null 2>&1; then |
| 36 | + # User exists, check if UID matches |
| 37 | + existing_uid=$(getent passwd "$username" | cut -d: -f3) |
| 38 | + if [ "$existing_uid" != "$uid" ]; then |
| 39 | + echo "ERROR: User '$username' already exists with UID $existing_uid, but requested UID is $uid" >&2 |
| 40 | + echo " Please recreate the container with the updated uid" >&2 |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + # If GID is provided, check if user's primary group matches |
| 45 | + if [ -n "$gid" ]; then |
| 46 | + existing_primary_gid=$(getent passwd "$username" | cut -d: -f4) |
| 47 | + if [ "$existing_primary_gid" != "$gid" ]; then |
| 48 | + echo "ERROR: User '$username' already exists with primary GID $existing_primary_gid, but requested GID is $gid" >&2 |
| 49 | + echo " Please recreate the container with the updated gid" >&2 |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + fi |
| 53 | + |
| 54 | + echo "User '$username' already exists with correct UID $uid" |
| 55 | + else |
| 56 | + # User doesn't exist, create it |
| 57 | + if [ -n "$gid" ]; then |
| 58 | + echo "Creating user '$username' with UID $uid and GID $gid" |
| 59 | + useradd -M -u "$uid" -g "$gid" "$username" |
| 60 | + else |
| 61 | + echo "Creating user '$username' with UID $uid" |
| 62 | + useradd -M -u "$uid" "$username" |
| 63 | + fi |
| 64 | + fi |
| 65 | + |
| 66 | + # Return the appropriate privilege dropping command |
| 67 | + if [ -n "$gid" ]; then |
| 68 | + echo "runuser --user=$username --group=$groupname -- " |
| 69 | + else |
| 70 | + echo "runuser --user=$username -- " |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | + |
9 | 75 | # handle privilege dropping |
10 | 76 | if [ $UID -ne 0 ] |
11 | 77 | then |
|
15 | 81 | exit 1 |
16 | 82 | fi |
17 | 83 |
|
| 84 | +# set up privilege dropping to user and group |
| 85 | +PRIVDROP= |
| 86 | +if [ -n "$RUNAS_UID" ] |
| 87 | +then |
| 88 | + PRIVDROP=$(create_user_and_group "$RUNAS_UID" "$RUNAS_GID") |
| 89 | + echo "Dropping privileges to $($PRIVDROP id -u):$($PRIVDROP id -g)" |
| 90 | +fi |
| 91 | + |
18 | 92 | # run custom scripts before dropping privileges |
19 | 93 | echo "Running custom scripts in /container-init as root" |
20 | 94 | if [ -d "/container-init" ] |
|
23 | 97 | run-parts --verbose --regex '.*' "/container-init" |
24 | 98 | fi |
25 | 99 |
|
26 | | -# set up privilege dropping to user and group |
27 | | -PRIVDROP= |
28 | | -if [ -n "$RUNAS_UID" ] |
29 | | -then |
30 | | - if [ -n "$RUNAS_GID" ] |
31 | | - then |
32 | | - echo "Switching to user $RUNAS_UID and group $RUNAS_GID" |
33 | | - groupadd -g $RUNAS_GID openconext |
34 | | - useradd -M -u $RUNAS_UID -g $RUNAS_GID openconext |
35 | | - PRIVDROP="runuser --user=openconext --group=openconext -- " |
36 | | - else |
37 | | - echo "Switching to user $RUNAS_UID" |
38 | | - useradd -M -u $RUNAS_UID openconext |
39 | | - PRIVDROP="runuser --user=openconext -- " |
40 | | - fi |
41 | | - echo "Dropping privileges to $($PRIVDROP id -u):$($PRIVDROP id -g)" |
42 | | -fi |
43 | | - |
44 | 100 | # run custom scripts after dropping privileges |
45 | 101 | echo "Running custom scripts in /container-init-post" |
46 | 102 | if [ -d "/container-init-post" ] |
|
0 commit comments