switch user 'me' => 'app'
[webi-installers/.git] / ssh-utils / ssh-adduser.sh
1 #!/bin/bash
2
3 {
4     set -e
5     set -u
6
7     # TODO: a more complete VPS setup
8
9     # TODO would $EUID be better?
10     if [ "root" != "$(whoami)" ]; then
11       echo "webi adduser: running user is already a non-root user"
12       exit 0
13     fi
14
15     #apt-get -y update
16     #apt-get -y install curl wget rsync git
17
18     # Add User app
19     # Picking 'app' because that seems to be what the # Docker/Vagrant
20     # crowd is doing. TODO: Other ideas? me, user, tron
21     adduser --disabled-password --gecos "" app
22     my_password=$(openssl rand -hex 16)
23     printf "$my_password"'\n'"$my_password" | passwd app
24
25     # make 'app' a sudo-er (admin)
26     adduser app sudo
27     echo "app ALL=(ALL:ALL) NOPASSWD: ALL" | tee /etc/sudoers.d/app
28
29     # allow users who can already login as 'root' to login as 'app'
30     mkdir -p /home/app/.ssh/
31     chmod 0700 /home/app/.ssh/
32     cp -r "$HOME/.ssh/authorized_keys" /home/app/.ssh/
33     chmod 0600 /home/app/.ssh/authorized_keys
34     touch /home/app/.ssh/config
35     chmod 0644 /home/app/.ssh/config
36     chown -R app:app /home/app/.ssh/
37
38     # ensure that 'app' has an SSH Keypair
39     sudo -i -u app bash -c 'ssh-keygen -b 2048 -t rsa -f /home/app/.ssh/id_rsa -q -N ""'
40
41     # Install webi for the new 'app' user
42     sudo -i -u app bash -c 'curl -fsSL https://webinstall.dev/webi | bash' \
43         || sudo -i -u app bash -c 'wget -q -O - https://webinstall.dev/webi | bash'
44
45     # TODO ensure that ssh-password login is off
46
47     echo "Created user 'app' with password '$my_password'"
48 }