Blob


1 #!/usr/bin/env perl
2 #
3 # Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
4 #
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.
8 #
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.
17 use strict;
18 use warnings;
19 use v5.32;
21 use open ":std", ":encoding(UTF-8)";
23 use Encode::Locale;
24 use Encode qw(decode);
26 use Getopt::Long qw(:config bundling require_order);
27 use File::Basename;
28 use File::Find;
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);
37 my %subcmd = (
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"],
44 );
46 my $usage = "[-h] command [argument ...]";
47 my $cmd;
48 sub usage {
49 my $prog = basename $0;
50 if (defined($cmd) and defined($subcmd{$cmd})) {
51 say STDERR "Usage: $prog $cmd $usage";
52 } else {
53 say STDERR "Usage: $prog $usage";
54 say STDERR "unknown command $cmd" if defined($cmd);
55 say STDERR "commands: ", join(' ', sort(keys %subcmd));
56 }
57 exit 1;
58 }
60 GetOptions("h|?" => \&usage) or usage();
62 $cmd = shift // 'find';
63 usage() unless defined $subcmd{$cmd};
64 my $fn;
65 ($fn, $usage) = @{$subcmd{$cmd}};
66 chdir $store;
67 $fn->();
68 exit 0;
71 # utils
73 sub name2file {
74 my $f = shift;
75 $f .= ".gpg" unless $f =~ m,\.gpg$,;
76 return $f;
77 }
79 sub edit {
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];
89 close($fh);
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.";
97 return;
98 }
100 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
101 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-',
102 $tempfile);
103 die "gpg failed" if $? != 0;
106 sub recipient {
107 open my $fh, '<', "$store/.gpg-id"
108 or die "can't open recipient file";
109 my $r = <$fh>;
110 chomp $r;
111 close($fh);
112 return $r;
115 sub passfind {
116 my $pattern = shift;
117 my @entries;
119 $pattern = decode(locale => $pattern) if defined $pattern;
121 find({
122 wanted => sub {
123 my $raw = $_;
124 $_ = decode(locale => $_);
125 if (m,/.git$, || m,/.got$,) {
126 $File::Find::prune = 1;
127 return;
129 return unless -f $raw && m,.gpg$,;
131 s,^$store/*,,;
132 s,.gpg$,,;
134 return if defined($pattern) && ! m/$pattern/ix;
135 push @entries, $_;
136 },
137 no_chdir => 1,
138 follow_fast => 1,
139 }, ($store));
140 my @sorted_entries = sort(@entries);
141 return @sorted_entries;
144 sub got {
145 # discard stdout
146 open my $fh, '-|', ('got', @_);
147 close($fh);
148 return !$?;
151 sub got_add {
152 my $file = shift;
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: $!";
158 got 'info', $file;
159 my $found = !$?;
161 open (STDERR, ">&", $stderr) or die "can't restore stderr: $!";
163 return $found ? 1 : (got 'add', '-I', $file);
166 sub got_rm {
167 got 'remove', '-f', shift
168 or exit(1);
171 sub got_ci {
172 my $pid = fork;
173 die "failed to fork: $!" unless defined $pid;
175 if ($pid != 0) {
176 wait;
177 die "failed to commit changes" if $?;
178 return;
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: $!";
188 # cmds
190 sub cmd_cat {
191 GetOptions('h|?' => \&usage) or usage;
192 usage unless @ARGV;
194 while (@ARGV) {
195 my $file = name2file(shift @ARGV);
196 system ($gpg, @gpg_flags, '-d', $file);
197 die "failed to exec $gpg: $!" if $? == -1;
201 sub cmd_edit {
202 GetOptions('h|?' => \&usage) or usage;
203 usage if @ARGV != 1;
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 };
212 unlink $tempfile;
213 die "$@\n" if $@;
215 got_add $epath;
216 got_ci "update $entry";
219 sub cmd_find {
220 GetOptions('h|?' => \&usage) or usage;
221 usage if @ARGV > 1;
223 say $_ foreach passfind(shift @ARGV);
226 # TODO: handle moving directories?
227 sub cmd_mv {
228 GetOptions('h|?' => \&usage) or usage;
229 usage if @ARGV != 2;
231 my $a = shift @ARGV;
232 my $b = shift @ARGV;
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: $!";
243 got_rm $pa;
244 got_add $pb or die "can't add $pb\n";
245 got_ci "mv $a $b";
248 sub cmd_rm {
249 GetOptions('h|?' => \&usage) or usage;
250 usage unless @ARGV;
252 while (@ARGV) {
253 my $name = shift @ARGV;
254 my $file = name2file $name;
256 got_rm $file;
257 got_ci "-$name";
261 sub cmd_tee {
262 my $q;
263 GetOptions(
264 'h|?' => \&usage,
265 'q' => \$q,
266 ) or usage;
267 usage if @ARGV != 1;
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";
283 local $/ = \1024;
284 while (<STDIN>) {
285 print $fh $_;
286 print $_ unless $q;
288 close($fh);
289 exit $? if $?;
291 got_add $file;
292 got_ci $msg;