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)";
24 use Encode qw(decode);
26 use Getopt::Long qw(:config bundling require_order);
29 use File::Path qw(make_path);
30 use File::Temp qw(tempfile);
32 my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
34 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
35 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
38 cat => [\&cmd_cat, "entries..."],
39 edit => [\&cmd_edit, "entry"],
40 find => [\&cmd_find, "[pattern]"],
41 mv => [\&cmd_mv, "from to"],
42 rm => [\&cmd_rm, "entries..."],
43 tee => [\&cmd_tee, "[-q] entry"],
46 my $usage = "[-h] command [argument ...]";
49 my $prog = basename $0;
50 if (defined($cmd) and defined($subcmd{$cmd})) {
51 say STDERR "Usage: $prog $cmd $usage";
53 say STDERR "Usage: $prog $usage";
54 say STDERR "unknown command $cmd" if defined($cmd);
55 say STDERR "commands: ", join(' ', sort(keys %subcmd));
60 GetOptions("h|?" => \&usage) or usage();
62 $cmd = shift // 'find';
63 usage() unless defined $subcmd{$cmd};
65 ($fn, $usage) = @{$subcmd{$cmd}};
75 $f .= ".gpg" unless $f =~ m,\.gpg$,;
80 my ($editor, $fh, $tempfile, $epath) = @_;
82 open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
83 open (STDOUT, ">&", $fh) or die "can't redirect stdout to $tempfile";
84 system ($gpg, @gpg_flags, '-d', $epath);
85 die "$gpg exited with $?\n" if $? != 0;
86 open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
88 my $oldtime = (stat($fh))[9];
91 system ($editor, $tempfile);
92 die "editor $editor failed\n" if $? != 0;
94 my $newtime = (stat($tempfile))[9];
95 if ($oldtime == $newtime) {
96 say STDERR "no changes made.";
100 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', $epath,
101 '--batch', '--yes', $tempfile);
103 die "gpg failed" if $? != 0;
107 open my $fh, '<', "$store/.gpg-id"
108 or die "can't open recipient file";
119 $pattern = decode(locale => $pattern) if defined $pattern;
124 $_ = decode(locale => $_);
125 if (m,/.git$, || m,/.got$,) {
126 $File::Find::prune = 1;
129 return unless -f $raw && m,.gpg$,;
134 return if defined($pattern) && ! m/$pattern/ix;
140 my @sorted_entries = sort(@entries);
141 return @sorted_entries;
146 die "failed to fork: $!" unless defined $pid;
153 open (STDOUT, '>', '/dev/null')
154 or die "can't redirect to /dev/null";
164 got 'remove', '-f', shift
170 die "failed to fork: $!" unless defined $pid;
174 die "failed to commit changes" if $?;
178 open (STDOUT, ">&", \*STDERR)
179 or die "can't redirect stdout to stderr";
180 exec ('got', 'commit', '-m', shift)
181 or die "failed to exec got: $!";
188 GetOptions('h|?' => \&usage) or usage;
192 my $file = name2file(shift @ARGV);
193 system ($gpg, @gpg_flags, '-d', $file);
194 die "failed to exec $gpg: $!" if $? == -1;
199 GetOptions('h|?' => \&usage) or usage;
202 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
204 my $entry = shift @ARGV;
205 my $epath = name2file $entry;
207 my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
208 eval { edit $editor, $fh, $tempfile, $epath };
213 got_ci "update $entry";
217 GetOptions('h|?' => \&usage) or usage;
220 say $_ foreach passfind(shift @ARGV);
223 # TODO: handle moving directories?
225 GetOptions('h|?' => \&usage) or usage;
231 my $pa = name2file $a;
232 my $pb = name2file $b;
234 die "source password doesn't exist" unless -f $pa;
235 die "target password exists" if -f $pb;
237 make_path(dirname $pb);
238 rename $pa, $pb or die "can't rename $a to $b: $!";
241 got_add $pb or die "can't add $pb\n";
246 GetOptions('h|?' => \&usage) or usage;
250 my $name = shift @ARGV;
251 my $file = name2file $name;
266 my $name = shift @ARGV;
267 my $file = name2file $name;
269 my $msg = -f $file ? "update $name" : "+$name";
270 make_path(dirname $file);
272 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
273 '--batch', '--yes', '-o', $file);
274 open my $fh, '|-', @args;
276 binmode(STDIN) or die "cannot binmode STDIN";
277 binmode(STDOUT) or die "cannot binmode STDOUT";
278 binmode($fh) or die "cannot binmode pipe";