#! /bin/sh set -e usage() { echo "$0 " } 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 PWD=$(printf '%s' "$PWD" | xxd -p | tr -d '\n') SALT=$(openssl rand 32 | xxd -p | tr -d '\n') KEY=$(openssl rand 32 | xxd -p | tr -d '\n') PWD=$(printf '%s%s' $SALT "$PWD") ROUNDS=1000 for i in $(seq $ROUNDS) do printf "\r%d/$ROUNDS" $i >&2 PWD=$(printf '%s' "$PWD" | xxd -p -r | sha256sum | cut -d' ' -f1) done echo >&2 TMP=$(mktemp) cleanup() { rm -f $TMP } trap cleanup EXIT jq ".users += [ { \"name\": \"$USER\", \"password\": \""$PWD"\", \"salt\": \"$SALT\", \"key\": \"$KEY\", \"quota\": \"$QUOTA\" }]" "$DB" > $TMP mv $TMP "$DB" mkdir "$DIR/user/$USER"