This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pass
executable file
·201 lines (173 loc) · 4.44 KB
/
pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/perl
use strict;
use warnings qw(all FATAL);
use YAML qw();
use Getopt::Long;
my $home = $ENV{PASS_HOME} || "$ENV{HOME}/.pass";
my @files = glob($ENV{PASS_FILE} || "$home/*.gpg");
# hack because YAML refuses to remember the order of parsing.
# Keys mentioned below take precedence over unmentioned ones,
# and order of mention determines precedence.
my %SORT_MAGIC = do {
my $n = 0;
map { $_ => $n++ } (
qw(desc),
qw(id user number password pin),
qw(routing account),
);
};
my $no_key;
my $generate_autofill;
my $cmd = 'grep';
Getopt::Long::Configure("bundling");
GetOptions(
'no-key|n' => \$no_key,
'list|l' => sub { $cmd = 'list' },
'paste|p' => sub { $cmd = 'paste' },
'autofill' => sub { $cmd = 'autofill' },
) or exit 100;
if ($cmd eq 'autofill') {
foreach my $file (@files) {
my ($user, $pass) = autofill(decrypt($file), @ARGV);
if (defined $user && defined $pass) {
print "$user\n$pass\n";
exit 0;
}
}
exit 1;
}
my @entries = map { match_entries(decrypt($_), @ARGV) } @files;
if ($cmd eq 'grep') {
foreach my $e (@entries) {
print $e->[0], ' ' unless $no_key;
print $e->[1], "\n";
}
}
elsif ($cmd eq 'list') {
foreach my $e (@entries) {
print $e->[0], "\n";
}
}
elsif ($cmd eq 'paste') {
open(my $out, '|-', qw(xsel -i));
foreach my $e (@entries) {
print $out $e->[1], "\n";
}
}
exit 0;
sub decrypt {
my $fn = shift;
return "" unless -e $fn;
open(my $fh, '-|', "gpg -qd --no-tty <" . quotemeta($fn));
local $/;
return YAML::Load(<$fh>);
}
# Poor man's xpath.
sub match_entries_recurse {
my ($entry, $k, $root, $can_skip) = @_;
# If we are at a leaf node, match only if we have
# no key left to match.
if (ref($entry) ne 'HASH') {
if (!@$k) {
my $fullname = join('.', @$root);
return ([$fullname, $entry]);
}
return ();
}
my @r;
foreach my $name (sort keycmp keys(%$entry)) {
my $child = $entry->{$name};
push @$root, $name;
# If we have no key left, then everything under
# us is a match.
if (!@$k) {
push @r, match_entries_recurse($child, $k, $root, $can_skip);
}
# Otherwise, we must match the key, or we
# can skip front nodes. We need to do both
# if possible, because always consuming key
# will miss something like searching for
# "foo.bar" in "foo.foo.bar".
else {
if (entry_matches($name, $child, $k->[0])) {
my $matched = shift @$k;
push @r, match_entries_recurse($child, $k, $root, 0);
unshift @$k, $matched;
}
if ($can_skip) {
push @r, match_entries_recurse($child, $k, $root, $can_skip);
}
}
pop @$root;
}
return @r;
}
sub entry_matches {
my ($name, $entry, $k) = @_;
return 1 if $name =~ $k;
return 1 if ref($entry) eq 'HASH' &&
exists($entry->{desc}) &&
$entry->{desc} =~ $k;
return 0;
}
sub keycmp {
if (exists $SORT_MAGIC{$a}) {
if (exists $SORT_MAGIC{$b}) {
return $SORT_MAGIC{$a} - $SORT_MAGIC{$b};
}
return -1;
}
if (exists $SORT_MAGIC{$b}) {
return 1;
}
return $a cmp $b;
}
sub match_entries {
my $entries = shift;
my @r;
foreach my $k (@_) {
my @regexes = map { qr/$_/i } split /\./, $k;
push @r, match_entries_recurse($entries, \@regexes, [], 1);
}
my %seen;
return grep { !$seen{$_->[0]}++; } @r;
}
sub autofill {
eval 'require URI' or die "unable to load URI module";
my $data = shift;
my $uri = URI->new(shift);
return autofill_recurse($data, $uri);
}
sub autofill_recurse {
my ($data, $want) = @_;
return unless ref($data) eq 'HASH';
my $url = $data->{url};
my $user = one_of($data, qw(user username));
my $pass = one_of($data, qw(pass password));
if (defined $url && defined $user && defined $pass) {
my $have = URI->new($url);
if (autofill_match_uri($have, $want)) {
return ($user, $pass);
}
}
foreach my $k (sort keys(%$data)) {
my @r = autofill_recurse($data->{$k}, $want);
@r and return @r;
}
return ();
}
sub autofill_match_uri {
my ($one, $two) = @_;
# Our strategy is to be picky about hostnames matching exactly,
# which might have false negatives, but not false positives. But
# we are lenient with the path; we assume that all paths at the
# same host are under the same control.
return $one->host eq $two->host;
}
sub one_of {
my $data = shift;
foreach my $option (@_) {
return $data->{$option} if exists $data->{$option};
}
return undef;
}