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 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', $epath,
101 '--batch', '--yes', $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 my $pid = fork;
146 die "failed to fork: $!" unless defined $pid;
148 if ($pid != 0) {
149 wait;
150 return !$?;
153 open (STDOUT, '>', '/dev/null')
154 or die "can't redirect to /dev/null";
155 exec ('got', @_);
158 sub got_add {
159 got 'add', shift
160 or exit(1);
163 sub got_rm {
164 got 'remove', '-f', shift
165 or exit(1);
168 sub got_ci {
169 my $pid = fork;
170 die "failed to fork: $!" unless defined $pid;
172 if ($pid != 0) {
173 wait;
174 die "failed to commit changes" if $?;
175 return;
178 open (STDOUT, ">&", \*STDERR)
179 or die "can't redirect stdout to stderr";
180 exec ('got', 'commit', '-m', shift)
181 or die "failed to exec got: $!";
185 # cmds
187 sub cmd_cat {
188 GetOptions('h|?' => \&usage) or usage;
189 usage unless @ARGV;
191 while (@ARGV) {
192 my $file = name2file(shift @ARGV);
193 system ($gpg, @gpg_flags, '-d', $file);
194 die "failed to exec $gpg: $!" if $? == -1;
198 sub cmd_edit {
199 GetOptions('h|?' => \&usage) or usage;
200 usage if @ARGV != 1;
202 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
204 my $entry = shift @ARGV;
205 my $epath = name2file $entry;
207 my ($fh, $tempfile) = tempfile "/tmp/plass-XXXXXXXXXX";
208 eval { edit $editor, $fh, $tempfile, $epath };
209 unlink $tempfile;
210 die "$@\n" if $@;
212 got_add $epath;
213 got_ci "update $entry";
216 sub cmd_find {
217 GetOptions('h|?' => \&usage) or usage;
218 usage if @ARGV > 1;
220 say $_ foreach passfind(shift @ARGV);
223 # TODO: handle moving directories?
224 sub cmd_mv {
225 GetOptions('h|?' => \&usage) or usage;
226 usage if @ARGV != 2;
228 my $a = shift @ARGV;
229 my $b = shift @ARGV;
231 my $pa = name2file $a;
232 my $pb = name2file $b;
234 die "source password doesn't exist" unless -f $pa;
235 die "target password exists" if -f $pb;
237 make_path(dirname $pb);
238 rename $pa, $pb or die "can't rename $a to $b: $!";
240 got_rm $pa;
241 got_add $pb or die "can't add $pb\n";
242 got_ci "mv $a $b";
245 sub cmd_rm {
246 GetOptions('h|?' => \&usage) or usage;
247 usage unless @ARGV;
249 while (@ARGV) {
250 my $name = shift @ARGV;
251 my $file = name2file $name;
253 got_rm $file;
254 got_ci "-$name";
258 sub cmd_tee {
259 my $q;
260 GetOptions(
261 'h|?' => \&usage,
262 'q' => \$q,
263 ) or usage;
264 usage if @ARGV != 1;
266 my $name = shift @ARGV;
267 my $file = name2file $name;
269 my $msg = -f $file ? "update $name" : "+$name";
270 make_path(dirname $file);
272 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
273 '--batch', '--yes', '-o', $file);
274 open my $fh, '|-', @args;
276 binmode(STDIN) or die "cannot binmode STDIN";
277 binmode(STDOUT) or die "cannot binmode STDOUT";
278 binmode($fh) or die "cannot binmode pipe";
280 local $/ = \1024;
281 while (<STDIN>) {
282 print $fh $_;
283 print $_ unless $q;
285 close($fh);
286 exit $? if $?;
288 got_add $file;
289 got_ci $msg;