#!/usr/bin/env perl # # Copyright (c) 2022 Omar Polo # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. use strict; use warnings; use v5.32; use open ":std", ":encoding(UTF-8)"; use Getopt::Long qw(:config bundling require_order); use File::Basename; use File::Find; my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store'; my $got = $ENV{'PLASS_GOT'} // 'got'; my $tog = $ENV{'PLASS_TOG'} // 'tog'; my $gpg = $ENV{'PLASS_GPG'} // 'gpg2'; my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent); my $default_chars = $ENV{'PLASS_CHARS'} // '!-~'; my $default_length = $ENV{'PLASS_LENGTH'}; if (!defined($default_length) || $default_length <= 0) { $default_length = 32; } my %subcmd = ( cat => [\&cmd_cat, "entries..."], find => [\&cmd_find, "[pattern]"], gen => [\&cmd_gen, "[-nq] [-c chars] [-l length] entry"], got => [\&cmd_got, "args ..."], mv => [\&cmd_mv, "from to"], rm => [\&cmd_rm, "entries..."], tee => [\&cmd_tee, "[-q] entry"], tog => [\&cmd_tog, "args ..."], ); my $usage = "[-h] [command argument ...]"; my $cmd; sub usage { my $prog = basename $0; if (defined($cmd) and defined($subcmd{$cmd})) { say STDERR "Usage: $prog $cmd $usage"; } else { say STDERR "Usage: $prog $usage"; say STDERR "unknown command $cmd" if defined($cmd); say STDERR "commands: ", join(' ', sort(keys %subcmd)); } exit 1; } GetOptions("h|?" => \&usage) or usage(); $cmd = shift // 'find'; usage() unless defined $subcmd{$cmd}; my $fn; ($fn, $usage) = @{$subcmd{$cmd}}; chdir $store; $fn->(); exit 0; # utils sub name2file { my $f = shift; $f .= ".gpg" unless $f =~ m,\.gpg$,; return $f; } # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none sub gen { my ($chars, $length) = @_; my $pass = ""; open(my $fh, '<:raw', '/dev/random') or die "can't open /dev/random: $!"; my $l = $length; while ($l >= 0) { read($fh, my $t, $length * 4) or die "failed to read /dev/random: $!"; $t =~ s/[^$chars]//g; $l -= length($t); $pass .= $t; } close($fh); return substr($pass, 0, $length); } sub readpass { # todo some stty black magic to avoid echo print shift if -t; my $pass = <>; die "failed to read stdin: $!" unless defined($pass); chomp $pass; return $pass; } sub mkdirs { my $dir = shift; my $parent = dirname $dir; mkdirs($parent) unless -d $parent || $parent eq '/'; mkdir $dir or die "mkdir $dir: $!" unless -d $dir; } sub writepass { my ($file, $pass) = @_; mkdirs(dirname $file); # temporary redirect stdout to $file open(my $stdout, '>&', STDOUT); open(STDOUT, '>', $file); my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-'); open my $fh, '|-', @args; say $fh "$pass"; close($fh); my $ok = !$?; open(STDOUT, '>&', $stdout); # restore stdout die "failed to run $gpg\n" unless $ok; } sub recipient { open my $fh, '<', "$store/.gpg-id" or die "can't open recipient file"; my $r = <$fh>; chomp $r; close($fh); return $r; } sub passfind { my $pattern = shift; my @entries; find({ wanted => sub { if (m,/.git$, || m,/.got$,) { $File::Find::prune = 1; return; } return if defined($pattern) && ! m/$pattern/; return unless -f && m,.gpg$,; s,^$store/*,,; s,.gpg$,,; push @entries, $_; }, no_chdir => 1, follow_fast => 1, }, ($store)); return sort(@entries); } sub got { # discard stdout open my $fh, '-|', ($got, @_); close($fh); return !$?; } sub got_add { return got 'add', '-I', shift; } sub got_rm { got 'remove', '-f', shift or exit(1); } sub got_ci { my $pid = fork; die "failed to fork: $!" unless defined $pid; if ($pid != 0) { wait; die "failed to commit changes" if $?; return; } open (STDOUT, ">&", \*STDERR) or die "can't redirect stdout to stderr"; exec ($got, 'commit', '-m', shift) or die "failed to exec $got: $!"; } # cmds sub cmd_cat { GetOptions('h|?' => \&usage) or usage; usage unless @ARGV; while (@ARGV) { my $file = name2file(shift @ARGV); system ($gpg, @gpg_flags, '-d', $file); die "failed to exec $gpg: $!" if $? == -1; } } sub cmd_find { GetOptions('h|?' => \&usage) or usage; usage if @ARGV > 1; map { say $_ } passfind(shift @ARGV); } sub cmd_gen { my $chars = $default_chars; my $length = $default_length; my $nop; my $q; GetOptions( 'c=s' => sub { $chars = $_[1] }, 'h|?' => \&usage, 'l=i' => sub { $length = $_[1] }, 'n' => \$nop, 'q' => \$q, ) or usage; usage if @ARGV != 1; my $name = shift @ARGV; my $file = name2file $name; my $renamed = -f $file; my $pass = gen($chars, $length); unless ($nop) { writepass($file, $pass); got_add $file; got_ci($renamed ? "update $name" : "+$name"); } say $pass unless $q; } sub cmd_got { exec $got, @ARGV; } # TODO: handle moving directories? sub cmd_mv { GetOptions('h|?' => \&usage) or usage; usage if @ARGV != 2; my $a = shift @ARGV; my $b = shift @ARGV; my $pa = name2file $a; my $pb = name2file $b; die "source password doesn't exist" unless -f $pa; die "target password exists" if -f $pb; mkdirs(dirname $pb); rename $pa, $pb or die "can't rename $a to $b: $!"; got_rm $pa; got_add $pb or die "can't add $pb\n"; got_ci "mv $a $b"; } sub cmd_rm { GetOptions('h|?' => \&usage) or usage; usage unless @ARGV; while (@ARGV) { my $name = shift @ARGV; my $file = name2file $name; got_rm $file; got_ci "-$name"; } } sub cmd_tee { my $q; GetOptions( 'h|?' => \&usage, 'q' => \$q, ) or usage; usage if @ARGV != 1; my $name = shift @ARGV; my $file = name2file $name; my $pass = readpass "Enter the password: "; writepass($file, $pass); got_add $file; got_ci (-f $file ? "update $name" : "+$name"); say $pass unless $q; } sub cmd_tog { exec $tog, @ARGV; }