3 # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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.
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.
21 use open ":std", ":encoding(UTF-8)";
23 use Getopt::Long qw(:config bundling require_order);
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);
35 my $default_chars = $ENV{'PLASS_CHARS'} // '!-~';
36 my $default_length = $ENV{'PLASS_LENGTH'};
37 if (!defined($default_length) || $default_length lt 0) {
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 ..."],
52 my $usage = "[-h] [command argument ...]";
55 my $prog = basename $0;
57 say STDERR "Usage: $prog $cmd $usage";
59 say STDERR "Usage: $prog $usage";
60 say STDERR "commands: ", join(' ', sort(keys %subcmd));
65 GetOptions("h|?" => \&usage) or usage();
67 $cmd = shift // 'find';
68 usage() unless defined $subcmd{$cmd};
70 ($fn, $usage) = @{$subcmd{$cmd}};
80 $f .= ".gpg" unless $f =~ m,\.gpg$,;
84 # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none
86 my ($chars, $length) = @_;
89 open(my $fh, '<:raw', '/dev/random')
90 or die "can't open /dev/random: $!";
93 read($fh, my $t, $length * 4)
94 or die "failed to read /dev/random: $!";
101 return substr($pass, 0, $length);
105 # todo some stty black magic to avoid echo
108 die "failed to read stdin: $!" unless defined($pass);
115 my $parent = dirname $dir;
116 mkdirs($parent) unless -d $parent || $parent eq '/';
117 mkdir $dir or die "mkdir $dir: $!"
122 my ($file, $pass) = @_;
124 mkdirs(dirname $file);
126 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
128 open my $fh, '|-', @args;
134 open my $fh, '<', "$store/.gpg-id"
135 or die "can't open recipient file";
148 if (m,/.git$, || m,/.got$,) {
149 $File::Find::prune = 1;
153 return if defined($pattern) && ! m/$pattern/;
154 return unless -f && m,.gpg$,;
163 return sort(@entries);
168 open my $fh, '-|', ($got, @_);
174 return got 'add', '-I', shift;
178 got 'remove', '-f', shift
184 die "failed to fork: $!" unless defined $pid;
188 die "failed to commit changes" if $?;
192 open (STDOUT, ">&", \*STDERR)
193 or die "can't redirect stdout to stderr";
194 exec ($got, 'commit', '-m', shift)
195 or die "failed to exec $got: $!";
202 GetOptions('h|?' => \&usage) or usage;
206 my $file = name2file(shift @ARGV);
207 system ($gpg, @gpg_flags, '-d', $file);
208 die "failed to exec $gpg: $!" if $? == -1;
213 GetOptions('h|?' => \&usage) or usage;
216 map { say $_ } passfind(shift @ARGV);
220 my $chars = $default_chars;
221 my $length = $default_length;
226 'c=s' => sub { $chars = $_[1] },
228 'l=i' => sub { $length = $_[1] },
234 my $name = shift @ARGV;
235 my $file = name2file $name;
236 my $renamed = -f $file;
238 my $pass = gen($chars, $length);
241 writepass($file, $pass);
243 got_ci($renamed ? "update $name" : "+$name");
252 # TODO: handle moving directories?
254 GetOptions('h|?' => \&usage) or usage;
260 my $pa = name2file $a;
261 my $pb = name2file $b;
263 die "source password doesn't exist" unless -f $pa;
264 die "target password exists" if -f $pb;
266 rename $pa, $pb or die "can't rename $a to $b: $!";
269 got_add $pb or die "can't add $pb\n";
274 GetOptions('h|?' => \&usage) or usage;
278 my $name = shift @ARGV;
279 my $file = name2file $name;
294 my $name = shift @ARGV;
295 my $file = name2file $name;
297 my $pass = readpass "Enter the password: ";
298 writepass($file, $pass);
301 got_ci (-f $file ? "update $name" : "+$name");