]> Sergey Matveev's repositories - passman.git/blobdiff - passman
Lower case keys
[passman.git] / passman
diff --git a/passman b/passman
index 31537932ea98fd9c95e6c4a1a25a1ddd674501ef..0032fcba22c464c85c46f60a892c2198e38a09bc 100755 (executable)
--- a/passman
+++ b/passman
@@ -1,11 +1,10 @@
 #!/bin/sh -e
 # passman -- simple password manager
-# Copyright (C) 2013-2015 Sergey Matveev (stargrave@stargrave.org)
+# Copyright (C) 2013-2025 Sergey Matveev <stargrave@stargrave.org>
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
+# the Free Software Foundation; version 3 of the License.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-DB=~/.passmandb
+DB=${PASSMANDB:-$HOME/.passmandb}
 
-generator()
-{
-    dd if=/dev/urandom bs=16 count=1 2>/dev/null | base64 | tr '+/' '-_' | tr -d =
+dec() {
+    if [ -s $DB ] ; then
+        # age -d $DB | zstd -d
+        cmenctool -p -d <$DB | zstd -d
+    else
+        cat <<EOF
+%rec: credential
+%key: name
+%mandatory: name passwd
+%allowed: data
+EOF
+    fi
+}
+
+enc() {
+    # zstd | age -p
+    zstd | cmenctool -p -embed
 }
 
-cliper()
-{
+generator() {
+    dd if=/dev/random bs=16 count=1 status=none | base64 | tr "+/" "-_" | tr -d =
+}
+
+cliper() {
     xclip -in -selection $1
 }
 
+tmp=`mktemp`.rec
+trap "rm -f $tmp ${DB}.tmp" HUP PIPE INT QUIT TERM EXIT
+
+finder() {
+    dec | recsel -e "name ~ '$1'" >$tmp
+    [ $(recsel -c $tmp) -eq 1 ] || {
+        recsel -C -P name $tmp
+        exit 1
+    }
+}
+
+commit() {
+    enc <$tmp >${DB}.tmp
+    fsync ${DB}.tmp
+    mv ${DB}.tmp $DB
+}
+
 case "$1" in
+has)
+    finder "$2"
+    ;;
 gen)
     generator
     ;;
@@ -36,27 +72,23 @@ add)
     passwd="$3"
     [ -n "$dst" ]
     [ -n "$passwd" ] || passwd=$(generator)
-    echo Adding password for $dst
-    mkdir -p $DB/$dst
-    echo "$passwd" > $DB/$dst/passwd
+    echo ${dst}...
+    dec | recins --verbose -t credential -f name -v "$dst" -f passwd -v "$passwd" >$tmp
+    commit
+    ;;
+mod)
+    dec >$tmp
+    $EDITOR $tmp
+    commit
     ;;
 *)
-    cd $DB
-    result=$(find . -type d -path "*$1*" | while read ent; do
-        [ -e "$ent/passwd" ] || continue
-        echo "$ent" | sed 's#^\./##'
-    done)
-    if [ $(echo "$result" | wc -l) -ne 1 ]; then
-        echo "$result"
-        exit
-    fi
-    echo $result
-    if [ -e "$result"/data ]; then
-        echo -n "Associated data: "
-        cat "$result"/data
-    fi
-    perl -ne 'chop and print' "$result"/passwd | cliper clipboard
-    echo $(basename "$result") | perl -ne 'chop and print' | cliper primary
+    finder "$1"
+    name=$(recsel -P name $tmp)
+    echo $name >&2
+    data=$(recsel -P data $tmp)
+    [ -z "$data" ] || echo "Associated data: $data"
+    echo -n $(recsel -P passwd $tmp) | cliper clipboard
+    echo -n ${name##*/} | cliper primary
     sleep 10
     echo -n | cliper clipboard
     ;;