From ad8a56789de863e8a04dce4711cc92f3f62e267a Mon Sep 17 00:00:00 2001
From: Thoralf Gutierrez <thoralfgutierrez@gmail.com>
Date: Mon, 22 May 2017 17:53:22 +0200
Subject: [PATCH] Fix chown commands on alpine for user names with a dot

---
 entrypoint | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/entrypoint b/entrypoint
index 861a328..98161f0 100755
--- a/entrypoint
+++ b/entrypoint
@@ -61,6 +61,10 @@ function createUser() {
     chown root:root /home/$user
     chmod 755 /home/$user
 
+    # Retrieving user id to use it in chown commands instead of the user name
+    # to avoid problems on alpine when the user name contains a '.'
+    uid=`id $user -u`
+
     if [ -n "$pass" ]; then
         echo "$user:$pass" | chpasswd $chpasswdOptions
     else
@@ -70,7 +74,7 @@ function createUser() {
     # Add SSH keys to authorized_keys with valid permissions
     if [ -d /home/$user/.ssh/keys ]; then
         cat /home/$user/.ssh/keys/* >> /home/$user/.ssh/authorized_keys
-        chown $user /home/$user/.ssh/authorized_keys
+        chown $uid /home/$user/.ssh/authorized_keys
         chmod 600 /home/$user/.ssh/authorized_keys
     fi
 
@@ -81,7 +85,7 @@ function createUser() {
             dirPath=/home/$user/$dirPath
             echo "Creating and/or setting permissions on $dirPath"
             mkdir -p $dirPath
-            chown -R $user:users $dirPath
+            chown -R $uid:users $dirPath
         done
     fi
 }