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 $got = $ENV{'PLASS_GOT'} // 'got';
32 my $gpg = $ENV{'PLASS_GPG'} // 'gpg';
33 my @gpg_flags = qw(--quiet --compress-algo=none --no-encrypt-to --use-agent);
36 cat => [\&cmd_cat, "entries..."],
37 edit => [\&cmd_edit, "entry"],
38 find => [\&cmd_find, "[pattern]"],
39 mv => [\&cmd_mv, "from to"],
40 rm => [\&cmd_rm, "entries..."],
41 tee => [\&cmd_tee, "[-q] entry"],
44 my $usage = "[-h] command [argument ...]";
47 my $prog = basename $0;
48 if (defined($cmd) and defined($subcmd{$cmd})) {
49 say STDERR "Usage: $prog $cmd $usage";
51 say STDERR "Usage: $prog $usage";
52 say STDERR "unknown command $cmd" if defined($cmd);
53 say STDERR "commands: ", join(' ', sort(keys %subcmd));
58 GetOptions("h|?" => \&usage) or usage();
60 $cmd = shift // 'find';
61 usage() unless defined $subcmd{$cmd};
63 ($fn, $usage) = @{$subcmd{$cmd}};
73 $f .= ".gpg" unless $f =~ m,\.gpg$,;
79 my $parent = dirname $dir;
80 mkdirs($parent) unless -d $parent || $parent eq '/';
81 mkdir $dir or die "mkdir $dir: $!"
86 my ($file, $pass) = @_;
88 mkdirs(dirname $file);
90 # temporary redirect stdout to $file
91 open(my $stdout, '>&', STDOUT);
92 open(STDOUT, '>', $file);
94 my @args = ($gpg, @gpg_flags, '-e', '-r', recipient(),
96 open my $fh, '|-', @args;
101 open(STDOUT, '>&', $stdout); # restore stdout
103 die "failed to run $gpg\n" unless $ok;
107 open my $fh, '<', "$store/.gpg-id"
108 or die "can't open recipient file";
121 if (m,/.git$, || m,/.got$,) {
122 $File::Find::prune = 1;
125 return unless -f && m,.gpg$,;
130 return if defined($pattern) && ! m/$pattern/i;
136 return sort(@entries);
141 open my $fh, '-|', ($got, @_);
147 return got 'add', '-I', shift;
151 got 'remove', '-f', shift
157 die "failed to fork: $!" unless defined $pid;
161 die "failed to commit changes" if $?;
165 open (STDOUT, ">&", \*STDERR)
166 or die "can't redirect stdout to stderr";
167 exec ($got, 'commit', '-m', shift)
168 or die "failed to exec $got: $!";
175 GetOptions('h|?' => \&usage) or usage;
179 my $file = name2file(shift @ARGV);
180 system ($gpg, @gpg_flags, '-d', $file);
181 die "failed to exec $gpg: $!" if $? == -1;
186 GetOptions('h|?' => \&usage) or usage;
189 my $editor = $ENV{'VISUAL'} // $ENV{'EDITOR'} // 'ed';
191 my $entry = shift @ARGV;
192 my $epath = name2file $entry;
194 my ($fh, $filename) = tempfile "/tmp/plass-XXXXXXXXXX";
196 open (my $stdout, ">&", STDOUT) or die "can't redirect stdout: $!";
198 open (STDOUT, ">&", $fh) or die "can't redirect stdout to $filename";
199 system ($gpg, @gpg_flags, '-d', $epath);
200 die "$gpg exited with $?\n" if $? != 0;
202 # restore stdout so the editor can access the TTY if needed.
203 open (STDOUT, ">&", $stdout) or die "can't restore stdout: $!";
205 my $oldtime = (stat($fh))[9];
207 system ($editor, $filename);
208 die "editor $editor failed\n" if $? != 0;
210 my $newtime = (stat($filename))[9];
212 if ($oldtime == $newtime) {
213 say STDERR "no changes made.";
214 unlink $filename or die "can't delete $filename: $!\n";
218 open(STDIN, '<', $filename) or die "can't redirect stdin: $!";
219 open(STDOUT, '>', $epath) or die "can't redirect stdout: $!";
220 system ($gpg, @gpg_flags, '-e', '-r', recipient(), '-o', '-');
221 unlink $filename or die "can't delete $filename: $!\n";
222 die "gpg failed" if $? != 0;
225 got_ci "update $entry";
229 GetOptions('h|?' => \&usage) or usage;
232 map { say $_ } passfind(shift @ARGV);
235 # TODO: handle moving directories?
237 GetOptions('h|?' => \&usage) or usage;
243 my $pa = name2file $a;
244 my $pb = name2file $b;
246 die "source password doesn't exist" unless -f $pa;
247 die "target password exists" if -f $pb;
250 rename $pa, $pb or die "can't rename $a to $b: $!";
253 got_add $pb or die "can't add $pb\n";
258 GetOptions('h|?' => \&usage) or usage;
262 my $name = shift @ARGV;
263 my $file = name2file $name;
278 my $name = shift @ARGV;
279 my $file = name2file $name;
281 my $msg = -f $file ? "update $name" : "+$name";
284 $pass .= $_ while <STDIN>;
285 die "No content!\n" if $pass eq "";
287 writepass($file, $pass);
291 print $pass unless $q;