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';
31 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
32 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
34 my $default_chars = $ENV{'PLASS_CHARS'} // '!-~';
35 my $default_length = $ENV{'PLASS_LENGTH'};
36 if (!defined($default_length) || $default_length <= 0) {
41 cat => [\&cmd_cat, "entries..."],
42 find => [\&cmd_find, "[pattern]"],
43 gen => [\&cmd_gen, "[-nq] [-c chars] [-l length] entry"],
44 mv => [\&cmd_mv, "from to"],
45 rm => [\&cmd_rm, "entries..."],
46 tee => [\&cmd_tee, "[-q] entry"],
49 my $usage = "[-h] command [argument ...]";
52 my $prog = basename $0;
53 if (defined($cmd) and defined($subcmd{$cmd})) {
54 say STDERR "Usage: $prog $cmd $usage";
56 say STDERR "Usage: $prog $usage";
57 say STDERR "unknown command $cmd" if defined($cmd);
58 say STDERR "commands: ", join(' ', sort(keys %subcmd));
63 GetOptions("h|?" => \&usage) or usage();
65 $cmd = shift // 'find';
66 usage() unless defined $subcmd{$cmd};
68 ($fn, $usage) = @{$subcmd{$cmd}};
78 $f .= ".gpg" unless $f =~ m,\.gpg$,;
82 # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none
84 my ($chars, $length) = @_;
87 open(my $fh, '<:raw', '/dev/random')
88 or die "can't open /dev/random: $!";
91 read($fh, my $t, $length * 4)
92 or die "failed to read /dev/random: $!";
99 return substr($pass, 0, $length);
103 # todo some stty black magic to avoid echo
106 die "failed to read stdin: $!" unless defined($pass);
113 my $parent = dirname $dir;
114 mkdirs($parent) unless -d $parent || $parent eq '/';
115 mkdir $dir or die "mkdir $dir: $!"
120 my ($file, $pass) = @_;
122 mkdirs(dirname $file);
124 # temporary redirect stdout to $file
125 open(my $stdout, '>&', STDOUT);
126 open(STDOUT, '>', $file);
128 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
130 open my $fh, '|-', @args;
135 open(STDOUT, '>&', $stdout); # restore stdout
137 die "failed to run $gpg\n" unless $ok;
141 open my $fh, '<', "$store/.gpg-id"
142 or die "can't open recipient file";
155 if (m,/.git$, || m,/.got$,) {
156 $File::Find::prune = 1;
160 return if defined($pattern) && ! m/$pattern/;
161 return unless -f && m,.gpg$,;
170 return sort(@entries);
175 open my $fh, '-|', ($got, @_);
181 return got 'add', '-I', shift;
185 got 'remove', '-f', shift
191 die "failed to fork: $!" unless defined $pid;
195 die "failed to commit changes" if $?;
199 open (STDOUT, ">&", \*STDERR)
200 or die "can't redirect stdout to stderr";
201 exec ($got, 'commit', '-m', shift)
202 or die "failed to exec $got: $!";
209 GetOptions('h|?' => \&usage) or usage;
213 my $file = name2file(shift @ARGV);
214 system ($gpg, @gpg_flags, '-d', $file);
215 die "failed to exec $gpg: $!" if $? == -1;
220 GetOptions('h|?' => \&usage) or usage;
223 map { say $_ } passfind(shift @ARGV);
227 my $chars = $default_chars;
228 my $length = $default_length;
233 'c=s' => sub { $chars = $_[1] },
235 'l=i' => sub { $length = $_[1] },
240 my $pass = gen($chars, $length);
248 my $name = shift @ARGV;
249 my $file = name2file $name;
250 my $renamed = -f $file;
252 writepass($file, $pass);
254 got_ci($renamed ? "update $name" : "+$name");
258 # TODO: handle moving directories?
260 GetOptions('h|?' => \&usage) or usage;
266 my $pa = name2file $a;
267 my $pb = name2file $b;
269 die "source password doesn't exist" unless -f $pa;
270 die "target password exists" if -f $pb;
273 rename $pa, $pb or die "can't rename $a to $b: $!";
276 got_add $pb or die "can't add $pb\n";
281 GetOptions('h|?' => \&usage) or usage;
285 my $name = shift @ARGV;
286 my $file = name2file $name;
301 my $name = shift @ARGV;
302 my $file = name2file $name;
304 my $pass = readpass "Enter the password: ";
305 writepass($file, $pass);
308 got_ci (-f $file ? "update $name" : "+$name");