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);
104 my $parent = dirname $dir;
105 mkdirs($parent) unless -d $parent || $parent eq '/';
106 mkdir $dir or die "mkdir $dir: $!"
111 my ($file, $pass) = @_;
113 mkdirs(dirname $file);
115 # temporary redirect stdout to $file
116 open(my $stdout, '>&', STDOUT);
117 open(STDOUT, '>', $file);
119 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
121 open my $fh, '|-', @args;
126 open(STDOUT, '>&', $stdout); # restore stdout
128 die "failed to run $gpg\n" unless $ok;
132 open my $fh, '<', "$store/.gpg-id"
133 or die "can't open recipient file";
146 if (m,/.git$, || m,/.got$,) {
147 $File::Find::prune = 1;
150 return unless -f && m,.gpg$,;
155 return if defined($pattern) && ! m/$pattern/;
161 return sort(@entries);
166 open my $fh, '-|', ($got, @_);
172 return got 'add', '-I', shift;
176 got 'remove', '-f', shift
182 die "failed to fork: $!" unless defined $pid;
186 die "failed to commit changes" if $?;
190 open (STDOUT, ">&", \*STDERR)
191 or die "can't redirect stdout to stderr";
192 exec ($got, 'commit', '-m', shift)
193 or die "failed to exec $got: $!";
200 GetOptions('h|?' => \&usage) or usage;
204 my $file = name2file(shift @ARGV);
205 system ($gpg, @gpg_flags, '-d', $file);
206 die "failed to exec $gpg: $!" if $? == -1;
211 GetOptions('h|?' => \&usage) or usage;
214 map { say $_ } passfind(shift @ARGV);
218 my $chars = $default_chars;
219 my $length = $default_length;
224 'c=s' => sub { $chars = $_[1] },
226 'l=i' => sub { $length = $_[1] },
231 my $pass = gen($chars, $length) . "\n";
239 my $name = shift @ARGV;
240 my $file = name2file $name;
241 my $renamed = -f $file;
243 writepass($file, $pass);
245 got_ci($renamed ? "update $name" : "+$name");
249 # TODO: handle moving directories?
251 GetOptions('h|?' => \&usage) or usage;
257 my $pa = name2file $a;
258 my $pb = name2file $b;
260 die "source password doesn't exist" unless -f $pa;
261 die "target password exists" if -f $pb;
264 rename $pa, $pb or die "can't rename $a to $b: $!";
267 got_add $pb or die "can't add $pb\n";
272 GetOptions('h|?' => \&usage) or usage;
276 my $name = shift @ARGV;
277 my $file = name2file $name;
292 my $name = shift @ARGV;
293 my $file = name2file $name;
295 my $msg = -f $file ? "update $name" : "+$name";
298 $pass .= $_ while <STDIN>;
299 die "No content!\n" if $pass eq "";
301 writepass($file, $pass);
305 print $pass unless $q;