| 1 | #!/bin/sh |
| 2 | |
| 3 | |
| 4 | usage() { |
| 5 | echo "Usage:" |
| 6 | echo "newuser NAME" |
| 7 | echo "Creates a new user account with the given name." |
| 8 | echo "An SSH key must be given in stdin." |
| 9 | echo "This must be run as root." |
| 10 | exit 0 |
| 11 | } |
| 12 | |
| 13 | fail() { |
| 14 | printf '%s: %s\n' "$0" "$*" >&2 |
| 15 | exit 1 |
| 16 | } |
| 17 | |
| 18 | |
| 19 | [ $# -lt 1 ] && usage |
| 20 | [ "$(id -u)" -ne 0 ] && fail "Must be run as root" |
| 21 | [ -z "$1" ] && fail "Invalid user name" |
| 22 | |
| 23 | read -r key |
| 24 | [ -z "$key" ] && fail "Invalid SSH key" |
| 25 | |
| 26 | useradd -m -s /bin/bash "$1" || fail "Could not add user" |
| 27 | |
| 28 | # set up ssh stuff |
| 29 | mkdir /home/"$1"/.ssh |
| 30 | printf '%s' "$key" >> /home/"$1"/.ssh/authorized_keys |
| 31 | touch /home/"$1"/.ssh/git_keys |
| 32 | chmod 700 /home/"$1"/.ssh |
| 33 | chown -R "$1":"$1" /home/"$1"/.ssh |
| 34 | |
| 35 | # set up web stuff |
| 36 | setfacl -m 'g:www-data:x' /home/"$1" |
| 37 | mkdir /home/"$1"/web |
| 38 | # world readable |
| 39 | chmod 775 /home/"$1"/web |
| 40 | # add index file |
| 41 | cat <<EOF > /home/"$1"/web/index.html |
| 42 | <!DOCTYPE html> |
| 43 | <html> |
| 44 | <head></head> |
| 45 | <body> |
| 46 | <p>Hello, world!</p> |
| 47 | <p>Click <a href="https://u.buni.party/mod/">here</a> for more info.</p> |
| 48 | </body> |
| 49 | </html> |
| 50 | EOF |
| 51 | |
| 52 | chown -R "$1":"www-data" /home/"$1"/web |
| 53 | chmod g+s /home/"$1"/web |
| 54 | |