-
Notifications
You must be signed in to change notification settings - Fork 13
/
getnetrc
executable file
·182 lines (159 loc) · 4.09 KB
/
getnetrc
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
#!/usr/bin/env perl
# getnetrc -- query passwords from ~/.netrc
use v5.8;
use warnings;
use strict;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use Net::Netrc;
$::arg0 = (split m!/!, $0)[-1];
sub warnx {
my ($msg) = @_;
warn "$::arg0: $msg\n";
}
sub errx {
my ($exit, $msg) = @_;
warnx($msg) if $msg;
exit $exit;
}
my $format = "%l:%p";
my $format_nonewline = 0;
my $format_url_encode = 0;
my $service_required = 0;
my $no_default = 0;
my $quiet = 0;
sub usage {
print "$_\n" for
"Usage: $::arg0 [-dnsu] [-f format] [service/]machine [login]",
"",
"Retrieve a password from the ~/.netrc file.",
"",
"Options:",
" -d disable fallback to 'default' entry",
" -f format format the output as specified (default is %l:%p)",
" -n suppress final newline",
" -s disable fallback from '<service>/<machine>' to '<machine>'",
" -u URL-encode output (each field encoded separately)",
" -q quiet on failure to find machine entry",
"",
"Format strings:",
" %m, %h result machine (hostname)",
" %l, %u result login (username)",
" %p result password",
" %a result account",
" %M query machine",
" %S query service",
" %%, %n, %0 percent sign, newline, null byte",
"",
"The .netrc file format is described in the manual page of ftp(1), with",
"the exception of 'service/machine' syntax which is a custom extension.";
}
sub lookup {
my ($machines, $login) = @_;
my $fallback;
for my $machine (@$machines) {
my $en = Net::Netrc->lookup($machine, $login);
if (defined $en) {
if (defined $en->{machine}) {
return $en;
} elsif (!$no_default) {
$fallback //= $en;
}
}
}
return $fallback;
}
sub _fmt_expn {
my ($data, $raw, $key) = @_;
if ($key eq "%") {
return $key;
} elsif (exists $data->{$key}) {
return $data->{$key} // "";
} else {
warnx("unknown format character '$raw'");
return "$raw";
}
}
sub fmt {
my ($str, $data) = @_;
$str =~ s!%(?:([^{])|\{(.+?)\})!_fmt_expn($data, $&, $1 // $2)!ge;
return $str;
}
sub uri_encode {
my ($str) = @_;
if (defined $str) {
$str =~ s/([^A-Za-z0-9.!~*'()-])/sprintf("%%%02X", ord($1))/seg;
}
return $str;
}
sub lookuparg {
my ($machine, $login, $strip_service) = @_;
my $service;
if ($machine =~ m{^([^/]+)[@/](.+)$}) {
$service = $1;
$machine = $2;
}
my @machines;
if ($service && !$strip_service) {
push @machines, $service.'/'.$machine;
push @machines, $service.'@'.$machine;
} else {
push @machines, $machine;
}
my $entry = lookup(\@machines, $login);
if ($entry) {
my %output = (
%$entry,
a => $entry->{account},
h => $entry->{machine},
l => $entry->{login},
m => $entry->{machine},
p => $entry->{password},
u => $entry->{login},
M => $machine,
S => $service,
);
if ($format_url_encode) {
$output{$_} = uri_encode($output{$_}) for keys %output;
}
@output{"n", "0"} = ("\n", "\0");
if (!$format_nonewline) {
$format .= "%n";
}
print fmt($format, \%output);
return 1;
}
return 0;
}
# parse command line
GetOptions(
"help" => sub { usage(); exit; },
"f|format=s" => \$format,
"n|no-newline" => \$format_nonewline,
"u|urlencode" => \$format_url_encode,
"s|service-required" => \$service_required,
"d|no-default" => \$no_default,
"q|quiet!" => \$quiet,
) or exit 2;
my ($machinearg, $login) = @ARGV;
unless (defined($machinearg) && length($machinearg)) {
errx(2, "missing machine name");
}
my @machinearg = split(/[\s,]+/, $machinearg);
# First try all machine names as-is, then try them again without the "service@"
# prefix. (This prioritizes a service/* match over a */host one, as it's more
# likely that a specific service will have its unique credentials.)
for my $machine (@machinearg) {
exit 0 if lookuparg($machine, $login, 0);
}
if (!$service_required) {
for my $machine (@machinearg) {
exit 0 if lookuparg($machine, $login, 1);
}
}
if ($quiet) {
errx(1);
} elsif (length($login)) {
errx(1, "could not find '$machinearg' as '$login' in ~/.netrc");
} else {
errx(1, "could not find '$machinearg' in ~/.netrc");
}