cours-snt/test-python-ssh/entrypoint.sh

71 lines
1.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
HOME_BASE="/app/python_app/modules"
USERS_LIST="/app/config/users.txt"
PASSWD_LIST="/app/config/passwords.txt"
CUSTOM_SCRIPT="/app/config/init.sh"
separator="=" # Must be ascii for cut
forbidden_chars=". /"
# Check we got user list
if [ ! -f "$USERS_LIST" ] && [ ! -f "$PASSWD_LIST" ] ; then
echo "Les fichiers des utilisateurs ou des passwords nont pas étés trouvées."
exit 1
fi
for c in $forbidden_chars ; do
for file in "$USERS_LIST" "$PASSWD_LIST" ; do
if [ -n "$(cat "$USERS_LIST" | grep -F $c)" ] ; then
echo "Le fichier « $file » ne doit pas contenir le caractère « $c » !"
exit 1
fi
done
done
# Generate passwords if not done yet
genPassowrd () {
tr -dc A-Za-z0-9 </dev/urandom | head -c $1
}
if [ ! -f $PASSWD_LIST ] ; then
for user in $(cat "$USERS_LIST") ; do
echo "$user$separator$(genPassowrd 10)" >> $PASSWD_LIST
done
fi
# Create users, home dirs, change passwords and home owners
for line in $(cat $PASSWD_LIST) ; do
name="$(echo "$line" | cut -d "$separator" -f 1)"
pass="$(echo "$line" | cut -d "$separator" -f 2)"
home="$HOME_BASE/$name"
mkdir -p "$home"
#useradd --home-dir "$home" --no-user-group -G eleve --shell /bin/bash --root "$home" "$name"
useradd --home-dir "$home" --no-user-group -G eleve --shell /bin/bash "$name"
echo "$pass\n$pass" | passwd "$name" &> /dev/null
chown "$name":eleve "$home"
done
# Allow SSH as root
if [ -z "$(grep '^PermitRootLogin yes' /etc/ssh/sshd_config)" ] ; then
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
fi
echo "\nFin de la préparation des utilisateurs.\n"
# Custom script
if [ -f "$CUSTOM_SCRIPT" ] ; then
if [ ! -x "$CUSTOM_SCRIPT" ] ; then
chmod +x "$CUSTOM_SCRIPT"
fi
"$CUSTOM_SCRIPT"
fi
# Nginx
nginx -c '/etc/nginx/nginx.conf'
# SSH server
/usr/sbin/sshd -E /dev/stderr
# Start watever the container should be doing
/bin/sh -c "$*"