3 # Copyright (c) 2022, 2023 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);
26 use File::Temp qw(tempfile);
28 my $store = $ENV{'PLASS_STORE'} // $ENV{'HOME'}.'/.password-store';
30 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
31 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
34 cat => [\&cmd_cat, "entries..."],
35 edit => [\&cmd_edit, "entry"],
36 find => [\&cmd_find, "[pattern]"],
37 mv => [\&cmd_mv, "from to"],
38 rm => [\&cmd_rm, "entries..."],
39 tee => [\&cmd_tee, "[-q] entry"],
42 my $usage = "[-h] command [argument ...]";
45 my $prog = basename $0;
46 if (defined($cmd) and defined($subcmd{$cmd})) {
47 say STDERR "Usage: $prog $cmd $usage";
49 say STDERR "Usage: $prog $usage";
50 say STDERR "unknown command $cmd" if defined($cmd);
51 say STDERR "commands: ", join(' ', sort(keys %subcmd));
56 GetOptions("h|?" => \&usage) or usage();
58 $cmd = shift // 'find';
59 usage() unless defined $subcmd{$cmd};
61 ($fn, $usage) = @{$subcmd{$cmd}};
71 $f .= ".gpg" unless $f =~ m,\.gpg$,;
77 my $parent = dirname $dir;
78 mkdirs($parent) unless -d $parent || $parent eq '/';
79 mkdir $dir or die "mkdir $dir: $!"
84 open my $fh, '<', "$store/.gpg-id"
85 or die "can't open recipient file";
98 if (m,/.git$, || m,/.got$,) {
99 $File::Find::prune = 1;
102 return unless -f && m,.gpg$,;
107 return if defined($pattern) && ! m/$pattern/i;
113 return sort(@entries);
118 open my $fh, '-|', ('got', @_);
124 return got 'add', '-I', shift;
128 got 'remove', '-f', shift
134 die "failed to fork: $!" unless defined $pid;
138 die "failed to commit changes" if $?;
142 open (STDOUT, ">&", \*STDERR)
143 or die "can't redirect stdout to stderr";
144 exec ('got', 'commit', '-m', shift)
145 or die "failed to exec got: $!";
152 GetOptions('h|?' => \&usage) or usage;
156 my $file = name2file(shift @ARGV);
157 system ($gpg, @gpg_flags, '-d', $file);
158 die "failed to exec $gpg: $!" if $? == -1;
163 GetOptions('h|?' => \&usage) or usage;
166 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
168 my $entry = shift @ARGV;
169 my $epath = name2file $entry;
171 my ($fh, $filename) = tempfile "/tmp/plass-XXXXXXXXXX";
173 open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
175 open (STDOUT, ">&", $fh) or die "can't redirect stdout to $filename";
176 system ($gpg, @gpg_flags, '-d', $epath);
177 die "$gpg exited with $?\n" if $? != 0;
179 # restore stdout so the editor can access the TTY if needed.
180 open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
182 my $oldtime = (stat($fh))[9];
184 system ($editor, $filename);
185 die "editor $editor failed\n" if $? != 0;
187 my $newtime = (stat($filename))[9];
189 if ($oldtime == $newtime) {
190 say STDERR "no changes made.";
191 unlink $filename or die "can't delete $filename: $!\n";
195 open(STDIN, '<', $filename) or die "can't redirect stdin: $!";
196 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
197 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-');
198 unlink $filename or die "can't delete $filename: $!\n";
199 die "gpg failed" if $? != 0;
202 got_ci "update $entry";
206 GetOptions('h|?' => \&usage) or usage;
209 map { say $_ } passfind(shift @ARGV);
212 # TODO: handle moving directories?
214 GetOptions('h|?' => \&usage) or usage;
220 my $pa = name2file $a;
221 my $pb = name2file $b;
223 die "source password doesn't exist" unless -f $pa;
224 die "target password exists" if -f $pb;
227 rename $pa, $pb or die "can't rename $a to $b: $!";
230 got_add $pb or die "can't add $pb\n";
235 GetOptions('h|?' => \&usage) or usage;
239 my $name = shift @ARGV;
240 my $file = name2file $name;
255 my $name = shift @ARGV;
256 my $file = name2file $name;
258 my $msg = -f $file ? "update $name" : "+$name";
259 mkdirs(dirname $file);
261 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
262 '--batch', '--yes', '-o', $file);
263 open my $fh, '|-', @args;