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 Getopt::Long qw(:config bundling require_order);
24 use File::Basename;
25 use File::Find;
26 use File::Path qw(make_path);
27 use File::Temp qw(tempfile);
29 my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
31 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
32 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
34 my %subcmd = (
35 cat => [\&cmd_cat, "entries..."],
36 edit => [\&cmd_edit, "entry"],
37 find => [\&cmd_find, "[pattern]"],
38 mv => [\&cmd_mv, "from to"],
39 rm => [\&cmd_rm, "entries..."],
40 tee => [\&cmd_tee, "[-q] entry"],
41 );
43 my $usage = "[-h] command [argument ...]";
44 my $cmd;
45 sub usage {
46 my $prog = basename $0;
47 if (defined($cmd) and defined($subcmd{$cmd})) {
48 say STDERR "Usage: $prog $cmd $usage";
49 } else {
50 say STDERR "Usage: $prog $usage";
51 say STDERR "unknown command $cmd" if defined($cmd);
52 say STDERR "commands: ", join(' ', sort(keys %subcmd));
53 }
54 exit 1;
55 }
57 GetOptions("h|?" => \&usage) or usage();
59 $cmd = shift // 'find';
60 usage() unless defined $subcmd{$cmd};
61 my $fn;
62 ($fn, $usage) = @{$subcmd{$cmd}};
63 chdir $store;
64 $fn->();
65 exit 0;
68 # utils
70 sub name2file {
71 my $f = shift;
72 $f .= ".gpg" unless $f =~ m,\.gpg$,;
73 return $f;
74 }
76 sub edit {
77 my ($editor, $fh, $tempfile, $epath) = @_;
79 open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
80 open (STDOUT, ">&", $fh) or die "can't redirect stdout to $tempfile";
81 system ($gpg, @gpg_flags, '-d', $epath);
82 die "$gpg exited with $?\n" if $? != 0;
83 open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
85 my $oldtime = (stat($fh))[9];
86 close($fh);
88 system ($editor, $tempfile);
89 die "editor $editor failed\n" if $? != 0;
91 my $newtime = (stat($tempfile))[9];
92 if ($oldtime == $newtime) {
93 say STDERR "no changes made.";
94 return;
95 }
97 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
98 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-',
99 $tempfile);
100 die "gpg failed" if $? != 0;
103 sub recipient {
104 open my $fh, '<', "$store/.gpg-id"
105 or die "can't open recipient file";
106 my $r = <$fh>;
107 chomp $r;
108 close($fh);
109 return $r;
112 sub passfind {
113 my $pattern = shift;
114 my @entries;
116 find({
117 wanted => sub {
118 if (m,/.git$, || m,/.got$,) {
119 $File::Find::prune = 1;
120 return;
122 return unless -f && m,.gpg$,;
124 s,^$store/*,,;
125 s,.gpg$,,;
127 return if defined($pattern) && ! m/$pattern/ix;
128 push @entries, $_;
129 },
130 no_chdir => 1,
131 follow_fast => 1,
132 }, ($store));
133 my @sorted_entries = sort(@entries);
134 return @sorted_entries;
137 sub got {
138 # discard stdout
139 open my $fh, '-|', ('got', @_);
140 close($fh);
141 return !$?;
144 sub got_add {
145 my $file = shift;
147 open (my $null, '>', '/dev/null') or die "can't open /dev/null: $!";
148 open (my $stderr, ">&", STDERR) or die "can't save stderr: $!";
149 open (STDERR, ">&", $null) or die "can't redirect stderr: $!";
151 got 'info', $file;
152 my $found = !$?;
154 open (STDERR, ">&", $stderr) or die "can't restore stderr: $!";
156 return $found ? 1 : (got 'add', '-I', $file);
159 sub got_rm {
160 got 'remove', '-f', shift
161 or exit(1);
164 sub got_ci {
165 my $pid = fork;
166 die "failed to fork: $!" unless defined $pid;
168 if ($pid != 0) {
169 wait;
170 die "failed to commit changes" if $?;
171 return;
174 open (STDOUT, ">&", \*STDERR)
175 or die "can't redirect stdout to stderr";
176 exec ('got', 'commit', '-m', shift)
177 or die "failed to exec got: $!";
181 # cmds
183 sub cmd_cat {
184 GetOptions('h|?' => \&usage) or usage;
185 usage unless @ARGV;
187 while (@ARGV) {
188 my $file = name2file(shift @ARGV);
189 system ($gpg, @gpg_flags, '-d', $file);
190 die "failed to exec $gpg: $!" if $? == -1;
194 sub cmd_edit {
195 GetOptions('h|?' => \&usage) or usage;
196 usage if @ARGV != 1;
198 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
200 my $entry = shift @ARGV;
201 my $epath = name2file $entry;
203 my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
204 eval { edit $editor, $fh, $tempfile, $epath };
205 unlink $tempfile;
206 die "$@\n" if $@;
208 got_add $epath;
209 got_ci "update $entry";
212 sub cmd_find {
213 GetOptions('h|?' => \&usage) or usage;
214 usage if @ARGV > 1;
216 say $_ foreach passfind(shift @ARGV);
219 # TODO: handle moving directories?
220 sub cmd_mv {
221 GetOptions('h|?' => \&usage) or usage;
222 usage if @ARGV != 2;
224 my $a = shift @ARGV;
225 my $b = shift @ARGV;
227 my $pa = name2file $a;
228 my $pb = name2file $b;
230 die "source password doesn't exist" unless -f $pa;
231 die "target password exists" if -f $pb;
233 make_path(dirname $pb);
234 rename $pa, $pb or die "can't rename $a to $b: $!";
236 got_rm $pa;
237 got_add $pb or die "can't add $pb\n";
238 got_ci "mv $a $b";
241 sub cmd_rm {
242 GetOptions('h|?' => \&usage) or usage;
243 usage unless @ARGV;
245 while (@ARGV) {
246 my $name = shift @ARGV;
247 my $file = name2file $name;
249 got_rm $file;
250 got_ci "-$name";
254 sub cmd_tee {
255 my $q;
256 GetOptions(
257 'h|?' => \&usage,
258 'q' => \$q,
259 ) or usage;
260 usage if @ARGV != 1;
262 my $name = shift @ARGV;
263 my $file = name2file $name;
265 my $msg = -f $file ? "update $name" : "+$name";
266 make_path(dirname $file);
268 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
269 '--batch', '--yes', '-o', $file);
270 open my $fh, '|-', @args;
272 binmode(STDIN) or die "cannot binmode STDIN";
273 binmode(STDOUT) or die "cannot binmode STDOUT";
274 binmode($fh) or die "cannot binmode pipe";
276 local $/ = \1024;
277 while (<STDIN>) {
278 print $fh $_;
279 print $_ unless $q;
281 close($fh);
282 exit $? if $?;
284 got_add $file;
285 got_ci $msg;