Blob


1 #!/usr/bin/env perl
2 #
3 # Copyright (c) 2022 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;
27 my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
29 my $got = $ENV{'PLASS_GOT'} // 'got';
30 my $tog = $ENV{'PLASS_TOG'} // 'tog';
32 my $gpg = $ENV{'PLASS_GPG'} // 'gpg2';
33 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
35 my $default_chars = $ENV{'PLASS_CHARS'} // '!-~';
36 my $default_length = $ENV{'PLASS_LENGTH'};
37 if (!defined($default_length) || $default_length <= 0) {
38 $default_length = 32;
39 }
41 my %subcmd = (
42 cat => [\&cmd_cat, "entries..."],
43 find => [\&cmd_find, "[pattern]"],
44 gen => [\&cmd_gen, "[-nq] [-c chars] [-l length] entry"],
45 got => [\&cmd_got, "args ..."],
46 mv => [\&cmd_mv, "from to"],
47 rm => [\&cmd_rm, "entries..."],
48 tee => [\&cmd_tee, "[-q] entry"],
49 tog => [\&cmd_tog, "args ..."],
50 );
52 my $usage = "[-h] [command argument ...]";
53 my $cmd;
54 sub usage {
55 my $prog = basename $0;
56 if (defined($cmd) and defined($subcmd{$cmd})) {
57 say STDERR "Usage: $prog $cmd $usage";
58 } else {
59 say STDERR "Usage: $prog $usage";
60 say STDERR "unknown command $cmd" if defined($cmd);
61 say STDERR "commands: ", join(' ', sort(keys %subcmd));
62 }
63 exit 1;
64 }
66 GetOptions("h|?" => \&usage) or usage();
68 $cmd = shift // 'find';
69 usage() unless defined $subcmd{$cmd};
70 my $fn;
71 ($fn, $usage) = @{$subcmd{$cmd}};
72 chdir $store;
73 $fn->();
74 exit 0;
77 # utils
79 sub name2file {
80 my $f = shift;
81 $f .= ".gpg" unless $f =~ m,\.gpg$,;
82 return $f;
83 }
85 # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none
86 sub gen {
87 my ($chars, $length) = @_;
88 my $pass = "";
90 open(my $fh, '<:raw', '/dev/random')
91 or die "can't open /dev/random: $!";
92 my $l = $length;
93 while ($l >= 0) {
94 read($fh, my $t, $length * 4)
95 or die "failed to read /dev/random: $!";
96 $t =~ s/[^$chars]//g;
97 $l -= length($t);
98 $pass .= $t;
99 }
100 close($fh);
102 return substr($pass, 0, $length);
105 sub readpass {
106 # todo some stty black magic to avoid echo
107 print shift if -t;
108 my $pass = <>;
109 die "failed to read stdin: $!" unless defined($pass);
110 chomp $pass;
111 return $pass;
114 sub mkdirs {
115 my $dir = shift;
116 my $parent = dirname $dir;
117 mkdirs($parent) unless -d $parent || $parent eq '/';
118 mkdir $dir or die "mkdir $dir: $!"
119 unless -d $dir;
122 sub writepass {
123 my ($file, $pass) = @_;
125 mkdirs(dirname $file);
127 # temporary redirect stdout to $file
128 open(my $stdout, '>&', STDOUT);
129 open(STDOUT, '>', $file);
131 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
132 '-o', '-');
133 open my $fh, '|-', @args;
134 say $fh "$pass";
135 close($fh);
136 my $ok = !$?;
138 open(STDOUT, '>&', $stdout); # restore stdout
140 die "failed to run $gpg\n" unless $ok;
143 sub recipient {
144 open my $fh, '<', "$store/.gpg-id"
145 or die "can't open recipient file";
146 my $r = <$fh>;
147 chomp $r;
148 close($fh);
149 return $r;
152 sub passfind {
153 my $pattern = shift;
154 my @entries;
156 find({
157 wanted => sub {
158 if (m,/.git$, || m,/.got$,) {
159 $File::Find::prune = 1;
160 return;
163 return if defined($pattern) && ! m/$pattern/;
164 return unless -f && m,.gpg$,;
166 s,^$store/*,,;
167 s,.gpg$,,;
168 push @entries, $_;
169 },
170 no_chdir => 1,
171 follow_fast => 1,
172 }, ($store));
173 return sort(@entries);
176 sub got {
177 # discard stdout
178 open my $fh, '-|', ($got, @_);
179 close($fh);
180 return !$?;
183 sub got_add {
184 return got 'add', '-I', shift;
187 sub got_rm {
188 got 'remove', '-f', shift
189 or exit(1);
192 sub got_ci {
193 my $pid = fork;
194 die "failed to fork: $!" unless defined $pid;
196 if ($pid != 0) {
197 wait;
198 die "failed to commit changes" if $?;
199 return;
202 open (STDOUT, ">&", \*STDERR)
203 or die "can't redirect stdout to stderr";
204 exec ($got, 'commit', '-m', shift)
205 or die "failed to exec $got: $!";
209 # cmds
211 sub cmd_cat {
212 GetOptions('h|?' => \&usage) or usage;
213 usage unless @ARGV;
215 while (@ARGV) {
216 my $file = name2file(shift @ARGV);
217 system ($gpg, @gpg_flags, '-d', $file);
218 die "failed to exec $gpg: $!" if $? == -1;
222 sub cmd_find {
223 GetOptions('h|?' => \&usage) or usage;
224 usage if @ARGV > 1;
226 map { say $_ } passfind(shift @ARGV);
229 sub cmd_gen {
230 my $chars = $default_chars;
231 my $length = $default_length;
232 my $nop;
233 my $q;
235 GetOptions(
236 'c=s' => sub { $chars = $_[1] },
237 'h|?' => \&usage,
238 'l=i' => sub { $length = $_[1] },
239 'n' => \$nop,
240 'q' => \$q,
241 ) or usage;
242 usage if @ARGV != 1;
244 my $name = shift @ARGV;
245 my $file = name2file $name;
246 my $renamed = -f $file;
248 my $pass = gen($chars, $length);
250 unless ($nop) {
251 writepass($file, $pass);
252 got_add $file;
253 got_ci($renamed ? "update $name" : "+$name");
255 say $pass unless $q;
258 sub cmd_got {
259 exec $got, @ARGV;
262 # TODO: handle moving directories?
263 sub cmd_mv {
264 GetOptions('h|?' => \&usage) or usage;
265 usage if @ARGV != 2;
267 my $a = shift @ARGV;
268 my $b = shift @ARGV;
270 my $pa = name2file $a;
271 my $pb = name2file $b;
273 die "source password doesn't exist" unless -f $pa;
274 die "target password exists" if -f $pb;
276 mkdirs(dirname $pb);
277 rename $pa, $pb or die "can't rename $a to $b: $!";
279 got_rm $pa;
280 got_add $pb or die "can't add $pb\n";
281 got_ci "mv $a $b";
284 sub cmd_rm {
285 GetOptions('h|?' => \&usage) or usage;
286 usage unless @ARGV;
288 while (@ARGV) {
289 my $name = shift @ARGV;
290 my $file = name2file $name;
292 got_rm $file;
293 got_ci "-$name";
297 sub cmd_tee {
298 my $q;
299 GetOptions(
300 'h|?' => \&usage,
301 'q' => \$q,
302 ) or usage;
303 usage if @ARGV != 1;
305 my $name = shift @ARGV;
306 my $file = name2file $name;
308 my $pass = readpass "Enter the password: ";
309 writepass($file, $pass);
311 got_add $file;
312 got_ci (-f $file ? "update $name" : "+$name");
313 say $pass unless $q;
316 sub cmd_tog {
317 exec $tog, @ARGV;