diff options
author | Anirudh Oppiliappan <x@icyphox.sh> | 2019-11-17 14:54:16 +0530 |
---|---|---|
committer | Anirudh Oppiliappan <x@icyphox.sh> | 2019-11-17 14:54:16 +0530 |
commit | ec848fac2e389b04c481c53a994ac104ccb4aa03 (patch) | |
tree | f39f4fb186a5b764f4868b3910424010e4be733c /pw | |
download | pw-ec848fac2e389b04c481c53a994ac104ccb4aa03.tar.gz |
Init
Signed-off-by: Anirudh Oppiliappan <x@icyphox.sh>
Diffstat (limited to 'pw')
-rwxr-xr-x | pw | 108 |
1 files changed, 108 insertions, 0 deletions
@@ -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 | |||
7 | init() { | ||
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 | |||
17 | add() { | ||
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 | |||
36 | del() { | ||
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 | |||
45 | show() { | ||
46 | gpg2 --decrypt --quiet --use-agent "$PW_DIR/$1.gpg" | ||
47 | } | ||
48 | |||
49 | copy() { | ||
50 | show "$1" | xclip -selection clipboard | ||
51 | echo "pw: copied $1 to clipboard" | ||
52 | } | ||
53 | |||
54 | usage() { | ||
55 | usage=" | ||
56 | pw - mnml password manager | ||
57 | |||
58 | usage: pw [options] | ||
59 | All options except -i and -h require a NAME argument. | ||
60 | |||
61 | options: | ||
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 | |||
70 | Requires PW_KEY to be set. Optionally, set PW_DIR for custom directory location. | ||
71 | " | ||
72 | |||
73 | echo "$usage" | ||
74 | exit 1 | ||
75 | } | ||
76 | |||
77 | die() { | ||
78 | printf "error: %s\n" "$1" | ||
79 | exit 1 | ||
80 | } | ||
81 | |||
82 | |||
83 | main() { | ||
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 | |||
108 | main "$@" | ||