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 d1bd3477 2022-06-29 op if (defined($cmd) and defined($subcmd{$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 d1bd3477 2022-06-29 op say STDERR "unknown command $cmd" if defined($cmd);
61 8dc394df 2022-06-29 op say STDERR "commands: ", join(' ', sort(keys %subcmd));
66 5f8e7670 2022-06-29 op GetOptions("h|?" => \&usage) or usage();
68 5f8e7670 2022-06-29 op $cmd = shift // 'find';
69 5f8e7670 2022-06-29 op usage() unless defined $subcmd{$cmd};
71 5f8e7670 2022-06-29 op ($fn, $usage) = @{$subcmd{$cmd}};
79 5cdf5adc 2022-05-08 op sub name2file {
80 5cdf5adc 2022-05-08 op my $f = shift;
81 5cdf5adc 2022-05-08 op $f .= ".gpg" unless $f =~ m,\.gpg$,;
85 5cdf5adc 2022-05-08 op # tr -cd -- $chars < /dev/random | dd bs=$len count=1 status=none
87 5cdf5adc 2022-05-08 op my ($chars, $length) = @_;
88 5cdf5adc 2022-05-08 op my $pass = "";
90 5cdf5adc 2022-05-08 op open(my $fh, '<:raw', '/dev/random')
91 5cdf5adc 2022-05-08 op or die "can't open /dev/random: $!";
92 5cdf5adc 2022-05-08 op my $l = $length;
93 5cdf5adc 2022-05-08 op while ($l gt 0) {
94 5cdf5adc 2022-05-08 op read($fh, my $t, $length * 4)
95 5cdf5adc 2022-05-08 op or die "failed to read /dev/random: $!";
96 5cdf5adc 2022-05-08 op $t =~ s/[^$chars]//g;
97 5cdf5adc 2022-05-08 op $l -= length($t);
102 5cdf5adc 2022-05-08 op return substr($pass, 0, $length);
105 5cdf5adc 2022-05-08 op sub readpass {
106 5cdf5adc 2022-05-08 op # todo some stty black magic to avoid echo
107 5cdf5adc 2022-05-08 op print shift if -t;
108 5cdf5adc 2022-05-08 op my $pass = <>;
109 5cdf5adc 2022-05-08 op die "failed to read stdin: $!" unless defined($pass);
111 5cdf5adc 2022-05-08 op return $pass;
115 1f89d66a 2022-06-29 op my $dir = shift;
116 1f89d66a 2022-06-29 op my $parent = dirname $dir;
117 1f89d66a 2022-06-29 op mkdirs($parent) unless -d $parent || $parent eq '/';
118 1f89d66a 2022-06-29 op mkdir $dir or die "mkdir $dir: $!"
119 1f89d66a 2022-06-29 op unless -d $dir;
122 5cdf5adc 2022-05-08 op sub writepass {
123 5cdf5adc 2022-05-08 op my ($file, $pass) = @_;
125 1f89d66a 2022-06-29 op mkdirs(dirname $file);
127 5cdf5adc 2022-05-08 op my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
128 5cdf5adc 2022-05-08 op '-o', $file);
129 5cdf5adc 2022-05-08 op open my $fh, '|-', @args;
130 5cdf5adc 2022-05-08 op say $fh "$pass";
134 5cdf5adc 2022-05-08 op sub recipient {
135 5cdf5adc 2022-05-08 op open my $fh, '<', "$store/.gpg-id"
136 5cdf5adc 2022-05-08 op or die "can't open recipient file";
137 5cdf5adc 2022-05-08 op my $r = <$fh>;
143 5cdf5adc 2022-05-08 op sub passfind {
144 5cdf5adc 2022-05-08 op my $pattern = shift;
148 5cdf5adc 2022-05-08 op wanted => sub {
149 5cdf5adc 2022-05-08 op if (m,/.git$, || m,/.got$,) {
150 5cdf5adc 2022-05-08 op $File::Find::prune = 1;
154 5cdf5adc 2022-05-08 op return if defined($pattern) && ! m/$pattern/;
155 5cdf5adc 2022-05-08 op return unless -f && m,.gpg$,;
157 5cdf5adc 2022-05-08 op s,^$store/*,,;
159 5cdf5adc 2022-05-08 op push @entries, $_;
161 5cdf5adc 2022-05-08 op no_chdir => 1,
162 5cdf5adc 2022-05-08 op follow_fast => 1,
163 5cdf5adc 2022-05-08 op }, ($store));
164 5cdf5adc 2022-05-08 op return sort(@entries);
168 87197963 2022-06-29 op # discard stdout
169 5cdf5adc 2022-05-08 op open my $fh, '-|', ($got, @_);
174 5cdf5adc 2022-05-08 op sub got_add {
175 f96edc25 2022-06-29 op return got 'add', '-I', shift;
179 f96edc25 2022-06-29 op got 'remove', '-f', shift
184 afdbc7b0 2022-06-29 op my $pid = fork;
185 afdbc7b0 2022-06-29 op die "failed to fork: $!" unless defined $pid;
187 afdbc7b0 2022-06-29 op if ($pid ne 0) {
189 afdbc7b0 2022-06-29 op die "failed to commit changes" if $?;
193 afdbc7b0 2022-06-29 op open (STDOUT, ">&", \*STDERR)
194 afdbc7b0 2022-06-29 op or die "can't redirect stdout to stderr";
195 afdbc7b0 2022-06-29 op exec ($got, 'commit', '-m', shift)
196 afdbc7b0 2022-06-29 op or die "failed to exec $got: $!";
202 5cdf5adc 2022-05-08 op sub cmd_cat {
203 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
204 5cdf5adc 2022-05-08 op usage unless @ARGV;
206 5cdf5adc 2022-05-08 op while (@ARGV) {
207 5cdf5adc 2022-05-08 op my $file = name2file(shift @ARGV);
208 5cdf5adc 2022-05-08 op system ($gpg, @gpg_flags, '-d', $file);
209 5cdf5adc 2022-05-08 op die "failed to exec $gpg: $!" if $? == -1;
213 5cdf5adc 2022-05-08 op sub cmd_find {
214 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
215 5cdf5adc 2022-05-08 op usage if @ARGV gt 1;
217 5cdf5adc 2022-05-08 op map { say $_ } passfind(shift @ARGV);
220 5cdf5adc 2022-05-08 op sub cmd_gen {
221 5cdf5adc 2022-05-08 op my $chars = $default_chars;
222 5cdf5adc 2022-05-08 op my $length = $default_length;
227 5cdf5adc 2022-05-08 op 'c=s' => sub { $chars = $_[1] },
228 5cdf5adc 2022-05-08 op 'h|?' => \&usage,
229 5cdf5adc 2022-05-08 op 'l=i' => sub { $length = $_[1] },
230 9d75d6d8 2022-06-29 op 'n' => \$nop,
233 5cdf5adc 2022-05-08 op usage if @ARGV ne 1;
235 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
236 5cdf5adc 2022-05-08 op my $file = name2file $name;
237 bd71caf6 2022-06-29 op my $renamed = -f $file;
239 5cdf5adc 2022-05-08 op my $pass = gen($chars, $length);
241 9d75d6d8 2022-06-29 op unless ($nop) {
242 9d75d6d8 2022-06-29 op writepass($file, $pass);
243 9d75d6d8 2022-06-29 op got_add $file;
244 bd71caf6 2022-06-29 op got_ci($renamed ? "update $name" : "+$name");
246 9d75d6d8 2022-06-29 op say $pass unless $q;
249 5cdf5adc 2022-05-08 op sub cmd_got {
250 5cdf5adc 2022-05-08 op exec $got, @ARGV;
253 5cdf5adc 2022-05-08 op # TODO: handle moving directories?
255 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
256 5cdf5adc 2022-05-08 op usage if @ARGV ne 2;
258 5cdf5adc 2022-05-08 op my $a = shift @ARGV;
259 5cdf5adc 2022-05-08 op my $b = shift @ARGV;
261 5cdf5adc 2022-05-08 op my $pa = name2file $a;
262 5cdf5adc 2022-05-08 op my $pb = name2file $b;
264 5cdf5adc 2022-05-08 op die "source password doesn't exist" unless -f $pa;
265 5cdf5adc 2022-05-08 op die "target password exists" if -f $pb;
267 5cdf5adc 2022-05-08 op rename $pa, $pb or die "can't rename $a to $b: $!";
270 bd71caf6 2022-06-29 op got_add $pb or die "can't add $pb\n";
271 5cdf5adc 2022-05-08 op got_ci "mv $a $b";
275 5cdf5adc 2022-05-08 op GetOptions('h|?' => \&usage) or usage;
276 6a455fdf 2022-06-29 op usage unless @ARGV;
278 6a455fdf 2022-06-29 op while (@ARGV) {
279 6a455fdf 2022-06-29 op my $name = shift @ARGV;
280 6a455fdf 2022-06-29 op my $file = name2file $name;
282 6a455fdf 2022-06-29 op got_rm $file;
283 6a455fdf 2022-06-29 op got_ci "-$name";
287 afdbc7b0 2022-06-29 op sub cmd_tee {
290 afdbc7b0 2022-06-29 op 'h|?' => \&usage,
293 5cdf5adc 2022-05-08 op usage if @ARGV ne 1;
295 5cdf5adc 2022-05-08 op my $name = shift @ARGV;
296 5cdf5adc 2022-05-08 op my $file = name2file $name;
298 d69b7902 2022-05-15 op my $pass = readpass "Enter the password: ";
299 d69b7902 2022-05-15 op writepass($file, $pass);
301 5cdf5adc 2022-05-08 op got_add $file;
302 afdbc7b0 2022-06-29 op got_ci (-f $file ? "update $name" : "+$name");
303 b2653662 2022-06-29 op say $pass unless $q;
306 afdbc7b0 2022-06-29 op sub cmd_tog {
307 afdbc7b0 2022-06-29 op exec $tog, @ARGV;