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';
30 50c751f3 2022-06-29 op my $tog = $ENV{'PLASS_TOG'} // 'tog';
32 50c751f3 2022-06-29 op my $gpg = $ENV{'PLASS_GPG'} // 'gpg2';
33 5cdf5adc 2022-05-08 op my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to);
35 50c751f3 2022-06-29 op my $default_chars = $ENV{'PLASS_CHARS'} // '!-~';
36 5cdf5adc 2022-05-08 op my $default_length = $ENV{'PLASS_LENGTH'};
37 5cdf5adc 2022-05-08 op if (!defined($default_length) || $default_length lt 0) {
38 5cdf5adc 2022-05-08 op $default_length = 32;
41 8dc394df 2022-06-29 op my %subcmd = (
42 8dc394df 2022-06-29 op cat => [\&cmd_cat, "entries..."],
43 8dc394df 2022-06-29 op find => [\&cmd_find, "[pattern]"],
44 8dc394df 2022-06-29 op gen => [\&cmd_gen, "[-nq] [-c chars] [-l length] entry"],
45 8dc394df 2022-06-29 op got => [\&cmd_got, "args ..."],
46 8dc394df 2022-06-29 op mv => [\&cmd_mv, "from to"],
47 8dc394df 2022-06-29 op rm => [\&cmd_rm, "entries..."],
48 8dc394df 2022-06-29 op tee => [\&cmd_tee, "[-q] entry"],
49 8dc394df 2022-06-29 op tog => [\&cmd_tog, "args ..."],
52 5f8e7670 2022-06-29 op my $usage = "[-h] [command argument ...]";
55 5f8e7670 2022-06-29 op my $prog = basename $0;
56 5f8e7670 2022-06-29 op if (defined($cmd)) {
57 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $cmd $usage";
59 5f8e7670 2022-06-29 op say STDERR "Usage: $prog $usage";
60 8dc394df 2022-06-29 op say STDERR "commands: ", join(' ', sort(keys %subcmd));
65 5f8e7670 2022-06-29 op GetOptions("h|?" => \&usage) or usage();
67 5f8e7670 2022-06-29 op $cmd = shift // 'find';
68 5f8e7670 2022-06-29 op usage() unless defined $subcmd{$cmd};
70 5f8e7670 2022-06-29 op ($fn, $usage) = @{$subcmd{$cmd}};
78 5cdf5adc 2022-05-08 op sub name2file {
79 5cdf5adc 2022-05-08 op my $f = shift;
80 5cdf5adc 2022-05-08 op $f .= ".gpg" unless $f =~ m,\.gpg$,;
84 5cdf5adc 2022-05-08 op # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none
86 5cdf5adc 2022-05-08 op my ($chars, $length) = @_;
87 5cdf5adc 2022-05-08 op my $pass = "";
89 5cdf5adc 2022-05-08 op open(my $fh, '<:raw', '/dev/random')
90 5cdf5adc 2022-05-08 op or die "can't open /dev/random: $!";
91 5cdf5adc 2022-05-08 op my $l = $length;
92 5cdf5adc 2022-05-08 op while ($l gt 0) {
93 5cdf5adc 2022-05-08 op read($fh, my $t, $length * 4)
94 5cdf5adc 2022-05-08 op or die "failed to read /dev/random: $!";
95 5cdf5adc 2022-05-08 op $t =~ s/[^$chars]//g;
96 5cdf5adc 2022-05-08 op $l -= length($t);
101 5cdf5adc 2022-05-08 op return substr($pass, 0, $length);
104 5cdf5adc 2022-05-08 op sub readpass {
105 5cdf5adc 2022-05-08 op # todo some stty black magic to avoid echo
106 5cdf5adc 2022-05-08 op print shift if -t;
107 5cdf5adc 2022-05-08 op my $pass = <>;
108 5cdf5adc 2022-05-08 op die "failed to read stdin: $!" unless defined($pass);
110 5cdf5adc 2022-05-08 op return $pass;
114 1f89d66a 2022-06-29 op my $dir = shift;
115 1f89d66a 2022-06-29 op my $parent = dirname $dir;
116 1f89d66a 2022-06-29 op mkdirs($parent) unless -d $parent || $parent eq '/';
117 1f89d66a 2022-06-29 op mkdir $dir or die "mkdir $dir: $!"
118 1f89d66a 2022-06-29 op unless -d $dir;
121 5cdf5adc 2022-05-08 op sub writepass {
122 5cdf5adc 2022-05-08 op my ($file, $pass) = @_;
124 1f89d66a 2022-06-29 op mkdirs(dirname $file);
126 5cdf5adc 2022-05-08 op my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
127 5cdf5adc 2022-05-08 op '-o', $file);
128 5cdf5adc 2022-05-08 op open my $fh, '|-', @args;
129 5cdf5adc 2022-05-08 op say $fh "$pass";
133 5cdf5adc 2022-05-08 op sub recipient {
134 5cdf5adc 2022-05-08 op open my $fh, '<', "$store/.gpg-id"
135 5cdf5adc 2022-05-08 op or die "can't open recipient file";
136 5cdf5adc 2022-05-08 op my $r = <$fh>;
142 5cdf5adc 2022-05-08 op sub passfind {
143 5cdf5adc 2022-05-08 op my $pattern = shift;
147 5cdf5adc 2022-05-08 op wanted => sub {
148 5cdf5adc 2022-05-08 op if (m,/.git$, || m,/.got$,) {
149 5cdf5adc 2022-05-08 op $File::Find::prune = 1;
153 5cdf5adc 2022-05-08 op return if defined($pattern) && ! m/$pattern/;
154 5cdf5adc 2022-05-08 op return unless -f && m,.gpg$,;
156 5cdf5adc 2022-05-08 op s,^$store/*,,;
158 5cdf5adc 2022-05-08 op push @entries, $_;
160 5cdf5adc 2022-05-08 op no_chdir => 1,
161 5cdf5adc 2022-05-08 op follow_fast => 1,
162 5cdf5adc 2022-05-08 op }, ($store));
163 5cdf5adc 2022-05-08 op return sort(@entries);
167 87197963 2022-06-29 op # discard stdout
168 5cdf5adc 2022-05-08 op open my $fh, '-|', ($got, @_);
173 5cdf5adc 2022-05-08 op sub got_add {
174 f96edc25 2022-06-29 op return got 'add', '-I', shift;
178 f96edc25 2022-06-29 op got 'remove', '-f', shift
183 afdbc7b0 2022-06-29 op my $pid = fork;
184 afdbc7b0 2022-06-29 op die "failed to fork: $!" unless defined $pid;
186 afdbc7b0 2022-06-29 op if ($pid ne 0) {
188 afdbc7b0 2022-06-29 op die "failed to commit changes" if $?;
192 afdbc7b0 2022-06-29 op open (STDOUT, ">&", \*STDERR)
193 afdbc7b0 2022-06-29 op or die "can't redirect stdout to stderr";
194 afdbc7b0 2022-06-29 op exec ($got, 'commit', '-m', shift)
195 afdbc7b0 2022-06-29 op or die "failed to exec $got: $!";
201 5cdf5adc 2022-05-08 op sub cmd_cat {
202 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
203 5cdf5adc 2022-05-08 op usage unless @ARGV;
205 5cdf5adc 2022-05-08 op while (@ARGV) {
206 5cdf5adc 2022-05-08 op my $file = name2file(shift @ARGV);
207 5cdf5adc 2022-05-08 op system ($gpg, @gpg_flags, '-d', $file);
208 5cdf5adc 2022-05-08 op die "failed to exec $gpg: $!" if $? == -1;
212 5cdf5adc 2022-05-08 op sub cmd_find {
213 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
214 5cdf5adc 2022-05-08 op usage if @ARGV gt 1;
216 5cdf5adc 2022-05-08 op map { say $_ } passfind(shift @ARGV);
219 5cdf5adc 2022-05-08 op sub cmd_gen {
220 5cdf5adc 2022-05-08 op my $chars = $default_chars;
221 5cdf5adc 2022-05-08 op my $length = $default_length;
226 5cdf5adc 2022-05-08 op 'c=s' => sub { $chars = $_[1] },
227 5cdf5adc 2022-05-08 op 'h|?' => \&usage,
228 5cdf5adc 2022-05-08 op 'l=i' => sub { $length = $_[1] },
229 9d75d6d8 2022-06-29 op 'n' => \$nop,
232 5cdf5adc 2022-05-08 op usage if @ARGV ne 1;
234 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
235 5cdf5adc 2022-05-08 op my $file = name2file $name;
236 bd71caf6 2022-06-29 op my $renamed = -f $file;
238 5cdf5adc 2022-05-08 op my $pass = gen($chars, $length);
240 9d75d6d8 2022-06-29 op unless ($nop) {
241 9d75d6d8 2022-06-29 op writepass($file, $pass);
242 9d75d6d8 2022-06-29 op got_add $file;
243 bd71caf6 2022-06-29 op got_ci($renamed ? "update $name" : "+$name");
245 9d75d6d8 2022-06-29 op say $pass unless $q;
248 5cdf5adc 2022-05-08 op sub cmd_got {
249 5cdf5adc 2022-05-08 op exec $got, @ARGV;
252 5cdf5adc 2022-05-08 op # TODO: handle moving directories?
254 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
255 5cdf5adc 2022-05-08 op usage if @ARGV ne 2;
257 5cdf5adc 2022-05-08 op my $a = shift @ARGV;
258 5cdf5adc 2022-05-08 op my $b = shift @ARGV;
260 5cdf5adc 2022-05-08 op my $pa = name2file $a;
261 5cdf5adc 2022-05-08 op my $pb = name2file $b;
263 5cdf5adc 2022-05-08 op die "source password doesn't exist" unless -f $pa;
264 5cdf5adc 2022-05-08 op die "target password exists" if -f $pb;
266 5cdf5adc 2022-05-08 op rename $pa, $pb or die "can't rename $a to $b: $!";
269 bd71caf6 2022-06-29 op got_add $pb or die "can't add $pb\n";
270 5cdf5adc 2022-05-08 op got_ci "mv $a $b";
274 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
275 6a455fdf 2022-06-29 op usage unless @ARGV;
277 6a455fdf 2022-06-29 op while (@ARGV) {
278 6a455fdf 2022-06-29 op my $name = shift @ARGV;
279 6a455fdf 2022-06-29 op my $file = name2file $name;
281 6a455fdf 2022-06-29 op got_rm $file;
282 6a455fdf 2022-06-29 op got_ci "-$name";
286 afdbc7b0 2022-06-29 op sub cmd_tee {
289 afdbc7b0 2022-06-29 op 'h|?' => \&usage,
292 5cdf5adc 2022-05-08 op usage if @ARGV ne 1;
294 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
295 5cdf5adc 2022-05-08 op my $file = name2file $name;
297 d69b7902 2022-05-15 op my $pass = readpass "Enter the password: ";
298 d69b7902 2022-05-15 op writepass($file, $pass);
300 5cdf5adc 2022-05-08 op got_add $file;
301 afdbc7b0 2022-06-29 op got_ci (-f $file ? "update $name" : "+$name");
302 b2653662 2022-06-29 op say $pass unless $q;
305 afdbc7b0 2022-06-29 op sub cmd_tog {
306 afdbc7b0 2022-06-29 op exec $tog, @ARGV;