]> Sergey Matveev's repositories - passman.git/blob - passman
0032fcba22c464c85c46f60a892c2198e38a09bc
[passman.git] / passman
1 #!/bin/sh -e
2 # passman -- simple password manager
3 # Copyright (C) 2013-2025 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; version 3 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 DB=${PASSMANDB:-$HOME/.passmandb}
18
19 dec() {
20     if [ -s $DB ] ; then
21         # age -d $DB | zstd -d
22         cmenctool -p -d <$DB | zstd -d
23     else
24         cat <<EOF
25 %rec: credential
26 %key: name
27 %mandatory: name passwd
28 %allowed: data
29 EOF
30     fi
31 }
32
33 enc() {
34     # zstd | age -p
35     zstd | cmenctool -p -embed
36 }
37
38 generator() {
39     dd if=/dev/random bs=16 count=1 status=none | base64 | tr "+/" "-_" | tr -d =
40 }
41
42 cliper() {
43     xclip -in -selection $1
44 }
45
46 tmp=`mktemp`.rec
47 trap "rm -f $tmp ${DB}.tmp" HUP PIPE INT QUIT TERM EXIT
48
49 finder() {
50     dec | recsel -e "name ~ '$1'" >$tmp
51     [ $(recsel -c $tmp) -eq 1 ] || {
52         recsel -C -P name $tmp
53         exit 1
54     }
55 }
56
57 commit() {
58     enc <$tmp >${DB}.tmp
59     fsync ${DB}.tmp
60     mv ${DB}.tmp $DB
61 }
62
63 case "$1" in
64 has)
65     finder "$2"
66     ;;
67 gen)
68     generator
69     ;;
70 add)
71     dst="$2"
72     passwd="$3"
73     [ -n "$dst" ]
74     [ -n "$passwd" ] || passwd=$(generator)
75     echo ${dst}...
76     dec | recins --verbose -t credential -f name -v "$dst" -f passwd -v "$passwd" >$tmp
77     commit
78     ;;
79 mod)
80     dec >$tmp
81     $EDITOR $tmp
82     commit
83     ;;
84 *)
85     finder "$1"
86     name=$(recsel -P name $tmp)
87     echo $name >&2
88     data=$(recsel -P data $tmp)
89     [ -z "$data" ] || echo "Associated data: $data"
90     echo -n $(recsel -P passwd $tmp) | cliper clipboard
91     echo -n ${name##*/} | cliper primary
92     sleep 10
93     echo -n | cliper clipboard
94     ;;
95 esac