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 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
101 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-',
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 open my $fh, '-|', ('got', @_);
154 open (my $null, '>', '/dev/null') or die "can't open /dev/null: $!";
155 open (my $stderr, ">&", STDERR) or die "can't save stderr: $!";
156 open (STDERR, ">&", $null) or die "can't redirect stderr: $!";
161 open (STDERR, ">&", $stderr) or die "can't restore stderr: $!";
163 return $found ? 1 : (got 'add', '-I', $file);
167 got 'remove', '-f', shift
173 die "failed to fork: $!" unless defined $pid;
177 die "failed to commit changes" if $?;
181 open (STDOUT, ">&", \*STDERR)
182 or die "can't redirect stdout to stderr";
183 exec ('got', 'commit', '-m', shift)
184 or die "failed to exec got: $!";
191 GetOptions('h|?' => \&usage) or usage;
195 my $file = name2file(shift @ARGV);
196 system ($gpg, @gpg_flags, '-d', $file);
197 die "failed to exec $gpg: $!" if $? == -1;
202 GetOptions('h|?' => \&usage) or usage;
205 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
207 my $entry = shift @ARGV;
208 my $epath = name2file $entry;
210 my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
211 eval { edit $editor, $fh, $tempfile, $epath };
216 got_ci "update $entry";
220 GetOptions('h|?' => \&usage) or usage;
223 say $_ foreach passfind(shift @ARGV);
226 # TODO: handle moving directories?
228 GetOptions('h|?' => \&usage) or usage;
234 my $pa = name2file $a;
235 my $pb = name2file $b;
237 die "source password doesn't exist" unless -f $pa;
238 die "target password exists" if -f $pb;
240 make_path(dirname $pb);
241 rename $pa, $pb or die "can't rename $a to $b: $!";
244 got_add $pb or die "can't add $pb\n";
249 GetOptions('h|?' => \&usage) or usage;
253 my $name = shift @ARGV;
254 my $file = name2file $name;
269 my $name = shift @ARGV;
270 my $file = name2file $name;
272 my $msg = -f $file ? "update $name" : "+$name";
273 make_path(dirname $file);
275 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
276 '--batch', '--yes', '-o', $file);
277 open my $fh, '|-', @args;
279 binmode(STDIN) or die "cannot binmode STDIN";
280 binmode(STDOUT) or die "cannot binmode STDOUT";
281 binmode($fh) or die "cannot binmode pipe";