aboutsummaryrefslogtreecommitdiff
path: root/usergen
blob: eac072b40095203f43803ddeccb3f28a7d641a9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /bin/sh

set -e

usage()
{
    echo "$0 <dir>"
}

to_hex()
{
    od -An -t x1 | tr -d ' ' | tr -d '\n'
}

to_bin()
{
    sed -e 's,\([0-9a-f]\{2\}\),\\\\\\x\1,g' | xargs printf
}

mktemp_posix()
{
    m4 <<EOF
mkstemp(${TMPDIR:-/tmp}/tmp.XXXXXX)
EOF
}

if [ $# != 1 ]; then
    usage >&2
    exit 1
fi

DIR=$1

echo Username: >&2
IFS= read -r USER

if printf '%s' "$USER" | grep -qe '[[:space:]]'
then
    echo Username cannot contain whitespaces >&2
    exit 1
fi

DB="$DIR/db.json"

if jq '.users[].name' "$DB" | grep -q $USER
then
    echo User $USER already in $DB >&2
    exit 1
fi

TTYCFG=$(stty -g)
trap "stty $TTYCFG" INT QUIT TERM EXIT
stty -echo
echo Password: >&2
IFS= read -r PWD
stty echo
# Force newline
echo

echo "Quota, in MiB (leave empty for unlimited quota):" >&2
read -r QUOTA
QUOTA="${QUOTA:+"$(printf '%d' "$QUOTA")"}"

PWD=$(printf '%s' "$PWD" | to_hex)
SALT=$(openssl rand -hex 32)
KEY=$(openssl rand -hex 32)
PWD=$(printf '%s%s' "$SALT" "$PWD")

ROUNDS=1000

for i in $(seq $ROUNDS)
do
    printf "\r%d/$ROUNDS" $i >&2
    PWD=$(printf '%s' "$PWD" | to_bin | openssl sha256 -r | cut -d' ' -f1)
done

echo >&2
TMP=$(mktemp_posix)

cleanup()
{
    rm -f $TMP
}

trap cleanup EXIT

jq ".users += [
{
    \"name\": \"$USER\",
    \"password\": \""$PWD"\",
    \"salt\": \"$SALT\",
    \"key\": \"$KEY\",
    \"quota\": \"$QUOTA\"
}]" "$DB" > $TMP

mkdir -p "$DIR/user/$USER"
mv $TMP "$DB"