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