Blob


1 #!/usr/bin/env perl
2 #
3 # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 #
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.
8 #
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.
17 use strict;
18 use warnings;
19 use v5.32;
21 use open ":std", ":encoding(UTF-8)";
23 use Getopt::Long qw(:config bundling require_order);
24 use File::Basename;
25 use File::Find;
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 %subcmd = (
35 cat => [\&cmd_cat, "entries..."],
36 find => [\&cmd_find, "[pattern]"],
37 mv => [\&cmd_mv, "from to"],
38 rm => [\&cmd_rm, "entries..."],
39 tee => [\&cmd_tee, "[-q] entry"],
40 );
42 my $usage = "[-h] command [argument ...]";
43 my $cmd;
44 sub usage {
45 my $prog = basename $0;
46 if (defined($cmd) and defined($subcmd{$cmd})) {
47 say STDERR "Usage: $prog $cmd $usage";
48 } else {
49 say STDERR "Usage: $prog $usage";
50 say STDERR "unknown command $cmd" if defined($cmd);
51 say STDERR "commands: ", join(' ', sort(keys %subcmd));
52 }
53 exit 1;
54 }
56 GetOptions("h|?" => \&usage) or usage();
58 $cmd = shift // 'find';
59 usage() unless defined $subcmd{$cmd};
60 my $fn;
61 ($fn, $usage) = @{$subcmd{$cmd}};
62 chdir $store;
63 $fn->();
64 exit 0;
67 # utils
69 sub name2file {
70 my $f = shift;
71 $f .= ".gpg" unless $f =~ m,\.gpg$,;
72 return $f;
73 }
75 sub mkdirs {
76 my $dir = shift;
77 my $parent = dirname $dir;
78 mkdirs($parent) unless -d $parent || $parent eq '/';
79 mkdir $dir or die "mkdir $dir: $!"
80 unless -d $dir;
81 }
83 sub writepass {
84 my ($file, $pass) = @_;
86 mkdirs(dirname $file);
88 # temporary redirect stdout to $file
89 open(my $stdout, '>&', STDOUT);
90 open(STDOUT, '>', $file);
92 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
93 '-o', '-');
94 open my $fh, '|-', @args;
95 print $fh "$pass";
96 close($fh);
97 my $ok = !$?;
99 open(STDOUT, '>&', $stdout); # restore stdout
101 die "failed to run $gpg\n" unless $ok;
104 sub recipient {
105 open my $fh, '<', "$store/.gpg-id"
106 or die "can't open recipient file";
107 my $r = <$fh>;
108 chomp $r;
109 close($fh);
110 return $r;
113 sub passfind {
114 my $pattern = shift;
115 my @entries;
117 find({
118 wanted => sub {
119 if (m,/.git$, || m,/.got$,) {
120 $File::Find::prune = 1;
121 return;
123 return unless -f && m,.gpg$,;
125 s,^$store/*,,;
126 s,.gpg$,,;
128 return if defined($pattern) && ! m/$pattern/;
129 push @entries, $_;
130 },
131 no_chdir => 1,
132 follow_fast => 1,
133 }, ($store));
134 return sort(@entries);
137 sub got {
138 # discard stdout
139 open my $fh, '-|', ($got, @_);
140 close($fh);
141 return !$?;
144 sub got_add {
145 return got 'add', '-I', shift;
148 sub got_rm {
149 got 'remove', '-f', shift
150 or exit(1);
153 sub got_ci {
154 my $pid = fork;
155 die "failed to fork: $!" unless defined $pid;
157 if ($pid != 0) {
158 wait;
159 die "failed to commit changes" if $?;
160 return;
163 open (STDOUT, ">&", \*STDERR)
164 or die "can't redirect stdout to stderr";
165 exec ($got, 'commit', '-m', shift)
166 or die "failed to exec $got: $!";
170 # cmds
172 sub cmd_cat {
173 GetOptions('h|?' => \&usage) or usage;
174 usage unless @ARGV;
176 while (@ARGV) {
177 my $file = name2file(shift @ARGV);
178 system ($gpg, @gpg_flags, '-d', $file);
179 die "failed to exec $gpg: $!" if $? == -1;
183 sub cmd_find {
184 GetOptions('h|?' => \&usage) or usage;
185 usage if @ARGV > 1;
187 map { say $_ } passfind(shift @ARGV);
190 # TODO: handle moving directories?
191 sub cmd_mv {
192 GetOptions('h|?' => \&usage) or usage;
193 usage if @ARGV != 2;
195 my $a = shift @ARGV;
196 my $b = shift @ARGV;
198 my $pa = name2file $a;
199 my $pb = name2file $b;
201 die "source password doesn't exist" unless -f $pa;
202 die "target password exists" if -f $pb;
204 mkdirs(dirname $pb);
205 rename $pa, $pb or die "can't rename $a to $b: $!";
207 got_rm $pa;
208 got_add $pb or die "can't add $pb\n";
209 got_ci "mv $a $b";
212 sub cmd_rm {
213 GetOptions('h|?' => \&usage) or usage;
214 usage unless @ARGV;
216 while (@ARGV) {
217 my $name = shift @ARGV;
218 my $file = name2file $name;
220 got_rm $file;
221 got_ci "-$name";
225 sub cmd_tee {
226 my $q;
227 GetOptions(
228 'h|?' => \&usage,
229 'q' => \$q,
230 ) or usage;
231 usage if @ARGV != 1;
233 my $name = shift @ARGV;
234 my $file = name2file $name;
236 my $msg = -f $file ? "update $name" : "+$name";
238 my $pass = "";
239 $pass .= $_ while <STDIN>;
240 die "No content!\n" if $pass eq "";
242 writepass($file, $pass);
244 got_add $file;
245 got_ci $msg;
246 print $pass unless $q;