1 5cdf5adc 2022-05-08 op #!/usr/bin/env perl
3 71c704e3 2023-01-16 op # Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
5 5cdf5adc 2022-05-08 op # Permission to use, copy, modify, and distribute this software for any
6 5cdf5adc 2022-05-08 op # purpose with or without fee is hereby granted, provided that the above
7 5cdf5adc 2022-05-08 op # copyright notice and this permission notice appear in all copies.
9 5cdf5adc 2022-05-08 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5cdf5adc 2022-05-08 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5cdf5adc 2022-05-08 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5cdf5adc 2022-05-08 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5cdf5adc 2022-05-08 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5cdf5adc 2022-05-08 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5cdf5adc 2022-05-08 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 5cdf5adc 2022-05-08 op use open ":std", ":encoding(UTF-8)";
23 42215f91 2023-10-17 op use Encode::Locale;
24 cb8467de 2023-10-17 op use Encode qw(decode);
26 5cdf5adc 2022-05-08 op use Getopt::Long qw(:config bundling require_order);
27 5cdf5adc 2022-05-08 op use File::Basename;
28 5cdf5adc 2022-05-08 op use File::Find;
29 6fd928d3 2023-06-17 op use File::Path qw(make_path);
30 8ce01796 2023-01-11 op use File::Temp qw(tempfile);
32 50c751f3 2022-06-29 op my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
34 d16aedaf 2022-09-09 op my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
35 12ad62fd 2022-06-29 op my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
37 8dc394df 2022-06-29 op my %subcmd = (
38 8dc394df 2022-06-29 op cat => [\&cmd_cat, "entries..."],
39 8ce01796 2023-01-11 op edit => [\&cmd_edit, "entry"],
40 8dc394df 2022-06-29 op find => [\&cmd_find, "[pattern]"],
41 8dc394df 2022-06-29 op mv => [\&cmd_mv, "from to"],
42 8dc394df 2022-06-29 op rm => [\&cmd_rm, "entries..."],
43 8dc394df 2022-06-29 op tee => [\&cmd_tee, "[-q] entry"],
46 f19c01bc 2022-09-09 op my $usage = "[-h] command [argument ...]";
49 5f8e7670 2022-06-29 op my $prog = basename $0;
50 d1bd3477 2022-06-29 op if (defined($cmd) and defined($subcmd{$cmd})) {
51 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $cmd $usage";
53 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $usage";
54 d1bd3477 2022-06-29 op say STDERR "unknown command $cmd" if defined($cmd);
55 8dc394df 2022-06-29 op say STDERR "commands: ", join(' ', sort(keys %subcmd));
60 5f8e7670 2022-06-29 op GetOptions("h|?" => \&usage) or usage();
62 5f8e7670 2022-06-29 op $cmd = shift // 'find';
63 5f8e7670 2022-06-29 op usage() unless defined $subcmd{$cmd};
65 5f8e7670 2022-06-29 op ($fn, $usage) = @{$subcmd{$cmd}};
73 5cdf5adc 2022-05-08 op sub name2file {
74 5cdf5adc 2022-05-08 op my $f = shift;
75 5cdf5adc 2022-05-08 op $f .= ".gpg" unless $f =~ m,\.gpg$,;
80 8c4930cf 2023-01-21 op my ($editor, $fh, $tempfile, $epath) = @_;
82 8c4930cf 2023-01-21 op open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
83 8c4930cf 2023-01-21 op open (STDOUT, ">&", $fh) or die "can't redirect stdout to $tempfile";
84 8c4930cf 2023-01-21 op system ($gpg, @gpg_flags, '-d', $epath);
85 8c4930cf 2023-01-21 op die "$gpg exited with $?\n" if $? != 0;
86 8c4930cf 2023-01-21 op open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
88 8c4930cf 2023-01-21 op my $oldtime = (stat($fh))[9];
91 8c4930cf 2023-01-21 op system ($editor, $tempfile);
92 8c4930cf 2023-01-21 op die "editor $editor failed\n" if $? != 0;
94 8c4930cf 2023-01-21 op my $newtime = (stat($tempfile))[9];
95 8c4930cf 2023-01-21 op if ($oldtime == $newtime) {
96 8c4930cf 2023-01-21 op say STDERR "no changes made.";
100 fdb1653a 2024-02-16 op system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', $epath,
101 fdb1653a 2024-02-16 op '--batch', '--yes', $tempfile);
103 8c4930cf 2023-01-21 op die "gpg failed" if $? != 0;
106 5cdf5adc 2022-05-08 op sub recipient {
107 5cdf5adc 2022-05-08 op open my $fh, '<', "$store/.gpg-id"
108 5cdf5adc 2022-05-08 op or die "can't open recipient file";
109 5cdf5adc 2022-05-08 op my $r = <$fh>;
115 5cdf5adc 2022-05-08 op sub passfind {
116 5cdf5adc 2022-05-08 op my $pattern = shift;
119 42215f91 2023-10-17 op $pattern = decode(locale => $pattern) if defined $pattern;
122 5cdf5adc 2022-05-08 op wanted => sub {
123 cb8467de 2023-10-17 op my $raw = $_;
124 cb8467de 2023-10-17 op $_ = decode(locale => $_);
125 5cdf5adc 2022-05-08 op if (m,/.git$, || m,/.got$,) {
126 5cdf5adc 2022-05-08 op $File::Find::prune = 1;
129 cb8467de 2023-10-17 op return unless -f $raw && m,.gpg$,;
131 5cdf5adc 2022-05-08 op s,^$store/*,,;
134 84eda8d6 2023-06-17 op return if defined($pattern) && ! m/$pattern/ix;
135 5cdf5adc 2022-05-08 op push @entries, $_;
137 5cdf5adc 2022-05-08 op no_chdir => 1,
138 5cdf5adc 2022-05-08 op follow_fast => 1,
139 5cdf5adc 2022-05-08 op }, ($store));
140 a0e32af2 2023-06-17 op my @sorted_entries = sort(@entries);
141 a0e32af2 2023-06-17 op return @sorted_entries;
145 2d265c92 2024-02-16 op my $pid = fork;
146 2d265c92 2024-02-16 op die "failed to fork: $!" unless defined $pid;
148 2d265c92 2024-02-16 op if ($pid != 0) {
153 2d265c92 2024-02-16 op open (STDOUT, '>', '/dev/null')
154 2d265c92 2024-02-16 op or die "can't redirect to /dev/null";
155 2d265c92 2024-02-16 op exec ('got', @_);
158 5cdf5adc 2022-05-08 op sub got_add {
159 2d265c92 2024-02-16 op got 'add', shift
164 f96edc25 2022-06-29 op got 'remove', '-f', shift
169 afdbc7b0 2022-06-29 op my $pid = fork;
170 afdbc7b0 2022-06-29 op die "failed to fork: $!" unless defined $pid;
172 f9b1eaa1 2022-06-29 op if ($pid != 0) {
174 afdbc7b0 2022-06-29 op die "failed to commit changes" if $?;
178 afdbc7b0 2022-06-29 op open (STDOUT, ">&", \*STDERR)
179 afdbc7b0 2022-06-29 op or die "can't redirect stdout to stderr";
180 77ea6ba1 2023-01-21 op exec ('got', 'commit', '-m', shift)
181 77ea6ba1 2023-01-21 op or die "failed to exec got: $!";
187 5cdf5adc 2022-05-08 op sub cmd_cat {
188 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
189 5cdf5adc 2022-05-08 op usage unless @ARGV;
191 5cdf5adc 2022-05-08 op while (@ARGV) {
192 5cdf5adc 2022-05-08 op my $file = name2file(shift @ARGV);
193 5cdf5adc 2022-05-08 op system ($gpg, @gpg_flags, '-d', $file);
194 5cdf5adc 2022-05-08 op die "failed to exec $gpg: $!" if $? == -1;
198 8ce01796 2023-01-11 op sub cmd_edit {
199 8ce01796 2023-01-11 op GetOptions('h|?' => \&usage) or usage;
200 8ce01796 2023-01-11 op usage if @ARGV != 1;
202 8ce01796 2023-01-11 op my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
204 8ce01796 2023-01-11 op my $entry = shift @ARGV;
205 8ce01796 2023-01-11 op my $epath = name2file $entry;
207 8c4930cf 2023-01-21 op my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
208 8c4930cf 2023-01-21 op eval { edit $editor, $fh, $tempfile, $epath };
209 8c4930cf 2023-01-21 op unlink $tempfile;
210 8c4930cf 2023-01-21 op die "$@\n" if $@;
212 8ce01796 2023-01-11 op got_add $epath;
213 8ce01796 2023-01-11 op got_ci "update $entry";
216 5cdf5adc 2022-05-08 op sub cmd_find {
217 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
218 f9b1eaa1 2022-06-29 op usage if @ARGV > 1;
220 b458d1aa 2023-06-17 op say $_ foreach passfind(shift @ARGV);
223 5cdf5adc 2022-05-08 op # TODO: handle moving directories?
225 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
226 f9b1eaa1 2022-06-29 op usage if @ARGV != 2;
228 5cdf5adc 2022-05-08 op my $a = shift @ARGV;
229 5cdf5adc 2022-05-08 op my $b = shift @ARGV;
231 5cdf5adc 2022-05-08 op my $pa = name2file $a;
232 5cdf5adc 2022-05-08 op my $pb = name2file $b;
234 5cdf5adc 2022-05-08 op die "source password doesn't exist" unless -f $pa;
235 5cdf5adc 2022-05-08 op die "target password exists" if -f $pb;
237 6fd928d3 2023-06-17 op make_path(dirname $pb);
238 5cdf5adc 2022-05-08 op rename $pa, $pb or die "can't rename $a to $b: $!";
241 bd71caf6 2022-06-29 op got_add $pb or die "can't add $pb\n";
242 5cdf5adc 2022-05-08 op got_ci "mv $a $b";
246 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
247 6a455fdf 2022-06-29 op usage unless @ARGV;
249 6a455fdf 2022-06-29 op while (@ARGV) {
250 6a455fdf 2022-06-29 op my $name = shift @ARGV;
251 6a455fdf 2022-06-29 op my $file = name2file $name;
253 6a455fdf 2022-06-29 op got_rm $file;
254 6a455fdf 2022-06-29 op got_ci "-$name";
258 afdbc7b0 2022-06-29 op sub cmd_tee {
261 afdbc7b0 2022-06-29 op 'h|?' => \&usage,
264 f9b1eaa1 2022-06-29 op usage if @ARGV != 1;
266 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
267 5cdf5adc 2022-05-08 op my $file = name2file $name;
269 5eba1883 2022-10-20 op my $msg = -f $file ? "update $name" : "+$name";
270 6fd928d3 2023-06-17 op make_path(dirname $file);
272 1169deba 2023-01-21 op my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
273 1169deba 2023-01-21 op '--batch', '--yes', '-o', $file);
274 1169deba 2023-01-21 op open my $fh, '|-', @args;
276 fc9fec05 2023-01-21 op binmode(STDIN) or die "cannot binmode STDIN";
277 fc9fec05 2023-01-21 op binmode(STDOUT) or die "cannot binmode STDOUT";
278 fc9fec05 2023-01-21 op binmode($fh) or die "cannot binmode pipe";
280 1169deba 2023-01-21 op local $/ = \1024;
281 1169deba 2023-01-21 op while (<STDIN>) {
282 1169deba 2023-01-21 op print $fh $_;
283 1169deba 2023-01-21 op print $_ unless $q;
286 1169deba 2023-01-21 op exit $? if $?;
288 5cdf5adc 2022-05-08 op got_add $file;