dotfiles

<-- duh.
git clone https://hhvn.uk/dotfiles
git clone git://hhvn.uk/dotfiles
Log | Files | Refs | Submodules | LICENSE

pass (2435B)


      1 #!/bin/rc
      2 # Copyright (c) 2022 hhvn <dev@hhvn.uk>
      3 
      4 if (~ $#PASS 0) {
      5 	PASS = $home/.local/pass
      6 }
      7 
      8 if (~ $DISPLAY ()) {
      9 	x11 = false
     10 } else {
     11 	x11 = true
     12 }
     13 
     14 fn send {
     15 	if (~ $x11 true) {
     16 		xclip
     17 	} else {
     18 		cat
     19 		printf '\n'
     20 	}
     21 }
     22 
     23 fn getpass {
     24 	if (! ~ $1 ()) {
     25 		if (~ $x11 true) {
     26 			p = (-p $1)
     27 			shift
     28 		} else {
     29 			p = $1
     30 			shift
     31 		}
     32 	} else {
     33 		p = ()
     34 	}
     35 
     36 	if (~ $x11 true) {
     37 		dmenu $p -P < /dev/null
     38 	} else {
     39 		printf '%s: ' $p >/dev/stderr
     40 
     41 		stty -echo
     42 		head -n 1 < /dev/tty
     43 		stty echo
     44 	}
     45 }
     46 
     47 fn choose {
     48 	if (! ~ $1 '-'*) {
     49 		if (~ $x11 true) {
     50 			p = (-p $1)
     51 			shift
     52 		} else {
     53 			p = (--prompt=$1)
     54 			shift
     55 		}
     56 	} else {
     57 		p = ()
     58 	}
     59 		
     60 	if (~ $x11 true) {
     61 		dmenu -i $p $*
     62 	} else {
     63 		fzf $p
     64 	}
     65 }
     66 
     67 dec = `''{getpass 'Master password' | gpg -qd --passphrase-fd 0 $PASS}
     68 
     69 if (!~ $#* 0) {
     70 	opt = $1
     71 	fromarg = 1
     72 } else {
     73 	opt = `$nl{printf '%s' $dec | awk -F: 'BEGIN {printf("GENERATE\nAPPEND\nEDIT\n")}; {print $1}' | choose -l 20}
     74 }
     75 
     76 switch ($opt) {
     77 case GENERATE
     78 	newpass = `''{tr -dc 'a-zA-Z0-9~!@$%^&*_+=-' < /dev/urandom | head -c 35}
     79 	newname = `$nl{choose 'Name' </dev/null}
     80 	if (~ $newname '') {
     81 		herbe 'dpass exiting... no pass given'
     82 		exit 1
     83 	}
     84 	dec     = `''{printf '%s%s: %s\n' $dec $newname $newpass}
     85 	encfile = /tmp/$pid.plain
     86 	touch $encfile
     87 	chmod 0700 $encfile
     88 	printf '%s' $dec > $encfile
     89 	gpg --armor -e --default-recipient-self < $encfile > $PASS
     90 	shred -u $encfile
     91 	printf '%s' $newpass | send
     92 	@{ sleep 25; send </dev/null } &
     93 case APPEND
     94 	newname = `$nl{choose 'Name' </dev/null}
     95 	newpass = `$nl{getpass 'Password'}
     96 	if (~ $newname '') {
     97 		herbe 'dpass exiting... no pass given'
     98 		exit 1
     99 	}
    100 	dec     = `''{printf '%s%s: %s\n' $dec $newname $newpass}
    101 	encfile = /tmp/$pid.plain
    102 	touch $encfile
    103 	chmod 0700 $encfile
    104 	printf '%s' $dec > $encfile
    105 	gpg --armor -e --default-recipient-self < $encfile > $PASS
    106 	shred -u $encfile
    107 	printf '%s' $newpass | send
    108 	@{ sleep 25; send </dev/null } &
    109 case EDIT
    110 	encfile = /tmp/$pid.plain
    111 	touch $encfile
    112 	chmod 0700 $encfile
    113 	printf '%s' $dec > $encfile
    114 	if (~ $EDITOR ()) { EDITOR=vim }
    115 	$EDITOR $encfile
    116 	gpg --armor -e --default-recipient-self < $encfile > $PASS
    117 	shred -u $encfile
    118 	printf '%s' $newpass | send
    119 	@{ sleep 25; send </dev/null } &
    120 case *
    121 	out = `$nl{printf '%s' $dec | awk -F: -v 'opt=' ^ $opt '$1 == opt { print $2 }' | sed 's/^.//'}
    122 	if (~ $fromarg 1) {
    123 		echo $out
    124 	} else {
    125 		printf '%s' $out | send
    126 		@{ sleep 25; send </dev/null } &
    127 	}
    128 }