#!/bin/sh

echo $EUID

if [[ -z "$PAM_USER" || -d "/home/$PAM_USER" ]]
then
	exit 0
fi

mkdir -p "/home/$PAM_USER"
chown -R "$PAM_USER" "/home/$PAM_USER"
chgrp -R "$PAM_USER" "/home/$PAM_USER"

sudo --user "$PAM_USER" /usr/bin/ssh-keygen -t ecdsa -N "" -b 521 -f "/home/$PAM_USER/.ssh/id_ecdsa" > /dev/null
sudo --user "$PAM_USER" /usr/bin/cp /opt/ssh_config "/home/$PAM_USER/.ssh/config"
cat "/home/$PAM_USER/.ssh/id_ecdsa.pub" | sudo --user "$PAM_USER" /bin/tee "/home/$PAM_USER/.ssh/authorized_keys" > /dev/null
