Blame


1 5cdf5adc 2022-05-08 op #!/usr/bin/env perl
2 5cdf5adc 2022-05-08 op #
3 71c704e3 2023-01-16 op # Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
4 5cdf5adc 2022-05-08 op #
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.
8 5cdf5adc 2022-05-08 op #
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.
16 5cdf5adc 2022-05-08 op
17 5cdf5adc 2022-05-08 op use strict;
18 5cdf5adc 2022-05-08 op use warnings;
19 5cdf5adc 2022-05-08 op use v5.32;
20 5cdf5adc 2022-05-08 op
21 5cdf5adc 2022-05-08 op use open ":std", ":encoding(UTF-8)";
22 5cdf5adc 2022-05-08 op
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 6fd928d3 2023-06-17 op use File::Path qw(make_path);
27 8ce01796 2023-01-11 op use File::Temp qw(tempfile);
28 5cdf5adc 2022-05-08 op
29 50c751f3 2022-06-29 op my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
30 5cdf5adc 2022-05-08 op
31 d16aedaf 2022-09-09 op my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
32 12ad62fd 2022-06-29 op my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
33 5cdf5adc 2022-05-08 op
34 8dc394df 2022-06-29 op my %subcmd = (
35 8dc394df 2022-06-29 op cat => [\&cmd_cat, "entries..."],
36 8ce01796 2023-01-11 op edit => [\&cmd_edit, "entry"],
37 8dc394df 2022-06-29 op find => [\&cmd_find, "[pattern]"],
38 8dc394df 2022-06-29 op mv => [\&cmd_mv, "from to"],
39 8dc394df 2022-06-29 op rm => [\&cmd_rm, "entries..."],
40 8dc394df 2022-06-29 op tee => [\&cmd_tee, "[-q] entry"],
41 8dc394df 2022-06-29 op );
42 8dc394df 2022-06-29 op
43 f19c01bc 2022-09-09 op my $usage = "[-h] command [argument ...]";
44 5f8e7670 2022-06-29 op my $cmd;
45 5f8e7670 2022-06-29 op sub usage {
46 5f8e7670 2022-06-29 op my $prog = basename $0;
47 d1bd3477 2022-06-29 op if (defined($cmd) and defined($subcmd{$cmd})) {
48 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $cmd $usage";
49 5f8e7670 2022-06-29 op } else {
50 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $usage";
51 d1bd3477 2022-06-29 op say STDERR "unknown command $cmd" if defined($cmd);
52 8dc394df 2022-06-29 op say STDERR "commands: ", join(' ', sort(keys %subcmd));
53 5f8e7670 2022-06-29 op }
54 5f8e7670 2022-06-29 op exit 1;
55 5f8e7670 2022-06-29 op }
56 5cdf5adc 2022-05-08 op
57 5f8e7670 2022-06-29 op GetOptions("h|?" => \&usage) or usage();
58 5cdf5adc 2022-05-08 op
59 5f8e7670 2022-06-29 op $cmd = shift // 'find';
60 5f8e7670 2022-06-29 op usage() unless defined $subcmd{$cmd};
61 5f8e7670 2022-06-29 op my $fn;
62 5f8e7670 2022-06-29 op ($fn, $usage) = @{$subcmd{$cmd}};
63 5cdf5adc 2022-05-08 op chdir $store;
64 5cdf5adc 2022-05-08 op $fn->();
65 5cdf5adc 2022-05-08 op exit 0;
66 5cdf5adc 2022-05-08 op
67 5cdf5adc 2022-05-08 op
68 5cdf5adc 2022-05-08 op # utils
69 5cdf5adc 2022-05-08 op
70 5cdf5adc 2022-05-08 op sub name2file {
71 5cdf5adc 2022-05-08 op my $f = shift;
72 5cdf5adc 2022-05-08 op $f .= ".gpg" unless $f =~ m,\.gpg$,;
73 5cdf5adc 2022-05-08 op return $f;
74 5cdf5adc 2022-05-08 op }
75 5cdf5adc 2022-05-08 op
76 8c4930cf 2023-01-21 op sub edit {
77 8c4930cf 2023-01-21 op my ($editor, $fh, $tempfile, $epath) = @_;
78 8c4930cf 2023-01-21 op
79 8c4930cf 2023-01-21 op open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
80 8c4930cf 2023-01-21 op open (STDOUT, ">&", $fh) or die "can't redirect stdout to $tempfile";
81 8c4930cf 2023-01-21 op system ($gpg, @gpg_flags, '-d', $epath);
82 8c4930cf 2023-01-21 op die "$gpg exited with $?\n" if $? != 0;
83 8c4930cf 2023-01-21 op open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
84 8c4930cf 2023-01-21 op
85 8c4930cf 2023-01-21 op my $oldtime = (stat($fh))[9];
86 8c4930cf 2023-01-21 op close($fh);
87 8c4930cf 2023-01-21 op
88 8c4930cf 2023-01-21 op system ($editor, $tempfile);
89 8c4930cf 2023-01-21 op die "editor $editor failed\n" if $? != 0;
90 8c4930cf 2023-01-21 op
91 8c4930cf 2023-01-21 op my $newtime = (stat($tempfile))[9];
92 8c4930cf 2023-01-21 op if ($oldtime == $newtime) {
93 8c4930cf 2023-01-21 op say STDERR "no changes made.";
94 6b4b0a36 2023-06-17 op return;
95 8c4930cf 2023-01-21 op }
96 8c4930cf 2023-01-21 op
97 8c4930cf 2023-01-21 op open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
98 8c4930cf 2023-01-21 op system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-',
99 8c4930cf 2023-01-21 op $tempfile);
100 8c4930cf 2023-01-21 op die "gpg failed" if $? != 0;
101 8c4930cf 2023-01-21 op }
102 8c4930cf 2023-01-21 op
103 5cdf5adc 2022-05-08 op sub recipient {
104 5cdf5adc 2022-05-08 op open my $fh, '<', "$store/.gpg-id"
105 5cdf5adc 2022-05-08 op or die "can't open recipient file";
106 5cdf5adc 2022-05-08 op my $r = <$fh>;
107 5cdf5adc 2022-05-08 op chomp $r;
108 5cdf5adc 2022-05-08 op close($fh);
109 5cdf5adc 2022-05-08 op return $r;
110 5cdf5adc 2022-05-08 op }
111 5cdf5adc 2022-05-08 op
112 5cdf5adc 2022-05-08 op sub passfind {
113 5cdf5adc 2022-05-08 op my $pattern = shift;
114 5cdf5adc 2022-05-08 op my @entries;
115 5cdf5adc 2022-05-08 op
116 5cdf5adc 2022-05-08 op find({
117 5cdf5adc 2022-05-08 op wanted => sub {
118 5cdf5adc 2022-05-08 op if (m,/.git$, || m,/.got$,) {
119 5cdf5adc 2022-05-08 op $File::Find::prune = 1;
120 5cdf5adc 2022-05-08 op return;
121 5cdf5adc 2022-05-08 op }
122 5cdf5adc 2022-05-08 op return unless -f && m,.gpg$,;
123 5cdf5adc 2022-05-08 op
124 5cdf5adc 2022-05-08 op s,^$store/*,,;
125 5cdf5adc 2022-05-08 op s,.gpg$,,;
126 fb70a412 2022-10-21 op
127 84eda8d6 2023-06-17 op return if defined($pattern) && ! m/$pattern/ix;
128 5cdf5adc 2022-05-08 op push @entries, $_;
129 5cdf5adc 2022-05-08 op },
130 5cdf5adc 2022-05-08 op no_chdir => 1,
131 5cdf5adc 2022-05-08 op follow_fast => 1,
132 5cdf5adc 2022-05-08 op }, ($store));
133 a0e32af2 2023-06-17 op my @sorted_entries = sort(@entries);
134 a0e32af2 2023-06-17 op return @sorted_entries;
135 5cdf5adc 2022-05-08 op }
136 5cdf5adc 2022-05-08 op
137 5cdf5adc 2022-05-08 op sub got {
138 87197963 2022-06-29 op # discard stdout
139 77ea6ba1 2023-01-21 op open my $fh, '-|', ('got', @_);
140 5cdf5adc 2022-05-08 op close($fh);
141 f96edc25 2022-06-29 op return !$?;
142 5cdf5adc 2022-05-08 op }
143 5cdf5adc 2022-05-08 op
144 5cdf5adc 2022-05-08 op sub got_add {
145 3cf28f46 2023-01-21 op my $file = shift;
146 3cf28f46 2023-01-21 op
147 3cf28f46 2023-01-21 op open (my $null, '>', '/dev/null') or die "can't open /dev/null: $!";
148 3cf28f46 2023-01-21 op open (my $stderr, ">&", STDERR) or die "can't save stderr: $!";
149 3cf28f46 2023-01-21 op open (STDERR, ">&", $null) or die "can't redirect stderr: $!";
150 3cf28f46 2023-01-21 op
151 3cf28f46 2023-01-21 op got 'info', $file;
152 3cf28f46 2023-01-21 op my $found = !$?;
153 3cf28f46 2023-01-21 op
154 3cf28f46 2023-01-21 op open (STDERR, ">&", $stderr) or die "can't restore stderr: $!";
155 3cf28f46 2023-01-21 op
156 3cf28f46 2023-01-21 op return $found ? 1 : (got 'add', '-I', $file);
157 5cdf5adc 2022-05-08 op }
158 5cdf5adc 2022-05-08 op
159 5cdf5adc 2022-05-08 op sub got_rm {
160 f96edc25 2022-06-29 op got 'remove', '-f', shift
161 f96edc25 2022-06-29 op or exit(1);
162 5cdf5adc 2022-05-08 op }
163 5cdf5adc 2022-05-08 op
164 5cdf5adc 2022-05-08 op sub got_ci {
165 afdbc7b0 2022-06-29 op my $pid = fork;
166 afdbc7b0 2022-06-29 op die "failed to fork: $!" unless defined $pid;
167 afdbc7b0 2022-06-29 op
168 f9b1eaa1 2022-06-29 op if ($pid != 0) {
169 afdbc7b0 2022-06-29 op wait;
170 afdbc7b0 2022-06-29 op die "failed to commit changes" if $?;
171 afdbc7b0 2022-06-29 op return;
172 afdbc7b0 2022-06-29 op }
173 afdbc7b0 2022-06-29 op
174 afdbc7b0 2022-06-29 op open (STDOUT, ">&", \*STDERR)
175 afdbc7b0 2022-06-29 op or die "can't redirect stdout to stderr";
176 77ea6ba1 2023-01-21 op exec ('got', 'commit', '-m', shift)
177 77ea6ba1 2023-01-21 op or die "failed to exec got: $!";
178 5cdf5adc 2022-05-08 op }
179 5cdf5adc 2022-05-08 op
180 5cdf5adc 2022-05-08 op
181 5cdf5adc 2022-05-08 op # cmds
182 5cdf5adc 2022-05-08 op
183 5cdf5adc 2022-05-08 op sub cmd_cat {
184 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
185 5cdf5adc 2022-05-08 op usage unless @ARGV;
186 5cdf5adc 2022-05-08 op
187 5cdf5adc 2022-05-08 op while (@ARGV) {
188 5cdf5adc 2022-05-08 op my $file = name2file(shift @ARGV);
189 5cdf5adc 2022-05-08 op system ($gpg, @gpg_flags, '-d', $file);
190 5cdf5adc 2022-05-08 op die "failed to exec $gpg: $!" if $? == -1;
191 5cdf5adc 2022-05-08 op }
192 5cdf5adc 2022-05-08 op }
193 5cdf5adc 2022-05-08 op
194 8ce01796 2023-01-11 op sub cmd_edit {
195 8ce01796 2023-01-11 op GetOptions('h|?' => \&usage) or usage;
196 8ce01796 2023-01-11 op usage if @ARGV != 1;
197 8ce01796 2023-01-11 op
198 8ce01796 2023-01-11 op my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
199 8ce01796 2023-01-11 op
200 8ce01796 2023-01-11 op my $entry = shift @ARGV;
201 8ce01796 2023-01-11 op my $epath = name2file $entry;
202 8ce01796 2023-01-11 op
203 8c4930cf 2023-01-21 op my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
204 8c4930cf 2023-01-21 op eval { edit $editor, $fh, $tempfile, $epath };
205 8c4930cf 2023-01-21 op unlink $tempfile;
206 8c4930cf 2023-01-21 op die "$@\n" if $@;
207 8ce01796 2023-01-11 op
208 8ce01796 2023-01-11 op got_add $epath;
209 8ce01796 2023-01-11 op got_ci "update $entry";
210 8ce01796 2023-01-11 op }
211 8ce01796 2023-01-11 op
212 5cdf5adc 2022-05-08 op sub cmd_find {
213 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
214 f9b1eaa1 2022-06-29 op usage if @ARGV > 1;
215 5cdf5adc 2022-05-08 op
216 b458d1aa 2023-06-17 op say $_ foreach passfind(shift @ARGV);
217 5cdf5adc 2022-05-08 op }
218 5cdf5adc 2022-05-08 op
219 5cdf5adc 2022-05-08 op # TODO: handle moving directories?
220 5cdf5adc 2022-05-08 op sub cmd_mv {
221 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
222 f9b1eaa1 2022-06-29 op usage if @ARGV != 2;
223 5cdf5adc 2022-05-08 op
224 5cdf5adc 2022-05-08 op my $a = shift @ARGV;
225 5cdf5adc 2022-05-08 op my $b = shift @ARGV;
226 5cdf5adc 2022-05-08 op
227 5cdf5adc 2022-05-08 op my $pa = name2file $a;
228 5cdf5adc 2022-05-08 op my $pb = name2file $b;
229 5cdf5adc 2022-05-08 op
230 5cdf5adc 2022-05-08 op die "source password doesn't exist" unless -f $pa;
231 5cdf5adc 2022-05-08 op die "target password exists" if -f $pb;
232 5cdf5adc 2022-05-08 op
233 6fd928d3 2023-06-17 op make_path(dirname $pb);
234 5cdf5adc 2022-05-08 op rename $pa, $pb or die "can't rename $a to $b: $!";
235 5cdf5adc 2022-05-08 op
236 5cdf5adc 2022-05-08 op got_rm $pa;
237 bd71caf6 2022-06-29 op got_add $pb or die "can't add $pb\n";
238 5cdf5adc 2022-05-08 op got_ci "mv $a $b";
239 5cdf5adc 2022-05-08 op }
240 5cdf5adc 2022-05-08 op
241 5cdf5adc 2022-05-08 op sub cmd_rm {
242 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
243 6a455fdf 2022-06-29 op usage unless @ARGV;
244 5cdf5adc 2022-05-08 op
245 6a455fdf 2022-06-29 op while (@ARGV) {
246 6a455fdf 2022-06-29 op my $name = shift @ARGV;
247 6a455fdf 2022-06-29 op my $file = name2file $name;
248 5cdf5adc 2022-05-08 op
249 6a455fdf 2022-06-29 op got_rm $file;
250 6a455fdf 2022-06-29 op got_ci "-$name";
251 6a455fdf 2022-06-29 op }
252 5cdf5adc 2022-05-08 op }
253 5cdf5adc 2022-05-08 op
254 afdbc7b0 2022-06-29 op sub cmd_tee {
255 afdbc7b0 2022-06-29 op my $q;
256 afdbc7b0 2022-06-29 op GetOptions(
257 afdbc7b0 2022-06-29 op 'h|?' => \&usage,
258 afdbc7b0 2022-06-29 op 'q' => \$q,
259 afdbc7b0 2022-06-29 op ) or usage;
260 f9b1eaa1 2022-06-29 op usage if @ARGV != 1;
261 5cdf5adc 2022-05-08 op
262 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
263 5cdf5adc 2022-05-08 op my $file = name2file $name;
264 5cdf5adc 2022-05-08 op
265 5eba1883 2022-10-20 op my $msg = -f $file ? "update $name" : "+$name";
266 6fd928d3 2023-06-17 op make_path(dirname $file);
267 5eba1883 2022-10-20 op
268 1169deba 2023-01-21 op my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
269 1169deba 2023-01-21 op '--batch', '--yes', '-o', $file);
270 1169deba 2023-01-21 op open my $fh, '|-', @args;
271 fc9fec05 2023-01-21 op
272 fc9fec05 2023-01-21 op binmode(STDIN) or die "cannot binmode STDIN";
273 fc9fec05 2023-01-21 op binmode(STDOUT) or die "cannot binmode STDOUT";
274 fc9fec05 2023-01-21 op binmode($fh) or die "cannot binmode pipe";
275 5eba1883 2022-10-20 op
276 1169deba 2023-01-21 op local $/ = \1024;
277 1169deba 2023-01-21 op while (<STDIN>) {
278 1169deba 2023-01-21 op print $fh $_;
279 1169deba 2023-01-21 op print $_ unless $q;
280 1169deba 2023-01-21 op }
281 1169deba 2023-01-21 op close($fh);
282 1169deba 2023-01-21 op exit $? if $?;
283 5cdf5adc 2022-05-08 op
284 5cdf5adc 2022-05-08 op got_add $file;
285 5eba1883 2022-10-20 op got_ci $msg;
286 5cdf5adc 2022-05-08 op }