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 5cdf5adc 2022-05-08 op use Getopt::Long qw(:config bundling require_order);
24 5cdf5adc 2022-05-08 op use File::Basename;
25 5cdf5adc 2022-05-08 op use File::Find;
26 8ce01796 2023-01-11 op use File::Temp qw(tempfile);
28 50c751f3 2022-06-29 op my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
30 d16aedaf 2022-09-09 op my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
31 12ad62fd 2022-06-29 op my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
33 8dc394df 2022-06-29 op my %subcmd = (
34 8dc394df 2022-06-29 op cat => [\&cmd_cat, "entries..."],
35 8ce01796 2023-01-11 op edit => [\&cmd_edit, "entry"],
36 8dc394df 2022-06-29 op find => [\&cmd_find, "[pattern]"],
37 8dc394df 2022-06-29 op mv => [\&cmd_mv, "from to"],
38 8dc394df 2022-06-29 op rm => [\&cmd_rm, "entries..."],
39 8dc394df 2022-06-29 op tee => [\&cmd_tee, "[-q] entry"],
42 f19c01bc 2022-09-09 op my $usage = "[-h] command [argument ...]";
45 5f8e7670 2022-06-29 op my $prog = basename $0;
46 d1bd3477 2022-06-29 op if (defined($cmd) and defined($subcmd{$cmd})) {
47 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $cmd $usage";
49 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $usage";
50 d1bd3477 2022-06-29 op say STDERR "unknown command $cmd" if defined($cmd);
51 8dc394df 2022-06-29 op say STDERR "commands: ", join(' ', sort(keys %subcmd));
56 5f8e7670 2022-06-29 op GetOptions("h|?" => \&usage) or usage();
58 5f8e7670 2022-06-29 op $cmd = shift // 'find';
59 5f8e7670 2022-06-29 op usage() unless defined $subcmd{$cmd};
61 5f8e7670 2022-06-29 op ($fn, $usage) = @{$subcmd{$cmd}};
69 5cdf5adc 2022-05-08 op sub name2file {
70 5cdf5adc 2022-05-08 op my $f = shift;
71 5cdf5adc 2022-05-08 op $f .= ".gpg" unless $f =~ m,\.gpg$,;
76 1f89d66a 2022-06-29 op my $dir = shift;
77 1f89d66a 2022-06-29 op my $parent = dirname $dir;
78 1f89d66a 2022-06-29 op mkdirs($parent) unless -d $parent || $parent eq '/';
79 1f89d66a 2022-06-29 op mkdir $dir or die "mkdir $dir: $!"
80 1f89d66a 2022-06-29 op unless -d $dir;
83 5cdf5adc 2022-05-08 op sub recipient {
84 5cdf5adc 2022-05-08 op open my $fh, '<', "$store/.gpg-id"
85 5cdf5adc 2022-05-08 op or die "can't open recipient file";
86 5cdf5adc 2022-05-08 op my $r = <$fh>;
92 5cdf5adc 2022-05-08 op sub passfind {
93 5cdf5adc 2022-05-08 op my $pattern = shift;
97 5cdf5adc 2022-05-08 op wanted => sub {
98 5cdf5adc 2022-05-08 op if (m,/.git$, || m,/.got$,) {
99 5cdf5adc 2022-05-08 op $File::Find::prune = 1;
102 5cdf5adc 2022-05-08 op return unless -f && m,.gpg$,;
104 5cdf5adc 2022-05-08 op s,^$store/*,,;
107 91fcc6e3 2023-01-11 op return if defined($pattern) && ! m/$pattern/i;
108 5cdf5adc 2022-05-08 op push @entries, $_;
110 5cdf5adc 2022-05-08 op no_chdir => 1,
111 5cdf5adc 2022-05-08 op follow_fast => 1,
112 5cdf5adc 2022-05-08 op }, ($store));
113 5cdf5adc 2022-05-08 op return sort(@entries);
117 87197963 2022-06-29 op # discard stdout
118 77ea6ba1 2023-01-21 op open my $fh, '-|', ('got', @_);
123 5cdf5adc 2022-05-08 op sub got_add {
124 f96edc25 2022-06-29 op return got 'add', '-I', shift;
128 f96edc25 2022-06-29 op got 'remove', '-f', shift
133 afdbc7b0 2022-06-29 op my $pid = fork;
134 afdbc7b0 2022-06-29 op die "failed to fork: $!" unless defined $pid;
136 f9b1eaa1 2022-06-29 op if ($pid != 0) {
138 afdbc7b0 2022-06-29 op die "failed to commit changes" if $?;
142 afdbc7b0 2022-06-29 op open (STDOUT, ">&", \*STDERR)
143 afdbc7b0 2022-06-29 op or die "can't redirect stdout to stderr";
144 77ea6ba1 2023-01-21 op exec ('got', 'commit', '-m', shift)
145 77ea6ba1 2023-01-21 op or die "failed to exec got: $!";
151 5cdf5adc 2022-05-08 op sub cmd_cat {
152 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
153 5cdf5adc 2022-05-08 op usage unless @ARGV;
155 5cdf5adc 2022-05-08 op while (@ARGV) {
156 5cdf5adc 2022-05-08 op my $file = name2file(shift @ARGV);
157 5cdf5adc 2022-05-08 op system ($gpg, @gpg_flags, '-d', $file);
158 5cdf5adc 2022-05-08 op die "failed to exec $gpg: $!" if $? == -1;
162 8ce01796 2023-01-11 op sub cmd_edit {
163 8ce01796 2023-01-11 op GetOptions('h|?' => \&usage) or usage;
164 8ce01796 2023-01-11 op usage if @ARGV != 1;
166 8ce01796 2023-01-11 op my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
168 8ce01796 2023-01-11 op my $entry = shift @ARGV;
169 8ce01796 2023-01-11 op my $epath = name2file $entry;
171 8ce01796 2023-01-11 op my ($fh, $filename) = tempfile "/tmp/plass-XXXXXXXXXX";
173 8ce01796 2023-01-11 op open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
175 8ce01796 2023-01-11 op open (STDOUT, ">&", $fh) or die "can't redirect stdout to $filename";
176 8ce01796 2023-01-11 op system ($gpg, @gpg_flags, '-d', $epath);
177 8ce01796 2023-01-11 op die "$gpg exited with $?\n" if $? != 0;
179 8ce01796 2023-01-11 op # restore stdout so the editor can access the TTY if needed.
180 8ce01796 2023-01-11 op open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
182 8ce01796 2023-01-11 op my $oldtime = (stat($fh))[9];
184 8ce01796 2023-01-11 op system ($editor, $filename);
185 8ce01796 2023-01-11 op die "editor $editor failed\n" if $? != 0;
187 8ce01796 2023-01-11 op my $newtime = (stat($filename))[9];
189 8ce01796 2023-01-11 op if ($oldtime == $newtime) {
190 8ce01796 2023-01-11 op say STDERR "no changes made.";
191 87986040 2023-01-11 op unlink $filename or die "can't delete $filename: $!\n";
195 8ce01796 2023-01-11 op open(STDIN, '<', $filename) or die "can't redirect stdin: $!";
196 8ce01796 2023-01-11 op open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
197 8ce01796 2023-01-11 op system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-');
198 87986040 2023-01-11 op unlink $filename or die "can't delete $filename: $!\n";
199 8ce01796 2023-01-11 op die "gpg failed" if $? != 0;
201 8ce01796 2023-01-11 op got_add $epath;
202 8ce01796 2023-01-11 op got_ci "update $entry";
205 5cdf5adc 2022-05-08 op sub cmd_find {
206 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
207 f9b1eaa1 2022-06-29 op usage if @ARGV > 1;
209 5cdf5adc 2022-05-08 op map { say $_ } passfind(shift @ARGV);
212 5cdf5adc 2022-05-08 op # TODO: handle moving directories?
214 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
215 f9b1eaa1 2022-06-29 op usage if @ARGV != 2;
217 5cdf5adc 2022-05-08 op my $a = shift @ARGV;
218 5cdf5adc 2022-05-08 op my $b = shift @ARGV;
220 5cdf5adc 2022-05-08 op my $pa = name2file $a;
221 5cdf5adc 2022-05-08 op my $pb = name2file $b;
223 5cdf5adc 2022-05-08 op die "source password doesn't exist" unless -f $pa;
224 5cdf5adc 2022-05-08 op die "target password exists" if -f $pb;
226 4568ada0 2022-06-29 op mkdirs(dirname $pb);
227 5cdf5adc 2022-05-08 op rename $pa, $pb or die "can't rename $a to $b: $!";
230 bd71caf6 2022-06-29 op got_add $pb or die "can't add $pb\n";
231 5cdf5adc 2022-05-08 op got_ci "mv $a $b";
235 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
236 6a455fdf 2022-06-29 op usage unless @ARGV;
238 6a455fdf 2022-06-29 op while (@ARGV) {
239 6a455fdf 2022-06-29 op my $name = shift @ARGV;
240 6a455fdf 2022-06-29 op my $file = name2file $name;
242 6a455fdf 2022-06-29 op got_rm $file;
243 6a455fdf 2022-06-29 op got_ci "-$name";
247 afdbc7b0 2022-06-29 op sub cmd_tee {
250 afdbc7b0 2022-06-29 op 'h|?' => \&usage,
253 f9b1eaa1 2022-06-29 op usage if @ARGV != 1;
255 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
256 5cdf5adc 2022-05-08 op my $file = name2file $name;
258 5eba1883 2022-10-20 op my $msg = -f $file ? "update $name" : "+$name";
259 1169deba 2023-01-21 op mkdirs(dirname $file);
261 1169deba 2023-01-21 op my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
262 1169deba 2023-01-21 op '--batch', '--yes', '-o', $file);
263 1169deba 2023-01-21 op open my $fh, '|-', @args;
265 1169deba 2023-01-21 op local $/ = \1024;
266 1169deba 2023-01-21 op while (<STDIN>) {
267 1169deba 2023-01-21 op print $fh $_;
268 1169deba 2023-01-21 op print $_ unless $q;
271 1169deba 2023-01-21 op exit $? if $?;
273 5cdf5adc 2022-05-08 op got_add $file;