server 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/bash
  2. # Check if username is set
  3. if [ -z "$SSH_USERNAME" ]; then
  4. echo "INFO: Username not set. Using default"
  5. SSH_USERNAME="sftpuser"
  6. fi
  7. # Check if uid/guid is set
  8. if [ -z "$SSH_USERID" ]; then
  9. echo "INFO: UID/GUID not set. Using default"
  10. SSH_USERID=1337
  11. fi
  12. # Check if data directory is set
  13. if [ -z "$SSH_DATADIR_NAME" ]; then
  14. echo "INFO: Data dir not set. Using default"
  15. SSH_DATADIR_NAME="data"
  16. fi
  17. # Check if generate hostkeys is set
  18. if [ -z "$SSH_GENERATE_HOSTKEYS" ]; then
  19. echo "INFO: Generate hostkeys not set. Using default"
  20. SSH_GENERATE_HOSTKEYS="true"
  21. fi
  22. # Create group
  23. echo "INFO: Adding group ${SSH_USERNAME}"
  24. addgroup -g $SSH_USERID $SSH_USERNAME
  25. # Create user
  26. echo "INFO: Adding user ${SSH_USERNAME}"
  27. adduser -D -u $SSH_USERID -G $SSH_USERNAME $SSH_USERNAME
  28. # Set password if provided
  29. if [ -z "$SSH_PASSWORD" ]; then
  30. echo "INFO: Password not provided for user ${SSH_USERNAME}"
  31. passwd -u $SSH_USERNAME
  32. else
  33. echo "INFO: Setting password for user ${SSH_USERNAME}"
  34. echo $SSH_USERNAME:$SSH_PASSWORD | chpasswd > /dev/null
  35. sed -i "s/PasswordAuthentication\s[^ ]*/PasswordAuthentication yes/g" /etc/ssh/sshd_config
  36. fi
  37. # Set Port to listen on
  38. if [ ! -z "$SSH_PORT" ]; then
  39. echo "INFO: Setting Port to ${SSH_PORT}"
  40. sed -i "s/Port\s[^ ]*/Port ${SSH_PORT}/g" /etc/ssh/sshd_config
  41. fi
  42. # Change ownership and permissions of users home root dir
  43. echo "INFO: Change ownership and permissions of home directory"
  44. chown root:root /home/$SSH_USERNAME
  45. chmod 755 /home/$SSH_USERNAME
  46. # Create data dir and set read/write permission for user
  47. echo "INFO: Create and set permissions on data dir"
  48. mkdir -p /home/$SSH_USERNAME/$SSH_DATADIR_NAME
  49. chown $SSH_USERNAME /home/$SSH_USERNAME/$SSH_DATADIR_NAME
  50. chmod 777 /home/$SSH_USERNAME/$SSH_DATADIR_NAME
  51. # Add SSH keys to authorized_keys with valid permissions
  52. if [ -d /home/$SSH_USERNAME/.ssh/keys ]; then
  53. echo "INFO: Set ownership and permission of .ssh directory"
  54. chown -R root:root /home/$SSH_USERNAME/.ssh
  55. chmod 755 /home/$SSH_USERNAME/.ssh
  56. echo "INFO: Add SSH keys to authorized_keys with valid permissions"
  57. cat /home/$SSH_USERNAME/.ssh/keys/* >> /home/$SSH_USERNAME/.ssh/authorized_keys
  58. chown $SSH_USERNAME:root /home/$SSH_USERNAME/.ssh/authorized_keys
  59. chmod 644 /home/$SSH_USERNAME/.ssh/authorized_keys
  60. fi
  61. # Generate host keys by default
  62. if [ "${SSH_GENERATE_HOSTKEYS,,}" == "true" ]; then
  63. echo "INFO: Generating host keys"
  64. mkdir -p /etc/ssh/host_keys/
  65. ssh-keygen -f /etc/ssh/host_keys/ssh_host_rsa_key -q -N '' -t rsa
  66. ln -s /etc/ssh/host_keys/ssh_host_rsa_key /etc/ssh/ssh_host_rsa_key
  67. ssh-keygen -f /etc/ssh/host_keys/ssh_host_dsa_key -q -N '' -t dsa
  68. ln -s /etc/ssh/host_keys/ssh_host_dsa_key /etc/ssh/ssh_host_dsa_key
  69. ssh-keygen -f /etc/ssh/host_keys/ssh_host_ecdsa_key -q -N '' -t ecdsa
  70. ln -s /etc/ssh/host_keys/ssh_host_ecdsa_key /etc/ssh/ssh_host_ecdsa_key
  71. ssh-keygen -f /etc/ssh/host_keys/ssh_host_ed25519_key -q -N '' -t ed25519
  72. ln -s /etc/ssh/host_keys/ssh_host_ed25519_key /etc/ssh/ssh_host_ed25519_key
  73. fi
  74. echo "INFO: Setting permissions on host keys"
  75. chmod 600 /etc/ssh/host_keys/*
  76. # Check for loglevel and replace line in sshd_config
  77. if [ -n "$LOGLEVEL" ]; then
  78. echo "INFO: Setting LogLevel to ${LOGLEVEL}"
  79. sed -i "s/LogLevel\s[^ ]*/LogLevel ${LOGLEVEL}/g" /etc/ssh/sshd_config
  80. fi
  81. # Run sshd in debug mode
  82. if [ -z "$DEBUG" ]; then
  83. exec /usr/sbin/sshd -D -e
  84. else
  85. echo "WARN: Debug mode enabled!"
  86. exec /usr/sbin/sshd -D -e -d
  87. fi