Thumbnail

mod/infra.git

Clone URL: https://git.buni.party/mod/infra.git

Viewing file on branch master

1#!/bin/sh
2
3
4usage() {
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
13fail() {
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
23read -r key
24[ -z "$key" ] && fail "Invalid SSH key"
25
26useradd -m -s /bin/bash "$1" || fail "Could not add user"
27
28# set up ssh stuff
29mkdir /home/"$1"/.ssh
30printf '%s' "$key" >> /home/"$1"/.ssh/authorized_keys
31touch /home/"$1"/.ssh/git_keys
32chmod 700 /home/"$1"/.ssh
33chown -R "$1":"$1" /home/"$1"/.ssh
34
35# set up web stuff
36setfacl -m 'g:www-data:x' /home/"$1"
37mkdir /home/"$1"/web
38# world readable
39chmod 775 /home/"$1"/web
40# add index file
41cat <<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>
50EOF
51
52chown -R "$1":"www-data" /home/"$1"/web
53chmod g+s /home/"$1"/web
54