3 # Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 use open ":std", ":encoding(UTF-8)";
23 use Getopt::Long qw(:config bundling require_order);
26 use File::Path qw(make_path);
27 use File::Temp qw(tempfile);
29 my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
31 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
32 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
35 cat => [\&cmd_cat, "entries..."],
36 edit => [\&cmd_edit, "entry"],
37 find => [\&cmd_find, "[pattern]"],
38 mv => [\&cmd_mv, "from to"],
39 rm => [\&cmd_rm, "entries..."],
40 tee => [\&cmd_tee, "[-q] entry"],
43 my $usage = "[-h] command [argument ...]";
46 my $prog = basename $0;
47 if (defined($cmd) and defined($subcmd{$cmd})) {
48 say STDERR "Usage: $prog $cmd $usage";
50 say STDERR "Usage: $prog $usage";
51 say STDERR "unknown command $cmd" if defined($cmd);
52 say STDERR "commands: ", join(' ', sort(keys %subcmd));
57 GetOptions("h|?" => \&usage) or usage();
59 $cmd = shift // 'find';
60 usage() unless defined $subcmd{$cmd};
62 ($fn, $usage) = @{$subcmd{$cmd}};
72 $f .= ".gpg" unless $f =~ m,\.gpg$,;
77 my ($editor, $fh, $tempfile, $epath) = @_;
79 open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
80 open (STDOUT, ">&", $fh) or die "can't redirect stdout to $tempfile";
81 system ($gpg, @gpg_flags, '-d', $epath);
82 die "$gpg exited with $?\n" if $? != 0;
83 open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
85 my $oldtime = (stat($fh))[9];
88 system ($editor, $tempfile);
89 die "editor $editor failed\n" if $? != 0;
91 my $newtime = (stat($tempfile))[9];
92 if ($oldtime == $newtime) {
93 say STDERR "no changes made.";
97 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
98 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-',
100 die "gpg failed" if $? != 0;
104 open my $fh, '<', "$store/.gpg-id"
105 or die "can't open recipient file";
118 if (m,/.git$, || m,/.got$,) {
119 $File::Find::prune = 1;
122 return unless -f && m,.gpg$,;
127 return if defined($pattern) && ! m/$pattern/ix;
133 my @sorted_entries = sort(@entries);
134 return @sorted_entries;
139 open my $fh, '-|', ('got', @_);
147 open (my $null, '>', '/dev/null') or die "can't open /dev/null: $!";
148 open (my $stderr, ">&", STDERR) or die "can't save stderr: $!";
149 open (STDERR, ">&", $null) or die "can't redirect stderr: $!";
154 open (STDERR, ">&", $stderr) or die "can't restore stderr: $!";
156 return $found ? 1 : (got 'add', '-I', $file);
160 got 'remove', '-f', shift
166 die "failed to fork: $!" unless defined $pid;
170 die "failed to commit changes" if $?;
174 open (STDOUT, ">&", \*STDERR)
175 or die "can't redirect stdout to stderr";
176 exec ('got', 'commit', '-m', shift)
177 or die "failed to exec got: $!";
184 GetOptions('h|?' => \&usage) or usage;
188 my $file = name2file(shift @ARGV);
189 system ($gpg, @gpg_flags, '-d', $file);
190 die "failed to exec $gpg: $!" if $? == -1;
195 GetOptions('h|?' => \&usage) or usage;
198 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
200 my $entry = shift @ARGV;
201 my $epath = name2file $entry;
203 my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
204 eval { edit $editor, $fh, $tempfile, $epath };
209 got_ci "update $entry";
213 GetOptions('h|?' => \&usage) or usage;
216 say $_ foreach passfind(shift @ARGV);
219 # TODO: handle moving directories?
221 GetOptions('h|?' => \&usage) or usage;
227 my $pa = name2file $a;
228 my $pb = name2file $b;
230 die "source password doesn't exist" unless -f $pa;
231 die "target password exists" if -f $pb;
233 make_path(dirname $pb);
234 rename $pa, $pb or die "can't rename $a to $b: $!";
237 got_add $pb or die "can't add $pb\n";
242 GetOptions('h|?' => \&usage) or usage;
246 my $name = shift @ARGV;
247 my $file = name2file $name;
262 my $name = shift @ARGV;
263 my $file = name2file $name;
265 my $msg = -f $file ? "update $name" : "+$name";
266 make_path(dirname $file);
268 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
269 '--batch', '--yes', '-o', $file);
270 open my $fh, '|-', @args;
272 binmode(STDIN) or die "cannot binmode STDIN";
273 binmode(STDOUT) or die "cannot binmode STDOUT";
274 binmode($fh) or die "cannot binmode pipe";