#!/usr/bin/env perl # # Copyright (c) 2022, 2023 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; use File::Temp qw(tempfile); my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store'; my $gpg = $ENV{'PLASS_GPG'} // 'gpg'; my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent); my %subcmd = ( cat => [\&cmd_cat, "entries..."], edit => [\&cmd_edit, "entry"], find => [\&cmd_find, "[pattern]"], mv => [\&cmd_mv, "from to"], rm => [\&cmd_rm, "entries..."], tee => [\&cmd_tee, "[-q] entry"], ); 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; } 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; print $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 unless -f && m,.gpg$,; s,^$store/*,,; s,.gpg$,,; return if defined($pattern) && ! m/$pattern/i; 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_edit { GetOptions('h|?' => \&usage) or usage; usage if @ARGV != 1; my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed'; my $entry = shift @ARGV; my $epath = name2file $entry; my ($fh, $filename) = tempfile "/tmp/plass-XXXXXXXXXX"; open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!"; open (STDOUT, ">&", $fh) or die "can't redirect stdout to $filename"; system ($gpg, @gpg_flags, '-d', $epath); die "$gpg exited with $?\n" if $? != 0; # restore stdout so the editor can access the TTY if needed. open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!"; my $oldtime = (stat($fh))[9]; system ($editor, $filename); die "editor $editor failed\n" if $? != 0; my $newtime = (stat($filename))[9]; if ($oldtime == $newtime) { say STDERR "no changes made."; unlink $filename or die "can't delete $filename: $!\n"; return } open(STDIN, '<', $filename) or die "can't redirect stdin: $!"; open(STDOUT, '>', $epath) or die "can't redirect stdout: $!"; system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-'); unlink $filename or die "can't delete $filename: $!\n"; die "gpg failed" if $? != 0; got_add $epath; got_ci "update $entry"; } sub cmd_find { GetOptions('h|?' => \&usage) or usage; usage if @ARGV > 1; map { say $_ } passfind(shift @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 $msg = -f $file ? "update $name" : "+$name"; my $pass = ""; $pass .= $_ while ; die "No content!\n" if $pass eq ""; writepass($file, $pass); got_add $file; got_ci $msg; print $pass unless $q; }