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 $got = $ENV{'PLASS_GOT'} // 'got';
32 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
33 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
35 my %subcmd = (
36 cat => [\&cmd_cat, "entries..."],
37 edit => [\&cmd_edit, "entry"],
38 find => [\&cmd_find, "[pattern]"],
39 mv => [\&cmd_mv, "from to"],
40 rm => [\&cmd_rm, "entries..."],
41 tee => [\&cmd_tee, "[-q] entry"],
42 );
44 my $usage = "[-h] command [argument ...]";
45 my $cmd;
46 sub usage {
47 my $prog = basename $0;
48 if (defined($cmd) and defined($subcmd{$cmd})) {
49 say STDERR "Usage: $prog $cmd $usage";
50 } else {
51 say STDERR "Usage: $prog $usage";
52 say STDERR "unknown command $cmd" if defined($cmd);
53 say STDERR "commands: ", join(' ', sort(keys %subcmd));
54 }
55 exit 1;
56 }
58 GetOptions("h|?" => \&usage) or usage();
60 $cmd = shift // 'find';
61 usage() unless defined $subcmd{$cmd};
62 my $fn;
63 ($fn, $usage) = @{$subcmd{$cmd}};
64 chdir $store;
65 $fn->();
66 exit 0;
69 # utils
71 sub name2file {
72 my $f = shift;
73 $f .= ".gpg" unless $f =~ m,\.gpg$,;
74 return $f;
75 }
77 sub mkdirs {
78 my $dir = shift;
79 my $parent = dirname $dir;
80 mkdirs($parent) unless -d $parent || $parent eq '/';
81 mkdir $dir or die "mkdir $dir: $!"
82 unless -d $dir;
83 }
85 sub writepass {
86 my ($file, $pass) = @_;
88 mkdirs(dirname $file);
90 # temporary redirect stdout to $file
91 open(my $stdout, '>&', STDOUT);
92 open(STDOUT, '>', $file);
94 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
95 '-o', '-');
96 open my $fh, '|-', @args;
97 print $fh "$pass";
98 close($fh);
99 my $ok = !$?;
101 open(STDOUT, '>&', $stdout); # restore stdout
103 die "failed to run $gpg\n" unless $ok;
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 find({
120 wanted => sub {
121 if (m,/.git$, || m,/.got$,) {
122 $File::Find::prune = 1;
123 return;
125 return unless -f && m,.gpg$,;
127 s,^$store/*,,;
128 s,.gpg$,,;
130 return if defined($pattern) && ! m/$pattern/i;
131 push @entries, $_;
132 },
133 no_chdir => 1,
134 follow_fast => 1,
135 }, ($store));
136 return sort(@entries);
139 sub got {
140 # discard stdout
141 open my $fh, '-|', ($got, @_);
142 close($fh);
143 return !$?;
146 sub got_add {
147 return got 'add', '-I', shift;
150 sub got_rm {
151 got 'remove', '-f', shift
152 or exit(1);
155 sub got_ci {
156 my $pid = fork;
157 die "failed to fork: $!" unless defined $pid;
159 if ($pid != 0) {
160 wait;
161 die "failed to commit changes" if $?;
162 return;
165 open (STDOUT, ">&", \*STDERR)
166 or die "can't redirect stdout to stderr";
167 exec ($got, 'commit', '-m', shift)
168 or die "failed to exec $got: $!";
172 # cmds
174 sub cmd_cat {
175 GetOptions('h|?' => \&usage) or usage;
176 usage unless @ARGV;
178 while (@ARGV) {
179 my $file = name2file(shift @ARGV);
180 system ($gpg, @gpg_flags, '-d', $file);
181 die "failed to exec $gpg: $!" if $? == -1;
185 sub cmd_edit {
186 GetOptions('h|?' => \&usage) or usage;
187 usage if @ARGV != 1;
189 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
191 my $entry = shift @ARGV;
192 my $epath = name2file $entry;
194 my ($fh, $filename) = tempfile "/tmp/plass-XXXXXXXXXX";
196 open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
198 open (STDOUT, ">&", $fh) or die "can't redirect stdout to $filename";
199 system ($gpg, @gpg_flags, '-d', $epath);
200 die "$gpg exited with $?\n" if $? != 0;
202 # restore stdout so the editor can access the TTY if needed.
203 open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
205 my $oldtime = (stat($fh))[9];
207 system ($editor, $filename);
208 die "editor $editor failed\n" if $? != 0;
210 my $newtime = (stat($filename))[9];
212 if ($oldtime == $newtime) {
213 say STDERR "no changes made.";
214 unlink $filename or die "can't delete $filename: $!\n";
215 return
218 open(STDIN, '<', $filename) or die "can't redirect stdin: $!";
219 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
220 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-');
221 unlink $filename or die "can't delete $filename: $!\n";
222 die "gpg failed" if $? != 0;
224 got_add $epath;
225 got_ci "update $entry";
228 sub cmd_find {
229 GetOptions('h|?' => \&usage) or usage;
230 usage if @ARGV > 1;
232 map { say $_ } passfind(shift @ARGV);
235 # TODO: handle moving directories?
236 sub cmd_mv {
237 GetOptions('h|?' => \&usage) or usage;
238 usage if @ARGV != 2;
240 my $a = shift @ARGV;
241 my $b = shift @ARGV;
243 my $pa = name2file $a;
244 my $pb = name2file $b;
246 die "source password doesn't exist" unless -f $pa;
247 die "target password exists" if -f $pb;
249 mkdirs(dirname $pb);
250 rename $pa, $pb or die "can't rename $a to $b: $!";
252 got_rm $pa;
253 got_add $pb or die "can't add $pb\n";
254 got_ci "mv $a $b";
257 sub cmd_rm {
258 GetOptions('h|?' => \&usage) or usage;
259 usage unless @ARGV;
261 while (@ARGV) {
262 my $name = shift @ARGV;
263 my $file = name2file $name;
265 got_rm $file;
266 got_ci "-$name";
270 sub cmd_tee {
271 my $q;
272 GetOptions(
273 'h|?' => \&usage,
274 'q' => \$q,
275 ) or usage;
276 usage if @ARGV != 1;
278 my $name = shift @ARGV;
279 my $file = name2file $name;
281 my $msg = -f $file ? "update $name" : "+$name";
283 my $pass = "";
284 $pass .= $_ while <STDIN>;
285 die "No content!\n" if $pass eq "";
287 writepass($file, $pass);
289 got_add $file;
290 got_ci $msg;
291 print $pass unless $q;