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::Temp qw(tempfile);
28 my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
30 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
31 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
33 my %subcmd = (
34 cat => [\&cmd_cat, "entries..."],
35 edit => [\&cmd_edit, "entry"],
36 find => [\&cmd_find, "[pattern]"],
37 mv => [\&cmd_mv, "from to"],
38 rm => [\&cmd_rm, "entries..."],
39 tee => [\&cmd_tee, "[-q] entry"],
40 );
42 my $usage = "[-h] command [argument ...]";
43 my $cmd;
44 sub usage {
45 my $prog = basename $0;
46 if (defined($cmd) and defined($subcmd{$cmd})) {
47 say STDERR "Usage: $prog $cmd $usage";
48 } else {
49 say STDERR "Usage: $prog $usage";
50 say STDERR "unknown command $cmd" if defined($cmd);
51 say STDERR "commands: ", join(' ', sort(keys %subcmd));
52 }
53 exit 1;
54 }
56 GetOptions("h|?" => \&usage) or usage();
58 $cmd = shift // 'find';
59 usage() unless defined $subcmd{$cmd};
60 my $fn;
61 ($fn, $usage) = @{$subcmd{$cmd}};
62 chdir $store;
63 $fn->();
64 exit 0;
67 # utils
69 sub name2file {
70 my $f = shift;
71 $f .= ".gpg" unless $f =~ m,\.gpg$,;
72 return $f;
73 }
75 sub mkdirs {
76 my $dir = shift;
77 my $parent = dirname $dir;
78 mkdirs($parent) unless -d $parent || $parent eq '/';
79 mkdir $dir or die "mkdir $dir: $!"
80 unless -d $dir;
81 }
83 sub edit {
84 my ($editor, $fh, $tempfile, $epath) = @_;
86 open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
87 open (STDOUT, ">&", $fh) or die "can't redirect stdout to $tempfile";
88 system ($gpg, @gpg_flags, '-d', $epath);
89 die "$gpg exited with $?\n" if $? != 0;
90 open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
92 my $oldtime = (stat($fh))[9];
93 close($fh);
95 system ($editor, $tempfile);
96 die "editor $editor failed\n" if $? != 0;
98 my $newtime = (stat($tempfile))[9];
99 if ($oldtime == $newtime) {
100 say STDERR "no changes made.";
101 return
104 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
105 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-',
106 $tempfile);
107 die "gpg failed" if $? != 0;
110 sub recipient {
111 open my $fh, '<', "$store/.gpg-id"
112 or die "can't open recipient file";
113 my $r = <$fh>;
114 chomp $r;
115 close($fh);
116 return $r;
119 sub passfind {
120 my $pattern = shift;
121 my @entries;
123 find({
124 wanted => sub {
125 if (m,/.git$, || m,/.got$,) {
126 $File::Find::prune = 1;
127 return;
129 return unless -f && m,.gpg$,;
131 s,^$store/*,,;
132 s,.gpg$,,;
134 return if defined($pattern) && ! m/$pattern/i;
135 push @entries, $_;
136 },
137 no_chdir => 1,
138 follow_fast => 1,
139 }, ($store));
140 return sort(@entries);
143 sub got {
144 # discard stdout
145 open my $fh, '-|', ('got', @_);
146 close($fh);
147 return !$?;
150 sub got_add {
151 my $file = shift;
153 open (my $null, '>', '/dev/null') or die "can't open /dev/null: $!";
154 open (my $stderr, ">&", STDERR) or die "can't save stderr: $!";
155 open (STDERR, ">&", $null) or die "can't redirect stderr: $!";
157 got 'info', $file;
158 my $found = !$?;
160 open (STDERR, ">&", $stderr) or die "can't restore stderr: $!";
162 return $found ? 1 : (got 'add', '-I', $file);
165 sub got_rm {
166 got 'remove', '-f', shift
167 or exit(1);
170 sub got_ci {
171 my $pid = fork;
172 die "failed to fork: $!" unless defined $pid;
174 if ($pid != 0) {
175 wait;
176 die "failed to commit changes" if $?;
177 return;
180 open (STDOUT, ">&", \*STDERR)
181 or die "can't redirect stdout to stderr";
182 exec ('got', 'commit', '-m', shift)
183 or die "failed to exec got: $!";
187 # cmds
189 sub cmd_cat {
190 GetOptions('h|?' => \&usage) or usage;
191 usage unless @ARGV;
193 while (@ARGV) {
194 my $file = name2file(shift @ARGV);
195 system ($gpg, @gpg_flags, '-d', $file);
196 die "failed to exec $gpg: $!" if $? == -1;
200 sub cmd_edit {
201 GetOptions('h|?' => \&usage) or usage;
202 usage if @ARGV != 1;
204 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
206 my $entry = shift @ARGV;
207 my $epath = name2file $entry;
209 my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
210 eval { edit $editor, $fh, $tempfile, $epath };
211 unlink $tempfile;
212 die "$@\n" if $@;
214 got_add $epath;
215 got_ci "update $entry";
218 sub cmd_find {
219 GetOptions('h|?' => \&usage) or usage;
220 usage if @ARGV > 1;
222 map { say $_ } passfind(shift @ARGV);
225 # TODO: handle moving directories?
226 sub cmd_mv {
227 GetOptions('h|?' => \&usage) or usage;
228 usage if @ARGV != 2;
230 my $a = shift @ARGV;
231 my $b = shift @ARGV;
233 my $pa = name2file $a;
234 my $pb = name2file $b;
236 die "source password doesn't exist" unless -f $pa;
237 die "target password exists" if -f $pb;
239 mkdirs(dirname $pb);
240 rename $pa, $pb or die "can't rename $a to $b: $!";
242 got_rm $pa;
243 got_add $pb or die "can't add $pb\n";
244 got_ci "mv $a $b";
247 sub cmd_rm {
248 GetOptions('h|?' => \&usage) or usage;
249 usage unless @ARGV;
251 while (@ARGV) {
252 my $name = shift @ARGV;
253 my $file = name2file $name;
255 got_rm $file;
256 got_ci "-$name";
260 sub cmd_tee {
261 my $q;
262 GetOptions(
263 'h|?' => \&usage,
264 'q' => \$q,
265 ) or usage;
266 usage if @ARGV != 1;
268 my $name = shift @ARGV;
269 my $file = name2file $name;
271 my $msg = -f $file ? "update $name" : "+$name";
272 mkdirs(dirname $file);
274 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
275 '--batch', '--yes', '-o', $file);
276 open my $fh, '|-', @args;
278 binmode(STDIN) or die "cannot binmode STDIN";
279 binmode(STDOUT) or die "cannot binmode STDOUT";
280 binmode($fh) or die "cannot binmode pipe";
282 local $/ = \1024;
283 while (<STDIN>) {
284 print $fh $_;
285 print $_ unless $q;
287 close($fh);
288 exit $? if $?;
290 got_add $file;
291 got_ci $msg;