aboutsummaryrefslogtreecommitdiff
path: root/pw
diff options
context:
space:
mode:
authorAnirudh Oppiliappan <x@icyphox.sh>2019-11-17 14:54:16 +0530
committerAnirudh Oppiliappan <x@icyphox.sh>2019-11-17 14:54:16 +0530
commitec848fac2e389b04c481c53a994ac104ccb4aa03 (patch)
treef39f4fb186a5b764f4868b3910424010e4be733c /pw
downloadpw-ec848fac2e389b04c481c53a994ac104ccb4aa03.tar.gz
Init
Signed-off-by: Anirudh Oppiliappan <x@icyphox.sh>
Diffstat (limited to 'pw')
-rwxr-xr-xpw108
1 files changed, 108 insertions, 0 deletions
diff --git a/pw b/pw
new file mode 100755
index 0000000..3357dc0
--- /dev/null
+++ b/pw
@@ -0,0 +1,108 @@
1#!/usr/bin/env bash
2# pw - a mnml password manager
3
4# export PW_DIR to your own path
5[[ -z "$PW_DIR" ]] && PW_DIR="$HOME/.pw"
6
7init() {
8 if [[ ! -e "$PW_DIR" ]]; then
9 mkdir -p "$PW_DIR"
10 echo "pw: password directory initialized at $PW_DIR"
11 else
12 echo "PW_DIR is $PW_DIR"
13 die "$PW_DIR exists"
14 fi
15}
16
17add() {
18 # $1: path to file
19 # $2 [optional]: password text
20 [[ -z "$PW_KEY" ]] && die "\$PW_KEY not set"
21
22 if [[ "$#" -eq 2 ]]; then
23 pass="$2"
24 else
25 pass="$(pwgen 25 1 -s)"
26 echo "pw: generated password for $1"
27 fi
28 if [[ ! -f "$PW_DIR/$1.gpg" ]]; then
29 echo "$pass" | gpg2 -er "$PW_KEY" -o "$PW_DIR/$1.gpg"
30 echo "pw: $PW_DIR/$1.gpg created"
31 else
32 die "the file $PW_DIR/$1.gpg exists"
33 fi
34}
35
36del() {
37 read -rn 1 -p "pw: are you sure you want to delete $1? [y/n]: "
38 printf "\n"
39 [[ "$REPLY" == [yY] ]] && {
40 rm -f "$PW_DIR/$1.gpg"
41 echo "pw: deleted $1"
42 }
43}
44
45show() {
46 gpg2 --decrypt --quiet --use-agent "$PW_DIR/$1.gpg"
47}
48
49copy() {
50 show "$1" | xclip -selection clipboard
51 echo "pw: copied $1 to clipboard"
52}
53
54usage() {
55 usage="
56pw - mnml password manager
57
58usage: pw [options]
59All options except -i and -h require a NAME argument.
60
61options:
62 -i Initializes password directory at \$HOME/.pw or at \$PW_DIR, if it exists.
63 -a Add a password.
64 -g Generate a password.
65 -s Print password to STDOUT.
66 -c Copy existing password to clipboard.
67 -d Delete password.
68 -h Display this help message and exit.
69
70Requires PW_KEY to be set. Optionally, set PW_DIR for custom directory location.
71"
72
73 echo "$usage"
74 exit 1
75}
76
77die() {
78 printf "error: %s\n" "$1"
79 exit 1
80}
81
82
83main() {
84 [[ -z "$1" ]] && {
85 usage
86 }
87
88 while getopts "ia:g:s:c:d:h" options
89 do
90 case "$options" in
91 i) init ;;
92 g) add "$OPTARG" ;;
93 a)
94 read -rsp "enter password: " pass
95 printf "\n"
96 add "$OPTARG" "$pass"
97 ;;
98 s) show "$OPTARG" ;;
99 c) copy "$OPTARG" ;;
100 d) del "$OPTARG" ;;
101 *|h) usage ;;
102 esac
103 done
104
105 shift $(( OPTIND -1 ))
106}
107
108main "$@"