1 5cdf5adc 2022-05-08 op #!/usr/bin/env perl
3 5cdf5adc 2022-05-08 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
5 5cdf5adc 2022-05-08 op # Permission to use, copy, modify, and distribute this software for any
6 5cdf5adc 2022-05-08 op # purpose with or without fee is hereby granted, provided that the above
7 5cdf5adc 2022-05-08 op # copyright notice and this permission notice appear in all copies.
9 5cdf5adc 2022-05-08 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5cdf5adc 2022-05-08 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5cdf5adc 2022-05-08 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5cdf5adc 2022-05-08 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5cdf5adc 2022-05-08 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5cdf5adc 2022-05-08 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5cdf5adc 2022-05-08 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 5cdf5adc 2022-05-08 op use open ":std", ":encoding(UTF-8)";
23 5cdf5adc 2022-05-08 op use Getopt::Long qw(:config bundling require_order);
24 5cdf5adc 2022-05-08 op use File::Basename;
25 5cdf5adc 2022-05-08 op use File::Find;
27 50c751f3 2022-06-29 op my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
29 50c751f3 2022-06-29 op my $got = $ENV{'PLASS_GOT'} // 'got';
31 d16aedaf 2022-09-09 op my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
32 12ad62fd 2022-06-29 op my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
34 50c751f3 2022-06-29 op my $default_chars = $ENV{'PLASS_CHARS'} // '!-~';
35 5cdf5adc 2022-05-08 op my $default_length = $ENV{'PLASS_LENGTH'};
36 f9b1eaa1 2022-06-29 op if (!defined($default_length) || $default_length <= 0) {
37 5cdf5adc 2022-05-08 op $default_length = 32;
40 8dc394df 2022-06-29 op my %subcmd = (
41 8dc394df 2022-06-29 op cat => [\&cmd_cat, "entries..."],
42 8dc394df 2022-06-29 op find => [\&cmd_find, "[pattern]"],
43 8dc394df 2022-06-29 op gen => [\&cmd_gen, "[-nq] [-c chars] [-l length] entry"],
44 8dc394df 2022-06-29 op mv => [\&cmd_mv, "from to"],
45 8dc394df 2022-06-29 op rm => [\&cmd_rm, "entries..."],
46 8dc394df 2022-06-29 op tee => [\&cmd_tee, "[-q] entry"],
49 f19c01bc 2022-09-09 op my $usage = "[-h] command [argument ...]";
52 5f8e7670 2022-06-29 op my $prog = basename $0;
53 d1bd3477 2022-06-29 op if (defined($cmd) and defined($subcmd{$cmd})) {
54 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $cmd $usage";
56 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $usage";
57 d1bd3477 2022-06-29 op say STDERR "unknown command $cmd" if defined($cmd);
58 8dc394df 2022-06-29 op say STDERR "commands: ", join(' ', sort(keys %subcmd));
63 5f8e7670 2022-06-29 op GetOptions("h|?" => \&usage) or usage();
65 5f8e7670 2022-06-29 op $cmd = shift // 'find';
66 5f8e7670 2022-06-29 op usage() unless defined $subcmd{$cmd};
68 5f8e7670 2022-06-29 op ($fn, $usage) = @{$subcmd{$cmd}};
76 5cdf5adc 2022-05-08 op sub name2file {
77 5cdf5adc 2022-05-08 op my $f = shift;
78 5cdf5adc 2022-05-08 op $f .= ".gpg" unless $f =~ m,\.gpg$,;
82 5cdf5adc 2022-05-08 op # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none
84 5cdf5adc 2022-05-08 op my ($chars, $length) = @_;
85 5cdf5adc 2022-05-08 op my $pass = "";
87 5cdf5adc 2022-05-08 op open(my $fh, '<:raw', '/dev/random')
88 5cdf5adc 2022-05-08 op or die "can't open /dev/random: $!";
89 5cdf5adc 2022-05-08 op my $l = $length;
90 f9b1eaa1 2022-06-29 op while ($l >= 0) {
91 5cdf5adc 2022-05-08 op read($fh, my $t, $length * 4)
92 5cdf5adc 2022-05-08 op or die "failed to read /dev/random: $!";
93 5cdf5adc 2022-05-08 op $t =~ s/[^$chars]//g;
94 5cdf5adc 2022-05-08 op $l -= length($t);
99 5cdf5adc 2022-05-08 op return substr($pass, 0, $length);
102 5cdf5adc 2022-05-08 op sub readpass {
103 5cdf5adc 2022-05-08 op # todo some stty black magic to avoid echo
104 5cdf5adc 2022-05-08 op print shift if -t;
105 5cdf5adc 2022-05-08 op my $pass = <>;
106 5cdf5adc 2022-05-08 op die "failed to read stdin: $!" unless defined($pass);
108 5cdf5adc 2022-05-08 op return $pass;
112 1f89d66a 2022-06-29 op my $dir = shift;
113 1f89d66a 2022-06-29 op my $parent = dirname $dir;
114 1f89d66a 2022-06-29 op mkdirs($parent) unless -d $parent || $parent eq '/';
115 1f89d66a 2022-06-29 op mkdir $dir or die "mkdir $dir: $!"
116 1f89d66a 2022-06-29 op unless -d $dir;
119 5cdf5adc 2022-05-08 op sub writepass {
120 5cdf5adc 2022-05-08 op my ($file, $pass) = @_;
122 1f89d66a 2022-06-29 op mkdirs(dirname $file);
124 945f0333 2022-06-29 op # temporary redirect stdout to $file
125 945f0333 2022-06-29 op open(my $stdout, '>&', STDOUT);
126 945f0333 2022-06-29 op open(STDOUT, '>', $file);
128 5cdf5adc 2022-05-08 op my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
130 5cdf5adc 2022-05-08 op open my $fh, '|-', @args;
131 5cdf5adc 2022-05-08 op say $fh "$pass";
133 945f0333 2022-06-29 op my $ok = !$?;
135 945f0333 2022-06-29 op open(STDOUT, '>&', $stdout); # restore stdout
137 945f0333 2022-06-29 op die "failed to run $gpg\n" unless $ok;
140 5cdf5adc 2022-05-08 op sub recipient {
141 5cdf5adc 2022-05-08 op open my $fh, '<', "$store/.gpg-id"
142 5cdf5adc 2022-05-08 op or die "can't open recipient file";
143 5cdf5adc 2022-05-08 op my $r = <$fh>;
149 5cdf5adc 2022-05-08 op sub passfind {
150 5cdf5adc 2022-05-08 op my $pattern = shift;
154 5cdf5adc 2022-05-08 op wanted => sub {
155 5cdf5adc 2022-05-08 op if (m,/.git$, || m,/.got$,) {
156 5cdf5adc 2022-05-08 op $File::Find::prune = 1;
160 5cdf5adc 2022-05-08 op return if defined($pattern) && ! m/$pattern/;
161 5cdf5adc 2022-05-08 op return unless -f && m,.gpg$,;
163 5cdf5adc 2022-05-08 op s,^$store/*,,;
165 5cdf5adc 2022-05-08 op push @entries, $_;
167 5cdf5adc 2022-05-08 op no_chdir => 1,
168 5cdf5adc 2022-05-08 op follow_fast => 1,
169 5cdf5adc 2022-05-08 op }, ($store));
170 5cdf5adc 2022-05-08 op return sort(@entries);
174 87197963 2022-06-29 op # discard stdout
175 5cdf5adc 2022-05-08 op open my $fh, '-|', ($got, @_);
180 5cdf5adc 2022-05-08 op sub got_add {
181 f96edc25 2022-06-29 op return got 'add', '-I', shift;
185 f96edc25 2022-06-29 op got 'remove', '-f', shift
190 afdbc7b0 2022-06-29 op my $pid = fork;
191 afdbc7b0 2022-06-29 op die "failed to fork: $!" unless defined $pid;
193 f9b1eaa1 2022-06-29 op if ($pid != 0) {
195 afdbc7b0 2022-06-29 op die "failed to commit changes" if $?;
199 afdbc7b0 2022-06-29 op open (STDOUT, ">&", \*STDERR)
200 afdbc7b0 2022-06-29 op or die "can't redirect stdout to stderr";
201 afdbc7b0 2022-06-29 op exec ($got, 'commit', '-m', shift)
202 afdbc7b0 2022-06-29 op or die "failed to exec $got: $!";
208 5cdf5adc 2022-05-08 op sub cmd_cat {
209 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
210 5cdf5adc 2022-05-08 op usage unless @ARGV;
212 5cdf5adc 2022-05-08 op while (@ARGV) {
213 5cdf5adc 2022-05-08 op my $file = name2file(shift @ARGV);
214 5cdf5adc 2022-05-08 op system ($gpg, @gpg_flags, '-d', $file);
215 5cdf5adc 2022-05-08 op die "failed to exec $gpg: $!" if $? == -1;
219 5cdf5adc 2022-05-08 op sub cmd_find {
220 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
221 f9b1eaa1 2022-06-29 op usage if @ARGV > 1;
223 5cdf5adc 2022-05-08 op map { say $_ } passfind(shift @ARGV);
226 5cdf5adc 2022-05-08 op sub cmd_gen {
227 5cdf5adc 2022-05-08 op my $chars = $default_chars;
228 5cdf5adc 2022-05-08 op my $length = $default_length;
233 5cdf5adc 2022-05-08 op 'c=s' => sub { $chars = $_[1] },
234 5cdf5adc 2022-05-08 op 'h|?' => \&usage,
235 5cdf5adc 2022-05-08 op 'l=i' => sub { $length = $_[1] },
236 9d75d6d8 2022-06-29 op 'n' => \$nop,
240 8bd06def 2022-10-01 op my $pass = gen($chars, $length);
246 f9b1eaa1 2022-06-29 op usage if @ARGV != 1;
248 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
249 5cdf5adc 2022-05-08 op my $file = name2file $name;
250 bd71caf6 2022-06-29 op my $renamed = -f $file;
252 8bd06def 2022-10-01 op writepass($file, $pass);
253 8bd06def 2022-10-01 op got_add $file;
254 8bd06def 2022-10-01 op got_ci($renamed ? "update $name" : "+$name");
255 9d75d6d8 2022-06-29 op say $pass unless $q;
258 5cdf5adc 2022-05-08 op # TODO: handle moving directories?
260 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
261 f9b1eaa1 2022-06-29 op usage if @ARGV != 2;
263 5cdf5adc 2022-05-08 op my $a = shift @ARGV;
264 5cdf5adc 2022-05-08 op my $b = shift @ARGV;
266 5cdf5adc 2022-05-08 op my $pa = name2file $a;
267 5cdf5adc 2022-05-08 op my $pb = name2file $b;
269 5cdf5adc 2022-05-08 op die "source password doesn't exist" unless -f $pa;
270 5cdf5adc 2022-05-08 op die "target password exists" if -f $pb;
272 4568ada0 2022-06-29 op mkdirs(dirname $pb);
273 5cdf5adc 2022-05-08 op rename $pa, $pb or die "can't rename $a to $b: $!";
276 bd71caf6 2022-06-29 op got_add $pb or die "can't add $pb\n";
277 5cdf5adc 2022-05-08 op got_ci "mv $a $b";
281 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
282 6a455fdf 2022-06-29 op usage unless @ARGV;
284 6a455fdf 2022-06-29 op while (@ARGV) {
285 6a455fdf 2022-06-29 op my $name = shift @ARGV;
286 6a455fdf 2022-06-29 op my $file = name2file $name;
288 6a455fdf 2022-06-29 op got_rm $file;
289 6a455fdf 2022-06-29 op got_ci "-$name";
293 afdbc7b0 2022-06-29 op sub cmd_tee {
296 afdbc7b0 2022-06-29 op 'h|?' => \&usage,
299 f9b1eaa1 2022-06-29 op usage if @ARGV != 1;
301 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
302 5cdf5adc 2022-05-08 op my $file = name2file $name;
304 d69b7902 2022-05-15 op my $pass = readpass "Enter the password: ";
305 d69b7902 2022-05-15 op writepass($file, $pass);
307 5cdf5adc 2022-05-08 op got_add $file;
308 afdbc7b0 2022-06-29 op got_ci (-f $file ? "update $name" : "+$name");
309 b2653662 2022-06-29 op say $pass unless $q;