]> Sergey Matveev's repositories - passman.git/blob - passman
Exit immediately if no password exists
[passman.git] / passman
1 #!/bin/sh -e
2 # passman -- simple password manager
3 # Copyright (C) 2013-2016 Sergey Matveev (stargrave@stargrave.org)
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 DB=~/.passmandb
19
20 generator()
21 {
22     dd if=/dev/urandom bs=16 count=1 2>/dev/null | base64 | tr '+/' '-_' | tr -d =
23 }
24
25 cliper()
26 {
27     xclip -in -selection $1
28 }
29
30 case "$1" in
31 gen)
32     generator
33     ;;
34 add)
35     dst="$2"
36     passwd="$3"
37     [ -n "$dst" ]
38     [ -n "$passwd" ] || passwd=$(generator)
39     echo Adding password for $dst
40     mkdir -p $DB/$dst
41     echo "$passwd" > $DB/$dst/passwd
42     ;;
43 *)
44     cd $DB
45     result=$(find . -type d -path "*$1*" | while read ent; do
46         [ -e "$ent/passwd" ] || continue
47         echo "$ent" | sed 's#^\./##'
48     done)
49     if [ $(echo "$result" | wc -l) -ne 1 ]; then
50         echo "$result"
51         exit
52     fi
53     [ -n "$result" ] || exit 1
54     echo $result
55     if [ -e "$result"/data ]; then
56         echo -n "Associated data: "
57         cat "$result"/data
58     fi
59     perl -ne 'chop and print' "$result"/passwd | cliper clipboard
60     echo $(basename "$result") | perl -ne 'chop and print' | cliper primary
61     sleep 10
62     echo -n | cliper clipboard
63     ;;
64 esac