aboutsummaryrefslogtreecommitdiff
path: root/backend/cargohold-cli
blob: 23f5c19ce228ff13ef85e2ef08353db60f2a8968 (plain)
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
#!/usr/bin/env bash
DATABASE=${DATABASE:-cargohold.db3}
BASEURL=${BASEURL:-https://files.stumpf.es}

usage(){
	printf "This utility can be used to manage the cargohold database from the command line\n\n"
	printf "Cargohold database used: %s\n\n" "$DATABASE"
	printf "Usage:\n"
	printf "\t%s adduser <name> [<limit>]\n" "$0"
	printf "\tExample: %s adduser foo 10000\n" "$0"
	printf "\tThis will add user 'foo' with a storage limit of 10K bytes\n\n"
	printf "\t%s addalias <user> <directory> <access> [<alias>]\n" "$0"
	printf "\tExample: %s addalias foo mydirectory c send-me-files\n" "$0"
	printf "\tThis will make the directory 'mydirectory' under the fileroot (and optionally userdir) available for uploads ('c') under the alias 'send-me-files'\n\n"
	printf "\t%s list\n" "$0"
	printf "\tList all users and configured aliases\n"
}

# adduser <name> [<limit>]
adduser(){
	if [ -z "$1" ]; then
		usage
		exit 1
	fi
	limit=${2:-0}

	printf "Adding user %s\n" "$1"
	sqlite3 "$DATABASE" "PRAGMA foreign_keys = ON; INSERT INTO users (name, storage) VALUES ('$1', $limit);"
}

# addalias <user> <real> <access> [<alias>]
addalias(){
	if [ "$#" -lt 3 ]; then
		usage
		exit 1
	fi
	alias=${4:-$(xxd -g 0 -l 16 -p /dev/urandom)}

	printf "Adding alias %s/%s pointing to %s (ACL %s, User %s)\n" "$BASEURL" "$alias" "$2" "$3" "$1"
	sqlite3 "$DATABASE" "PRAGMA foreign_keys = ON; INSERT INTO aliases (alias, user, real, access) VALUES ('$alias', '$1', '$2', '$3');"
}

list(){
	printf "Listing all users\n"
	sqlite3 -column -header "$DATABASE" "SELECT name, storage FROM users;"

	printf "\nListing all aliases\n"
	sqlite3 -column "$DATABASE" "SELECT user || ': ' || '$BASEURL/' || alias || ' (' || access || ') -> ' || real FROM aliases;"
}

if [ "$#" -lt 1 ]; then
	usage
	exit 0
fi

case "$1" in
	"adduser")
		shift
		adduser $@
		;;
	"addalias")
		shift
		addalias $@
		;;
	"list")
		list
		;;
	*)
		usage
		exit 1
		;;
esac