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 --use-agent);
35 my $default_chars = $ENV{'PLASS_CHARS'} // '!-~';
36 my $default_length = $ENV{'PLASS_LENGTH'};
37 if (!defined($default_length) || $default_length <= 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;
56 if (defined($cmd) and defined($subcmd{$cmd})) {
57 say STDERR "Usage: $prog $cmd $usage";
59 say STDERR "Usage: $prog $usage";
60 say STDERR "unknown command $cmd" if defined($cmd);
61 say STDERR "commands: ", join(' ', sort(keys %subcmd));
66 GetOptions("h|?" => \&usage) or usage();
68 $cmd = shift // 'find';
69 usage() unless defined $subcmd{$cmd};
71 ($fn, $usage) = @{$subcmd{$cmd}};
81 $f .= ".gpg" unless $f =~ m,\.gpg$,;
85 # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none
87 my ($chars, $length) = @_;
90 open(my $fh, '<:raw', '/dev/random')
91 or die "can't open /dev/random: $!";
94 read($fh, my $t, $length * 4)
95 or die "failed to read /dev/random: $!";
102 return substr($pass, 0, $length);
106 # todo some stty black magic to avoid echo
109 die "failed to read stdin: $!" unless defined($pass);
116 my $parent = dirname $dir;
117 mkdirs($parent) unless -d $parent || $parent eq '/';
118 mkdir $dir or die "mkdir $dir: $!"
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(),
133 open my $fh, '|-', @args;
138 open(STDOUT, '>&', $stdout); # restore stdout
140 die "failed to run $gpg\n" unless $ok;
144 open my $fh, '<', "$store/.gpg-id"
145 or die "can't open recipient file";
158 if (m,/.git$, || m,/.got$,) {
159 $File::Find::prune = 1;
163 return if defined($pattern) && ! m/$pattern/;
164 return unless -f && m,.gpg$,;
173 return sort(@entries);
178 open my $fh, '-|', ($got, @_);
184 return got 'add', '-I', shift;
188 got 'remove', '-f', shift
194 die "failed to fork: $!" unless defined $pid;
198 die "failed to commit changes" if $?;
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: $!";
212 GetOptions('h|?' => \&usage) or usage;
216 my $file = name2file(shift @ARGV);
217 system ($gpg, @gpg_flags, '-d', $file);
218 die "failed to exec $gpg: $!" if $? == -1;
223 GetOptions('h|?' => \&usage) or usage;
226 map { say $_ } passfind(shift @ARGV);
230 my $chars = $default_chars;
231 my $length = $default_length;
236 'c=s' => sub { $chars = $_[1] },
238 'l=i' => sub { $length = $_[1] },
244 my $name = shift @ARGV;
245 my $file = name2file $name;
246 my $renamed = -f $file;
248 my $pass = gen($chars, $length);
251 writepass($file, $pass);
253 got_ci($renamed ? "update $name" : "+$name");
262 # TODO: handle moving directories?
264 GetOptions('h|?' => \&usage) or usage;
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;
277 rename $pa, $pb or die "can't rename $a to $b: $!";
280 got_add $pb or die "can't add $pb\n";
285 GetOptions('h|?' => \&usage) or usage;
289 my $name = shift @ARGV;
290 my $file = name2file $name;
305 my $name = shift @ARGV;
306 my $file = name2file $name;
308 my $pass = readpass "Enter the password: ";
309 writepass($file, $pass);
312 got_ci (-f $file ? "update $name" : "+$name");