-
Notifications
You must be signed in to change notification settings - Fork 0
/
rupm
executable file
·105 lines (90 loc) · 3.2 KB
/
rupm
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
98
99
100
101
102
103
104
105
#!/bin/bash
source "$(dirname "$0")/upm"
# usage $EXITCODE $MESSAGE
# Print usage and die() with the given message and exit code.
usage ()
{
cat << EOF >&2
Usage:
$(basename "$0") [-o | -c] [-h <hostname>] [-b <command>] [<key> | pass <key> | user <key> | both <key> | ls]
Remote access to upm, an ultra-minimalistic password manager.
Commands:
pass <key> (Default) Get password corresponding to the key.
If just the key is given, this is the default.
user <key> Get the username corresponding to the key.
both <key> Get the username and password, separated by a newline
corresponding to the key.
ls List all keys in vault. Outputs to stdout.
Options:
-h <hostname> Run upm with ssh in host <hostname>.
If this option is not given, the value of variable
'RUPM_HOST' from the environment or configuration
file is used.
-b <command> Use <command> as the upm binary in the remote host,
e.g. '~/bin/upm'.
If this option is not given, the value of variable
'REMOTE_UPM_BIN' from the environment or configuration
file is used.
-c Output to xclip (default).
-o Output to standard output.
Uses configuration from '$UPM_CONFFILE' if it exists.
EOF
echo "" >&2
die "$1" "$2"
}
[ $# -le 4 ] && [ $# -ge 1 ] || usage 1 "Error: Wrong number of arguments"
RCMD=pass
OUTPUT="xclip_output"
FILTER="tr -d \\n"
while [ $# -gt 0 ] ; do
PAR=$1
case $PAR in
pass) # get password (default)
RCMD=pass ;;
user) # get username
RCMD=user ;;
both) # get username and password
RCMD=both
FILTER=cat ;;
ls) # list keys
RCMD=ls
FILTER=cat
OUTPUT=cat ;;
-h) # remote host
RUPM_HOST="$PAR" ;;
-b) # remote command
REMOTE_UPM_BIN="$PAR" ;;
-o) # output to stdout
OUTPUT=cat ;;
-c) # output to xclip (default)
OUTPUT="xclip_output" ;;
-*)
usage 1 "Error: Unknown option ${PAR}." ;;
*)
KEY="$PAR" ;;
esac
shift
done
REMOTE_UPM_BIN=${REMOTE_UPM_BIN:-"upm"}
[ -r "$UPM_CONFFILE" ] && source "$UPM_CONFFILE"
[ -z "$RUPM_HOST" ] && usage 2 "Error: Remote host not given."
[ "$RCMD" != "ls" ] && [ -z "$KEY" ] \
&& usage 2 "Error: No key given."
[ "$RCMD" = "ls" ] && [ -n "$KEY" ] \
&& usage 2 "Error: Key argument given with '$(basename "$0") ls'."
[ "$OUTPUT" = "xclip_output" ] && ! xclip -h &> /dev/null \
&& die 1 "$(basename "$0") requires xclip for clipboard output."
if [ "$RCMD" = "both" ] && [ $OUTPUT = "xclip_output" ]; then
OUTPUT="xclip_double_output"
fi
[ -t 0 ] && stty -echo
ssh "${RUPM_HOST}" "${REMOTE_UPM_BIN} -o -p ${RCMD} ${KEY}" | $FILTER \
| $OUTPUT
UPM_EXIT="${PIPESTATUS[0]}"
[ -t 0 ] && stty echo
[ "${UPM_EXIT}" = "0" ] || exit "${UPM_EXIT}"
if [ "$OUTPUT" = "xclip_output" ] || [ "$OUTPUT" = "xclip_double_output" ] ; then
echo "Output sent to clipboard." >&2
elif [ "$OUTPUT" = "cat" ] && [ "$RCMD" != "ls" ]; then
echo "" >&2
fi